🤖 Merge PR #47077 Added definitions for argparse version 2 by @doberkofler

This commit is contained in:
Dieter Oberkofler 2020-08-27 14:38:11 +02:00 committed by GitHub
parent acf66c36ca
commit b392c8a4a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 127 additions and 138 deletions

View File

@ -11,147 +11,143 @@ import {
let args: any;
const simpleExample = new ArgumentParser({
version: '0.0.1',
addHelp: true,
add_help: true,
description: 'Argparse example',
});
simpleExample.addArgument(
['-f', '--foo'],
simpleExample.add_argument(
'-f', '--foo',
{
help: 'foo bar',
}
);
simpleExample.addArgument(
['-b', '--bar'],
simpleExample.add_argument(
'-b', '--bar',
{
help: 'bar foo',
}
);
simpleExample.addArgument(
simpleExample.add_argument(
'positional',
{
help: 'bar foo',
}
);
simpleExample.printHelp();
simpleExample.print_help();
console.log('-----------');
args = simpleExample.parseArgs('-f 1 -b2'.split(' '));
args = simpleExample.parse_args('-f 1 -b2'.split(' '));
console.dir(args);
console.log('-----------');
args = simpleExample.parseArgs('-f=3 --bar=4'.split(' '));
args = simpleExample.parse_args('-f=3 --bar=4'.split(' '));
console.dir(args);
console.log('-----------');
args = simpleExample.parseArgs('--foo 5 --bar 6'.split(' '));
args = simpleExample.parse_args('--foo 5 --bar 6'.split(' '));
console.dir(args);
console.log('-----------');
const choicesExample = new ArgumentParser({
version: '0.0.1',
addHelp: true,
add_help: true,
description: 'Argparse examples: choice'
});
choicesExample.addArgument(['foo'], { choices: 'abc' });
choicesExample.add_argument('foo', { choices: 'abc' });
choicesExample.printHelp();
choicesExample.print_help();
console.log('-----------');
args = choicesExample.parseArgs(['c']);
args = choicesExample.parse_args(['c']);
console.dir(args);
console.log('-----------');
// choicesExample.parseArgs(['X']);
// choicesExample.parse_args(['X']);
// console.dir(args);
const constantExample = new ArgumentParser({
version: '0.0.1',
addHelp: true,
add_help: true,
description: 'Argparse examples: constant'
});
constantExample.addArgument(
['-a'],
constantExample.add_argument(
'-a',
{
action: 'storeConst',
action: 'store_const',
dest: 'answer',
help: 'store constant',
constant: 42
const: 42
}
);
constantExample.addArgument(
['--str'],
constantExample.add_argument(
'--str',
{
action: 'appendConst',
action: 'append_const',
dest: 'types',
help: 'append constant "str" to types',
constant: 'str'
const: 'str'
}
);
constantExample.addArgument(
['--int'],
constantExample.add_argument(
'--int',
{
action: 'appendConst',
action: 'append_const',
dest: 'types',
help: 'append constant "int" to types',
constant: 'int'
const: 'int'
}
);
constantExample.addArgument(
['--true'],
constantExample.add_argument(
'--true',
{
action: 'storeTrue',
action: 'store_true',
help: 'store true constant'
}
);
constantExample.addArgument(
['--false'],
constantExample.add_argument(
'--false',
{
action: 'storeFalse',
action: 'store_false',
help: 'store false constant'
}
);
constantExample.printHelp();
constantExample.print_help();
console.log('-----------');
args = constantExample.parseArgs('-a --str --int --true'.split(' '));
args = constantExample.parse_args('-a --str --int --true'.split(' '));
console.dir(args);
const nargsExample = new ArgumentParser({
version: '0.0.1',
addHelp: true,
add_help: true,
description: 'Argparse examples: nargs'
});
nargsExample.addArgument(
['-f', '--foo'],
nargsExample.add_argument(
'-f', '--foo',
{
help: 'foo bar',
nargs: 1
}
);
nargsExample.addArgument(
['-b', '--bar'],
nargsExample.add_argument(
'-b', '--bar',
{
help: 'bar foo',
nargs: '*'
}
);
nargsExample.printHelp();
nargsExample.print_help();
console.log('-----------');
args = nargsExample.parseArgs('--foo a --bar c d'.split(' '));
args = nargsExample.parse_args('--foo a --bar c d'.split(' '));
console.dir(args);
console.log('-----------');
args = nargsExample.parseArgs('--bar b c f --foo a'.split(' '));
args = nargsExample.parse_args('--bar b c f --foo a'.split(' '));
console.dir(args);
const parent_parser = new ArgumentParser({ addHelp: false });
// note addHelp:false to prevent duplication of the -h option
parent_parser.addArgument(
['--parent'],
const parent_parser = new ArgumentParser({ add_help: false });
// note add_help:false to prevent duplication of the -h option
parent_parser.add_argument(
'--parent',
{ type: 'int', help: 'parent' }
);
@ -159,81 +155,79 @@ const foo_parser = new ArgumentParser({
parents: [parent_parser],
description: 'child1'
});
foo_parser.addArgument(['foo']);
args = foo_parser.parseArgs(['--parent', '2', 'XXX']);
foo_parser.add_argument('foo');
args = foo_parser.parse_args(['--parent', '2', 'XXX']);
console.log(args);
const bar_parser = new ArgumentParser({
parents: [parent_parser],
description: 'child2'
});
bar_parser.addArgument(['--bar']);
args = bar_parser.parseArgs(['--bar', 'YYY']);
bar_parser.add_argument('--bar');
args = bar_parser.parse_args(['--bar', 'YYY']);
console.log(args);
const prefixCharsExample = new ArgumentParser({
version: '0.0.1',
addHelp: true,
add_help: true,
description: 'Argparse examples: prefix_chars',
prefixChars: '-+'
prefix_chars: '-+'
});
prefixCharsExample.addArgument(['+f', '++foo']);
prefixCharsExample.addArgument(['++bar'], { action: 'storeTrue' });
prefixCharsExample.add_argument('+f', '++foo');
prefixCharsExample.add_argument('++bar', { action: 'store_true' });
prefixCharsExample.printHelp();
prefixCharsExample.print_help();
console.log('-----------');
args = prefixCharsExample.parseArgs(['+f', '1']);
args = prefixCharsExample.parse_args(['+f', '1']);
console.dir(args);
args = prefixCharsExample.parseArgs(['++bar']);
args = prefixCharsExample.parse_args(['++bar']);
console.dir(args);
args = prefixCharsExample.parseArgs(['++foo', '2', '++bar']);
args = prefixCharsExample.parse_args(['++foo', '2', '++bar']);
console.dir(args);
const subparserExample = new ArgumentParser({
version: '0.0.1',
addHelp: true,
add_help: true,
description: 'Argparse examples: sub-commands'
});
const subparsers = subparserExample.addSubparsers({
const subparsers = subparserExample.add_subparsers({
title: 'subcommands',
dest: "subcommand_name"
});
let bar = subparsers.addParser('c1', { addHelp: true, help: 'c1 help' });
bar.addArgument(
['-f', '--foo'],
let bar = subparsers.add_parser('c1', { add_help: true, help: 'c1 help' });
bar.add_argument(
'-f', '--foo',
{
action: 'store',
help: 'foo3 bar3'
}
);
bar = subparsers.addParser(
bar = subparsers.add_parser(
'c2',
{ aliases: ['co'], addHelp: true, help: 'c2 help' }
{ aliases: ['co'], add_help: true, help: 'c2 help' }
);
bar.addArgument(
['-b', '--bar'],
bar.add_argument(
'-b', '--bar',
{
action: 'store',
type: 'int',
help: 'foo3 bar3'
}
);
subparserExample.printHelp();
subparserExample.print_help();
console.log('-----------');
args = subparserExample.parseArgs('c1 -f 2'.split(' '));
args = subparserExample.parse_args('c1 -f 2'.split(' '));
console.dir(args);
console.log('-----------');
args = subparserExample.parseArgs('c2 -b 1'.split(' '));
args = subparserExample.parse_args('c2 -b 1'.split(' '));
console.dir(args);
console.log('-----------');
args = subparserExample.parseArgs('co -b 1'.split(' '));
args = subparserExample.parse_args('co -b 1'.split(' '));
console.dir(args);
console.log('-----------');
subparserExample.parseArgs(['c1', '-h']);
subparserExample.parse_args(['c1', '-h']);
const functionExample = new ArgumentParser({ description: 'Process some integers.' });
function sum(arr: number[]) {
@ -243,46 +237,46 @@ function max(arr: number[]) {
return Math.max.apply(Math, arr);
}
functionExample.addArgument(['integers'], {
functionExample.add_argument('integers', {
metavar: 'N',
type: 'int',
nargs: '+',
help: 'an integer for the accumulator'
});
functionExample.addArgument(['--sum'], {
functionExample.add_argument('--sum', {
dest: 'accumulate',
action: 'storeConst',
constant: sum,
defaultValue: max,
action: 'store_const',
const: sum,
default: max,
help: 'sum the integers (default: find the max)'
});
args = functionExample.parseArgs('--sum 1 2 -1'.split(' '));
args = functionExample.parse_args('--sum 1 2 -1'.split(' '));
console.log(args.accumulate(args.integers));
const formatterExample = new ArgumentParser({
prog: 'PROG',
formatterClass: RawDescriptionHelpFormatter,
formatter_class: RawDescriptionHelpFormatter,
description: `Keep the formatting\nexactly as it is written\n\nhere\n`,
});
formatterExample.addArgument(['--foo'], {
formatterExample.add_argument('--foo', {
help: `foo help should not\nretain this odd formatting`,
});
formatterExample.addArgument(['spam'], {
formatterExample.add_argument('spam', {
help: 'spam help',
});
const group = formatterExample.addArgumentGroup({
const group = formatterExample.add_argument_group({
title: 'title',
description: `This text\nshould be indented\nexactly like it is here\n`,
});
group.addArgument(['--bar'], {
group.add_argument('--bar', {
help: 'bar help'
});
formatterExample.printHelp();
formatterExample.print_help();
class CustomAction1 extends Action {
constructor(options: ActionConstructorOptions) {
@ -299,43 +293,43 @@ class CustomAction2 extends Action {
}
}
const customActionExample = new ArgumentParser({ addHelp: false });
customActionExample.addArgument('--abc', {
const customActionExample = new ArgumentParser({ add_help: false });
customActionExample.add_argument('--abc', {
action: CustomAction1,
});
customActionExample.addArgument('--def', {
customActionExample.add_argument('--def', {
action: CustomAction2,
});
const constExample = new ArgumentParser();
constExample.addArgument(
['-f', '--foo'],
constExample.add_argument(
'-f', '--foo',
{
help: 'foo bar',
nargs: Const.ONE_OR_MORE
}
);
constExample.addArgument(
['-b', '--bar'],
constExample.add_argument(
'-b', '--bar',
{
help: 'bar foo',
nargs: Const.ZERO_OR_MORE
}
);
constExample.addArgument(
constExample.add_argument(
'--baz',
{
help: 'baz',
nargs: Const.OPTIONAL
}
);
constExample.addArgument(
constExample.add_argument(
'--qux',
{
help: Const.SUPPRESS
}
);
constExample.addArgument(
constExample.add_argument(
'quux',
{
help: 'quux',
@ -343,9 +337,9 @@ constExample.addArgument(
}
);
constExample.printHelp();
constExample.print_help();
console.log('-----------');
args = constExample.parseArgs('--foo x --bar --baz y --qux z a b c d e'.split(' '));
args = constExample.parse_args('--foo x --bar --baz y --qux z a b c d e'.split(' '));
console.dir(args);
console.log('-----------');

View File

@ -1,54 +1,51 @@
// Type definitions for argparse 1.0
// Type definitions for argparse 2.0
// Project: https://github.com/nodeca/argparse
// Definitions by: Andrew Schurman <https://github.com/arcticwaters>
// Tomasz Łaziuk <https://github.com/tlaziuk>
// Sebastian Silbermann <https://github.com/eps1lon>
// Kannan Goundan <https://github.com/cakoose>
// Halvor Holsten Strand <https://github.com/ondkloss>
// Dieter Oberkofler <https://github.com/doberkofler>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
// TypeScript Version: 4.0
export class ArgumentParser extends ArgumentGroup {
constructor(options?: ArgumentParserOptions);
addSubparsers(options?: SubparserOptions): SubParser;
parseArgs(args?: string[], ns?: Namespace | object): any;
printUsage(): void;
printHelp(): void;
formatUsage(): string;
formatHelp(): string;
parseKnownArgs(args?: string[], ns?: Namespace | object): any[];
convertArgLineToArg(argLine: string): string[];
add_subparsers(options?: SubparserOptions): SubParser;
parse_args(args?: string[], ns?: Namespace | object): any;
print_usage(): void;
print_help(): void;
format_usage(): string;
format_help(): string;
parse_known_args(args?: string[], ns?: Namespace | object): any[];
convert_arg_line_to_arg(argLine: string): string[];
exit(status: number, message: string): void;
error(err: string | Error): void;
}
// tslint:disable-next-line:no-unnecessary-class
export class Namespace {
constructor(options: object);
get<K extends keyof this, D extends any>(key: K, defaultValue?: D): this[K] | D;
isset(key: keyof this): boolean;
set<K extends keyof this>(key: K, value: this[K]): this;
set<K extends string, V extends any>(key: K, value: V): this & Record<K, V>;
set<K extends object>(obj: K): this & K;
unset<K extends keyof this, D extends any>(key: K, defaultValue?: D): this[K] | D;
}
export class SubParser {
addParser(name: string, options?: SubArgumentParserOptions): ArgumentParser;
add_parser(name: string, options?: SubArgumentParserOptions): ArgumentParser;
}
export class ArgumentGroup {
addArgument(args: string[] | string, options?: ArgumentOptions): void;
addArgumentGroup(options?: ArgumentGroupOptions): ArgumentGroup;
addMutuallyExclusiveGroup(options?: { required: boolean }): ArgumentGroup;
setDefaults(options?: {}): void;
getDefault(dest: string): any;
add_argument(arg: string, options?: ArgumentOptions): void;
add_argument(arg1: string, arg2: string, options?: ArgumentOptions): void;
add_argument_group(options?: ArgumentGroupOptions): ArgumentGroup;
add_mutually_exclusive_group(options?: { required: boolean }): ArgumentGroup;
set_defaults(options?: {}): void;
get_default(dest: string): any;
}
export interface SubparserOptions {
title?: string;
description?: string;
prog?: string;
parserClass?: { new (): any };
parser_class?: { new (): any };
action?: string;
dest?: string;
help?: string;
@ -63,20 +60,18 @@ export interface SubArgumentParserOptions extends ArgumentParserOptions {
export interface ArgumentParserOptions {
description?: string;
epilog?: string;
addHelp?: boolean;
argumentDefault?: any;
add_help?: boolean;
argument_default?: any;
parents?: ArgumentParser[];
prefixChars?: string;
formatterClass?: { new (): HelpFormatter | ArgumentDefaultsHelpFormatter | RawDescriptionHelpFormatter | RawTextHelpFormatter };
prefix_chars?: string;
formatter_class?: { new (): HelpFormatter | ArgumentDefaultsHelpFormatter | RawDescriptionHelpFormatter | RawTextHelpFormatter };
prog?: string;
usage?: string;
version?: string;
debug?: boolean;
}
export interface ArgumentGroupOptions {
prefixChars?: string;
argumentDefault?: any;
prefix_chars?: string;
argument_default?: any;
title?: string;
description?: string;
}
@ -99,11 +94,11 @@ export class RawTextHelpFormatter { }
export interface ArgumentOptions {
action?: string | { new(options: ActionConstructorOptions): Action };
optionStrings?: string[];
option_strings?: string[];
dest?: string;
nargs?: string | number;
constant?: any;
defaultValue?: any;
const?: any;
default?: any;
// type may be a string (primitive) or a Function (constructor)
type?: string | Function; // tslint:disable-line:ban-types
choices?: string | string[];