From cb67bc6220f575ae192762a95df670a945477c2a Mon Sep 17 00:00:00 2001 From: C-3PO Date: Wed, 24 Oct 2018 04:44:49 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20Rename=20variable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 24 ++++++++++++------------ src/initState.ts | 12 ++++++------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7789aee..4ce023e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,8 +14,8 @@ export default function parseArguments( preParseHook?: (args: string[], fail: (message: string) => void) => (string[] | undefined), ): { fail: (errorMessage: string) => void, args: { [key: string]: string } } { //Initialize state - const { outputArgs, shortToLongLookup, requiredOptions, failFunction } = initState(spec); - const args = runPreParseHook({ args: process.argv.slice(2), preParseHook, fail: failFunction }); + const { outputArgs, shortToLongLookup, requiredOptions, fail } = initState(spec); + 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. //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` */ if (arg === '-' || arg === '--') { - failFunction(`Empty option "-" or "--" is not supported.`); + fail(`Empty option "-" or "--" is not supported.`); } else if (arg.startsWith('--')) { //long argument option = arg.substr(2); let value; @@ -39,11 +39,11 @@ export default function parseArguments( } //Check that argument name is valid 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 !== 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 = ''; } } else if (arg.startsWith('-')) { //short argument @@ -55,19 +55,19 @@ export default function parseArguments( } //Check that argument name is valid if (shortToLongLookup[option] === undefined) { - failFunction(`Unknown short option "-${option}".`); + fail(`Unknown short option "-${option}".`); } option = shortToLongLookup[option]; //If value was provided, check that value is correct and remove name for next loop iteration 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 = ''; } } 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 - 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 = ''; } @@ -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 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 for (const optName in requiredOptions) { 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 { - fail: failFunction, + fail, args: outputArgs, }; } diff --git a/src/initState.ts b/src/initState.ts index fd9356f..83fc796 100644 --- a/src/initState.ts +++ b/src/initState.ts @@ -1,7 +1,7 @@ import failWithError from './failWithError'; import IOption from './IOption'; -const fail = failWithError.bind(null, ''); +const failFunction = failWithError.bind(null, ''); export default function initState(spec: { [key: string]: IOption }) { //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) { if (spec.hasOwnProperty(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]+)*$/)) { - 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; if (shortOption !== undefined && shortOption !== '') { 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; } @@ -43,7 +43,7 @@ export default function initState(spec: { [key: string]: IOption }) { )).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 }; }