✨ Add pre-parse hook to rewrite arguments
This commit is contained in:
parent
ccdeb93349
commit
9bae99cf9a
4 changed files with 23 additions and 6 deletions
|
@ -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.
|
* 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.
|
* 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 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 } } {
|
export default function parseArguments(
|
||||||
//The original arguments from the command line
|
spec: { [key: string]: IOption },
|
||||||
const args = process.argv.slice(2);
|
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
|
//The parsed arguments that are returned by this function
|
||||||
const outputArgs: { [key: string]: string } = {};
|
const outputArgs: { [key: string]: string } = {};
|
||||||
|
|
||||||
|
@ -69,6 +71,21 @@ export default function parseArguments(spec: { [key: string]: IOption }): { fail
|
||||||
|
|
||||||
const failFunction = failWithError.bind(null, usage);
|
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) {
|
function parseValue(argumentValue: string) {
|
||||||
//Verify value for correctness
|
//Verify value for correctness
|
||||||
const verifyFunc = spec[argumentOption].verify;
|
const verifyFunc = spec[argumentOption].verify;
|
||||||
|
|
|
@ -5,7 +5,7 @@ import parseArguments from './funcs/parseArguments';
|
||||||
|
|
||||||
const { args, fail } = 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.` },
|
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
|
//Get manifest and write output to stdout
|
||||||
getManifest(args.product as Product).then((output: IManifest) => {
|
getManifest(args.product as Product).then((output: IManifest) => {
|
||||||
|
|
|
@ -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.` },
|
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 },
|
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 },
|
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
|
//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[]) => {
|
getPatchZip(args.product as Product, Number(args.from), Number(args.to)).then((output: ISsnFileEntry[]) => {
|
||||||
|
|
|
@ -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.` },
|
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 },
|
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 },
|
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
|
//Get solidpkg and write output to stdout
|
||||||
getSolidpkg(args.product as Product, Number(args.from), Number(args.to)).then((output: ISolidSimple) => {
|
getSolidpkg(args.product as Product, Number(args.from), Number(args.to)).then((output: ISolidSimple) => {
|
||||||
|
|
Loading…
Reference in a new issue