♻ Rename variable

This commit is contained in:
C-3PO 2018-10-24 04:44:49 +02:00
parent 4d689564a4
commit cb67bc6220
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
2 changed files with 18 additions and 18 deletions

View file

@ -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,
};
}

View file

@ -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 };
}