♻ Rename variable
This commit is contained in:
parent
4d689564a4
commit
cb67bc6220
2 changed files with 18 additions and 18 deletions
24
src/index.ts
24
src/index.ts
|
@ -14,8 +14,8 @@ export default function parseArguments(
|
||||||
preParseHook?: (args: string[], fail: (message: string) => void) => (string[] | undefined),
|
preParseHook?: (args: string[], fail: (message: string) => void) => (string[] | undefined),
|
||||||
): { fail: (errorMessage: string) => void, args: { [key: string]: string } } {
|
): { fail: (errorMessage: string) => void, args: { [key: string]: string } } {
|
||||||
//Initialize state
|
//Initialize state
|
||||||
const { outputArgs, shortToLongLookup, requiredOptions, failFunction } = initState(spec);
|
const { outputArgs, shortToLongLookup, requiredOptions, fail } = initState(spec);
|
||||||
const args = runPreParseHook({ args: process.argv.slice(2), preParseHook, fail: failFunction });
|
const args = runPreParseHook({ args: process.argv.slice(2), preParseHook, fail });
|
||||||
|
|
||||||
//Iterate through all command line arguments. When we have read both name and value, verify the argument for correctness.
|
//Iterate through all command line arguments. When we have read both name and value, verify the argument for correctness.
|
||||||
//Show error if a name has no value afterwards, or a value has no name in front of it.
|
//Show error if a name has no value afterwards, or a value has no name in front of it.
|
||||||
|
@ -29,7 +29,7 @@ export default function parseArguments(
|
||||||
* - `-n=value`
|
* - `-n=value`
|
||||||
*/
|
*/
|
||||||
if (arg === '-' || arg === '--') {
|
if (arg === '-' || arg === '--') {
|
||||||
failFunction(`Empty option "-" or "--" is not supported.`);
|
fail(`Empty option "-" or "--" is not supported.`);
|
||||||
} else if (arg.startsWith('--')) { //long argument
|
} else if (arg.startsWith('--')) { //long argument
|
||||||
option = arg.substr(2);
|
option = arg.substr(2);
|
||||||
let value;
|
let value;
|
||||||
|
@ -39,11 +39,11 @@ export default function parseArguments(
|
||||||
}
|
}
|
||||||
//Check that argument name is valid
|
//Check that argument name is valid
|
||||||
if (spec[option] === undefined) {
|
if (spec[option] === undefined) {
|
||||||
failFunction(`Unknown option "--${option}".`);
|
fail(`Unknown option "--${option}".`);
|
||||||
}
|
}
|
||||||
//If value was provided, check that value is correct and remove name for next loop iteration
|
//If value was provided, check that value is correct and remove name for next loop iteration
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
verifyAndStoreOptionValue({option, value, verify: spec[option].verify, message: spec[option].message, fail: failFunction, outputArgs, requiredOptions});
|
verifyAndStoreOptionValue({option, value, verify: spec[option].verify, message: spec[option].message, fail, outputArgs, requiredOptions});
|
||||||
option = '';
|
option = '';
|
||||||
}
|
}
|
||||||
} else if (arg.startsWith('-')) { //short argument
|
} else if (arg.startsWith('-')) { //short argument
|
||||||
|
@ -55,19 +55,19 @@ export default function parseArguments(
|
||||||
}
|
}
|
||||||
//Check that argument name is valid
|
//Check that argument name is valid
|
||||||
if (shortToLongLookup[option] === undefined) {
|
if (shortToLongLookup[option] === undefined) {
|
||||||
failFunction(`Unknown short option "-${option}".`);
|
fail(`Unknown short option "-${option}".`);
|
||||||
}
|
}
|
||||||
option = shortToLongLookup[option];
|
option = shortToLongLookup[option];
|
||||||
//If value was provided, check that value is correct and remove name for next loop iteration
|
//If value was provided, check that value is correct and remove name for next loop iteration
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
verifyAndStoreOptionValue({option, value, verify: spec[option].verify, message: spec[option].message, fail: failFunction, outputArgs, requiredOptions});
|
verifyAndStoreOptionValue({option, value, verify: spec[option].verify, message: spec[option].message, fail, outputArgs, requiredOptions});
|
||||||
option = '';
|
option = '';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
failFunction(`Arguments must be preceded by an option but there was no option in front of "${arg}".`);
|
fail(`Arguments must be preceded by an option but there was no option in front of "${arg}".`);
|
||||||
}
|
}
|
||||||
} else { //We expect a value, can be anything
|
} else { //We expect a value, can be anything
|
||||||
verifyAndStoreOptionValue({option, value: arg, verify: spec[option].verify, message: spec[option].message, fail: failFunction, outputArgs, requiredOptions});
|
verifyAndStoreOptionValue({option, value: arg, verify: spec[option].verify, message: spec[option].message, fail, outputArgs, requiredOptions});
|
||||||
option = '';
|
option = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,18 +75,18 @@ export default function parseArguments(
|
||||||
|
|
||||||
// argumentName must be cleared to '' after the value is read, so if it is not an empty string, the value was missing
|
// argumentName must be cleared to '' after the value is read, so if it is not an empty string, the value was missing
|
||||||
if (option !== '') {
|
if (option !== '') {
|
||||||
failFunction(`Option "${option}" was not followed by a value.`);
|
fail(`Option "${option}" was not followed by a value.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
//check if any entry in requiredArguments was not set
|
//check if any entry in requiredArguments was not set
|
||||||
for (const optName in requiredOptions) {
|
for (const optName in requiredOptions) {
|
||||||
if (spec.hasOwnProperty(optName) && requiredOptions[optName] === false) {
|
if (spec.hasOwnProperty(optName) && requiredOptions[optName] === false) {
|
||||||
failFunction(`Missing option "${optName}" even though it is required.`);
|
fail(`Missing option "${optName}" even though it is required.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
fail: failFunction,
|
fail,
|
||||||
args: outputArgs,
|
args: outputArgs,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import failWithError from './failWithError';
|
import failWithError from './failWithError';
|
||||||
import IOption from './IOption';
|
import IOption from './IOption';
|
||||||
|
|
||||||
const fail = failWithError.bind(null, '');
|
const failFunction = failWithError.bind(null, '');
|
||||||
|
|
||||||
export default function initState(spec: { [key: string]: IOption }) {
|
export default function initState(spec: { [key: string]: IOption }) {
|
||||||
//The parsed arguments that are returned by this function
|
//The parsed arguments that are returned by this function
|
||||||
|
@ -13,16 +13,16 @@ export default function initState(spec: { [key: string]: IOption }) {
|
||||||
for (const longOption in spec) {
|
for (const longOption in spec) {
|
||||||
if (spec.hasOwnProperty(longOption)) {
|
if (spec.hasOwnProperty(longOption)) {
|
||||||
if (longOption === '') {
|
if (longOption === '') {
|
||||||
fail(`The long option may not be an empty string. This is a bug in the source code of the script that you tried to call.`);
|
failFunction(`The long option may not be an empty string. This is a bug in the source code of the script that you tried to call.`);
|
||||||
}
|
}
|
||||||
if (!longOption.match(/^[a-z0-9]+(-[a-z0-9]+)*$/)) {
|
if (!longOption.match(/^[a-z0-9]+(-[a-z0-9]+)*$/)) {
|
||||||
fail(`The long option "${longOption}" must only contain alpha-numeric characters, optionally separated by hyphens. This is a bug in the source code of the script that you tried to call.`);
|
failFunction(`The long option "${longOption}" must only contain alpha-numeric characters, optionally separated by hyphens. This is a bug in the source code of the script that you tried to call.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const shortOption = spec[longOption].short;
|
const shortOption = spec[longOption].short;
|
||||||
if (shortOption !== undefined && shortOption !== '') {
|
if (shortOption !== undefined && shortOption !== '') {
|
||||||
if (shortOption.length > 1) {
|
if (shortOption.length > 1) {
|
||||||
fail(`Short options must only be one character long but the short option "${shortOption}" for "${longOption}" was ${shortOption.length} characters long. This is a bug in the source code of the script that you tried to call.`);
|
failFunction(`Short options must only be one character long but the short option "${shortOption}" for "${longOption}" was ${shortOption.length} characters long. This is a bug in the source code of the script that you tried to call.`);
|
||||||
}
|
}
|
||||||
shortToLongLookup[shortOption] = longOption;
|
shortToLongLookup[shortOption] = longOption;
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ export default function initState(spec: { [key: string]: IOption }) {
|
||||||
)).join('\n')
|
)).join('\n')
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
const failFunction = failWithError.bind(null, usage);
|
const failWithUsage = failWithError.bind(null, usage);
|
||||||
|
|
||||||
return { outputArgs, shortToLongLookup, requiredOptions, failFunction };
|
return { outputArgs, shortToLongLookup, requiredOptions, fail: failWithUsage };
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue