From 6f917d351eede7297e65132dbb3f33a3622be6c3 Mon Sep 17 00:00:00 2001 From: Daniel Nadeau <3473356+D4N14L@users.noreply.github.com> Date: Wed, 20 Apr 2022 14:37:34 -0700 Subject: [PATCH] Update argparse version --- libraries/ts-command-line/package.json | 4 +- .../src/providers/CommandLineAction.ts | 2 +- .../providers/CommandLineParameterProvider.ts | 43 +++++++------ .../src/providers/CommandLineParser.ts | 11 ++-- .../CommandLineParameter.test.ts.snap | 61 ++++++------------- .../CommandLineRemainder.test.ts.snap | 28 ++++++--- 6 files changed, 71 insertions(+), 78 deletions(-) diff --git a/libraries/ts-command-line/package.json b/libraries/ts-command-line/package.json index cab7c32bc5f..e389f405916 100644 --- a/libraries/ts-command-line/package.json +++ b/libraries/ts-command-line/package.json @@ -16,8 +16,8 @@ }, "license": "MIT", "dependencies": { - "@types/argparse": "1.0.38", - "argparse": "~1.0.9", + "@types/argparse": "2.0.10", + "argparse": "~2.0.1", "colors": "~1.2.1", "string-argv": "~0.3.1" }, diff --git a/libraries/ts-command-line/src/providers/CommandLineAction.ts b/libraries/ts-command-line/src/providers/CommandLineAction.ts index a0bd8deb4d9..c3e79814cbe 100644 --- a/libraries/ts-command-line/src/providers/CommandLineAction.ts +++ b/libraries/ts-command-line/src/providers/CommandLineAction.ts @@ -78,7 +78,7 @@ export abstract class CommandLineAction extends CommandLineParameterProvider { * @internal */ public _buildParser(actionsSubParser: argparse.SubParser): void { - this._argumentParser = actionsSubParser.addParser(this.actionName, { + this._argumentParser = actionsSubParser.add_parser(this.actionName, { help: this.summary, description: this.documentation }); diff --git a/libraries/ts-command-line/src/providers/CommandLineParameterProvider.ts b/libraries/ts-command-line/src/providers/CommandLineParameterProvider.ts index 48e0818d01f..73bd82030a2 100644 --- a/libraries/ts-command-line/src/providers/CommandLineParameterProvider.ts +++ b/libraries/ts-command-line/src/providers/CommandLineParameterProvider.ts @@ -272,11 +272,11 @@ export abstract class CommandLineParameterProvider { const argparseOptions: argparse.ArgumentOptions = { help: this._remainder.description, - nargs: argparse.Const.REMAINDER, + nargs: argparse.REMAINDER, metavar: '"..."' }; - this._getArgumentParser().addArgument(argparse.Const.REMAINDER, argparseOptions); + this._getArgumentParser().add_argument(argparse.REMAINDER, argparseOptions); return this._remainder; } @@ -294,7 +294,7 @@ export abstract class CommandLineParameterProvider { * Generates the command-line help text. */ public renderHelpText(): string { - return this._getArgumentParser().formatHelp(); + return this._getArgumentParser().format_help(); } /** @@ -361,7 +361,7 @@ export abstract class CommandLineParameterProvider { } if (this.remainder) { - this.remainder._setValue(data[argparse.Const.REMAINDER]); + this.remainder._setValue(data[argparse.REMAINDER]); } this._parametersProcessed = true; @@ -396,12 +396,6 @@ export abstract class CommandLineParameterProvider { ); } - const names: string[] = []; - if (parameter.shortName) { - names.push(parameter.shortName); - } - names.push(parameter.longName); - parameter._parserKey = this._generateKey(); let finalDescription: string = parameter.description; @@ -422,10 +416,16 @@ export abstract class CommandLineParameterProvider { const argparseOptions: argparse.ArgumentOptions = { help: finalDescription, dest: parameter._parserKey, - metavar: (parameter as CommandLineParameterWithArgument).argumentName || undefined, required: parameter.required }; + // Only add the metavar if it's specified. Setting to undefined will cause argparse to throw when + // metavar isn't + const metavarValue: string | undefined = (parameter as CommandLineParameterWithArgument).argumentName; + if (metavarValue) { + argparseOptions.metavar = metavarValue; + } + switch (parameter.kind) { case CommandLineParameterKind.Choice: { const choiceParameter: CommandLineChoiceParameter = parameter as CommandLineChoiceParameter; @@ -439,7 +439,7 @@ export abstract class CommandLineParameterProvider { break; } case CommandLineParameterKind.Flag: - argparseOptions.action = 'storeTrue'; + argparseOptions.action = 'store_true'; break; case CommandLineParameterKind.Integer: argparseOptions.type = 'int'; @@ -456,12 +456,19 @@ export abstract class CommandLineParameterProvider { } const argumentParser: argparse.ArgumentParser = this._getArgumentParser(); - argumentParser.addArgument(names, { ...argparseOptions }); - if (parameter.undocumentedSynonyms && parameter.undocumentedSynonyms.length > 0) { - argumentParser.addArgument(parameter.undocumentedSynonyms, { - ...argparseOptions, - help: argparse.Const.SUPPRESS - }); + if (parameter.shortName) { + argumentParser.add_argument(parameter.shortName, parameter.longName, { ...argparseOptions }); + } else { + argumentParser.add_argument(parameter.longName, { ...argparseOptions }); + } + + if (parameter.undocumentedSynonyms) { + for (const undocumentedSynonym of parameter.undocumentedSynonyms) { + argumentParser.add_argument(undocumentedSynonym, { + ...argparseOptions, + help: argparse.SUPPRESS + }); + } } this._parameters.push(parameter); diff --git a/libraries/ts-command-line/src/providers/CommandLineParser.ts b/libraries/ts-command-line/src/providers/CommandLineParser.ts index f14888886ec..9ccf390a5ac 100644 --- a/libraries/ts-command-line/src/providers/CommandLineParser.ts +++ b/libraries/ts-command-line/src/providers/CommandLineParser.ts @@ -64,7 +64,7 @@ export abstract class CommandLineParser extends CommandLineParameterProvider { this._actionsByName = new Map(); this._argumentParser = new CustomArgumentParser({ - addHelp: true, + add_help: true, prog: this._options.toolFilename, description: this._options.toolDescription, epilog: colors.bold( @@ -87,7 +87,7 @@ export abstract class CommandLineParser extends CommandLineParameterProvider { */ public addAction(action: CommandLineAction): void { if (!this._actionsSubParser) { - this._actionsSubParser = this._argumentParser.addSubparsers({ + this._actionsSubParser = this._argumentParser.add_subparsers({ metavar: '', dest: 'action' }); @@ -195,11 +195,14 @@ export abstract class CommandLineParser extends CommandLineParameterProvider { args = process.argv.slice(2); } if (args.length === 0) { - this._argumentParser.printHelp(); + this._argumentParser.print_help(); return; } - const data: ICommandLineParserData = this._argumentParser.parseArgs(args); + const data: ICommandLineParserData = { + parserOptions: this._options, + ...this._argumentParser.parse_args(args) + } this._processParsedData(data); diff --git a/libraries/ts-command-line/src/test/__snapshots__/CommandLineParameter.test.ts.snap b/libraries/ts-command-line/src/test/__snapshots__/CommandLineParameter.test.ts.snap index ed13c337e1d..1fbf55655f3 100644 --- a/libraries/ts-command-line/src/test/__snapshots__/CommandLineParameter.test.ts.snap +++ b/libraries/ts-command-line/src/test/__snapshots__/CommandLineParameter.test.ts.snap @@ -542,63 +542,36 @@ Array [ `; exports[`CommandLineParameter prints the action help 1`] = ` -"usage: example do:the-job [-h] [-c {one,two,three,default}] - [--choice-with-default {one,two,three,default}] - [-C {red,green,blue}] [-f] [-i NUMBER] - [--integer-with-default NUMBER] --integer-required - NUMBER [-I LIST_ITEM] [-s TEXT] - [--string-with-default TEXT] - [--string-with-undocumented-synonym TEXT] - [-l LIST_ITEM] - +"usage: example do:the-job [-h] [-c {one,two,three,default}] [--choice-with-default {one,two,three,default}] [-C {red,green,blue}] [-f] [-i NUMBER] [--integer-with-default NUMBER] --integer-required NUMBER [-I LIST_ITEM] [-s TEXT] + [--string-with-default TEXT] [--string-with-undocumented-synonym TEXT] [-l LIST_ITEM] a longer description -Optional arguments: - -h, --help Show this help message and exit. +optional arguments: + -h, --help show this help message and exit -c {one,two,three,default}, --choice {one,two,three,default} - A choice. This parameter may alternatively be - specified via the ENV_CHOICE environment variable. + A choice. This parameter may alternatively be specified via the ENV_CHOICE environment variable. --choice-with-default {one,two,three,default} - A choice with a default. This description ends with a - \\"quoted word\\". This parameter may alternatively be - specified via the ENV_CHOICE2 environment variable. - The default value is \\"default\\". + A choice with a default. This description ends with a \\"quoted word\\". This parameter may alternatively be specified via the ENV_CHOICE2 environment variable. The default value is \\"default\\". -C {red,green,blue}, --choice-list {red,green,blue} - This parameter may be specified multiple times to - make a list of choices. This parameter may - alternatively be specified via the ENV_CHOICE_LIST - environment variable. - -f, --flag A flag. This parameter may alternatively be specified - via the ENV_FLAG environment variable. + This parameter may be specified multiple times to make a list of choices. This parameter may alternatively be specified via the ENV_CHOICE_LIST environment variable. + -f, --flag A flag. This parameter may alternatively be specified via the ENV_FLAG environment variable. -i NUMBER, --integer NUMBER - An integer. This parameter may alternatively be - specified via the ENV_INTEGER environment variable. + An integer. This parameter may alternatively be specified via the ENV_INTEGER environment variable. --integer-with-default NUMBER - An integer with a default. This parameter may - alternatively be specified via the ENV_INTEGER2 - environment variable. The default value is 123. + An integer with a default. This parameter may alternatively be specified via the ENV_INTEGER2 environment variable. The default value is 123. --integer-required NUMBER An integer -I LIST_ITEM, --integer-list LIST_ITEM - This parameter may be specified multiple times to - make a list of integers. This parameter may - alternatively be specified via the ENV_INTEGER_LIST - environment variable. + This parameter may be specified multiple times to make a list of integers. This parameter may alternatively be specified via the ENV_INTEGER_LIST environment variable. -s TEXT, --string TEXT - A string. This parameter may alternatively be - specified via the ENV_STRING environment variable. + A string. This parameter may alternatively be specified via the ENV_STRING environment variable. --string-with-default TEXT - A string with a default. This parameter may - alternatively be specified via the ENV_STRING2 - environment variable. The default value is \\"123\\". + A string with a default. This parameter may alternatively be specified via the ENV_STRING2 environment variable. The default value is \\"123\\". --string-with-undocumented-synonym TEXT A string with an undocumented synonym -l LIST_ITEM, --string-list LIST_ITEM - This parameter may be specified multiple times to - make a list of strings. This parameter may - alternatively be specified via the ENV_STRING_LIST - environment variable. + This parameter may be specified multiple times to make a list of strings. This parameter may alternatively be specified via the ENV_STRING_LIST environment variable. " `; @@ -607,12 +580,12 @@ exports[`CommandLineParameter prints the global help 1`] = ` An example project -Positional arguments: +positional arguments: do:the-job does the job -Optional arguments: - -h, --help Show this help message and exit. +optional arguments: + -h, --help show this help message and exit -g, --global-flag A flag that affects all actions For detailed help about a specific command, use: example -h diff --git a/libraries/ts-command-line/src/test/__snapshots__/CommandLineRemainder.test.ts.snap b/libraries/ts-command-line/src/test/__snapshots__/CommandLineRemainder.test.ts.snap index e11dc7a569d..724c4350959 100644 --- a/libraries/ts-command-line/src/test/__snapshots__/CommandLineRemainder.test.ts.snap +++ b/libraries/ts-command-line/src/test/__snapshots__/CommandLineRemainder.test.ts.snap @@ -12,16 +12,26 @@ Array [ ] `; +exports[`CommandLineRemainder parses an action input with remainder options 1`] = ` +Array [ + "### --title output: ###", + "--title", + "The title", + "### remainder output: ###", + "--", + "--the", + "--remaining", + "--args", +] +`; + exports[`CommandLineRemainder prints the action help 1`] = ` -"usage: example run [-h] [--title TEXT] ... +"usage: example run [-h] [--title TEXT] a longer description -Positional arguments: - \\"...\\" The action remainder - -Optional arguments: - -h, --help Show this help message and exit. +optional arguments: + -h, --help show this help message and exit --title TEXT A string " `; @@ -31,12 +41,12 @@ exports[`CommandLineRemainder prints the global help 1`] = ` An example project -Positional arguments: +positional arguments: run does the job -Optional arguments: - -h, --help Show this help message and exit. +optional arguments: + -h, --help show this help message and exit --verbose A flag that affects all actions For detailed help about a specific command, use: example -h