Skip to content

Commit

Permalink
Update argparse version
Browse files Browse the repository at this point in the history
  • Loading branch information
D4N14L committed Apr 20, 2022
1 parent 7386ee3 commit 6f917d3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 78 deletions.
4 changes: 2 additions & 2 deletions libraries/ts-command-line/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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';
Expand All @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions libraries/ts-command-line/src/providers/CommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export abstract class CommandLineParser extends CommandLineParameterProvider {
this._actionsByName = new Map<string, CommandLineAction>();

this._argumentParser = new CustomArgumentParser({
addHelp: true,
add_help: true,
prog: this._options.toolFilename,
description: this._options.toolDescription,
epilog: colors.bold(
Expand All @@ -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: '<command>',
dest: 'action'
});
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"
`;

Expand All @@ -607,12 +580,12 @@ exports[`CommandLineParameter prints the global help 1`] = `
An example project
Positional arguments:
positional arguments:
<command>
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 <command> -h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
`;
Expand All @@ -31,12 +41,12 @@ exports[`CommandLineRemainder prints the global help 1`] = `
An example project
Positional arguments:
positional arguments:
<command>
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 <command> -h
Expand Down

0 comments on commit 6f917d3

Please sign in to comment.