Add pre-parse hook to rewrite arguments

This commit is contained in:
C-3PO 2018-10-23 20:34:30 +02:00
parent ccdeb93349
commit 9bae99cf9a
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
4 changed files with 23 additions and 6 deletions

View file

@ -20,10 +20,12 @@ interface IOption {
* Checks the command line arguments against the given specification, and returns the arguments as a JavaScript object, as well as a failure function that prints the usage instructions before terminating.
* If an error is found, outputs an error message and terminates with a non-zero exit code.
* @param spec A specification of what arguments are expected.
* @param preParseHook Optionally, a function to modify the command line arguments before parsing them.
*/
export default function parseArguments(spec: { [key: string]: IOption }): { fail: (errorMessage: string) => void, args: { [key: string]: string } } {
//The original arguments from the command line
const args = process.argv.slice(2);
export default function parseArguments(
spec: { [key: string]: IOption },
preParseHook?: (args: string[], fail: (message: string) => void) => (string[] | undefined),
): { fail: (errorMessage: string) => void, args: { [key: string]: string } } {
//The parsed arguments that are returned by this function
const outputArgs: { [key: string]: string } = {};
@ -69,6 +71,21 @@ export default function parseArguments(spec: { [key: string]: IOption }): { fail
const failFunction = failWithError.bind(null, usage);
//The original arguments from the command line
let args = process.argv.slice(2);
//If defined, run the pre-parse hook to modify the arguments
if (preParseHook !== undefined) {
try {
const modifiedArgs = preParseHook(args, failFunction);
if (modifiedArgs !== undefined) {
args = modifiedArgs;
}
} catch (error) {
failFunction(`Could not run pre-parse hook, encountered error: ${String(error)}.`);
}
}
function parseValue(argumentValue: string) {
//Verify value for correctness
const verifyFunc = spec[argumentOption].verify;

View file

@ -5,7 +5,7 @@ import parseArguments from './funcs/parseArguments';
const { args, fail } = parseArguments({
product: { short: 'p', description: 'Name of the product we want to get the manifest on', verify: verifyProductName, message: (value) => `"${value}" is not a valid product name.` },
});
}, (origArgs) => ((origArgs.length === 1) ? ['--product', origArgs[0]] : undefined));
//Get manifest and write output to stdout
getManifest(args.product as Product).then((output: IManifest) => {

View file

@ -7,7 +7,7 @@ const { args, fail } = parseArguments({
product: { short: 'p', description: 'Name of the product we want to get the patch info on', verify: verifyProductName, message: (value) => `"${value}" is not a valid product name.` },
from: { short: 'f', description: 'Number of the from release, must be an integer in the range [-1, 999]', verify: (str) => str.match(/^(-1|0|[1-9][0-9]{0,2})$/) !== null },
to: { short: 't', description: 'Number of the to release, must be an integer in the range [0, 999]', verify: (str) => str.match(/^(0|[1-9][0-9]{0,2})$/) !== null },
});
}, (origArgs) => ((origArgs.length === 3) ? ['--product', origArgs[0], '--from', origArgs[1], '--to', origArgs[2]] : undefined));
//Get the .zip file from this patch and write output to stdout
getPatchZip(args.product as Product, Number(args.from), Number(args.to)).then((output: ISsnFileEntry[]) => {

View file

@ -7,7 +7,7 @@ const { args, fail } = parseArguments({
product: { short: 'p', description: 'Name of the product we want to get the solidpkg on', verify: verifyProductName, message: (value) => `"${value}" is not a valid product name.` },
from: { short: 'f', description: 'Number of the from release, must be an integer in the range [-1, 999]', verify: (str) => str.match(/^(-1|0|[1-9][0-9]{0,2})$/) !== null },
to: { short: 't', description: 'Number of the to release, must be an integer in the range [0, 999]', verify: (str) => str.match(/^(0|[1-9][0-9]{0,2})$/) !== null },
});
}, (origArgs) => ((origArgs.length === 3) ? ['--product', origArgs[0], '--from', origArgs[1], '--to', origArgs[2]] : undefined));
//Get solidpkg and write output to stdout
getSolidpkg(args.product as Product, Number(args.from), Number(args.to)).then((output: ISolidSimple) => {