UNPKG

5.1 kBTypeScriptView Raw
1// Type definitions for argparse 2.0
2// Project: https://github.com/nodeca/argparse
3// Definitions by: Andrew Schurman <https://github.com/arcticwaters>
4// Tomasz Łaziuk <https://github.com/tlaziuk>
5// Sebastian Silbermann <https://github.com/eps1lon>
6// Kannan Goundan <https://github.com/cakoose>
7// Halvor Holsten Strand <https://github.com/ondkloss>
8// Dieter Oberkofler <https://github.com/doberkofler>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10// TypeScript Version: 4.0
11
12export class ArgumentParser extends ArgumentGroup {
13 constructor(options?: ArgumentParserOptions);
14 add_subparsers(options?: SubparserOptions): SubParser;
15 parse_args(args?: string[], ns?: Namespace | object): any;
16 print_usage(): void;
17 print_help(): void;
18 format_usage(): string;
19 format_help(): string;
20 parse_known_args(args?: string[], ns?: Namespace | object): any[];
21 convert_arg_line_to_args(argLine: string): string[];
22 exit(status: number, message: string): void;
23 error(err: string | Error): void;
24}
25
26// tslint:disable-next-line:no-unnecessary-class
27export class Namespace {
28 constructor(options: object);
29 [key: string]: any;
30}
31
32export class SubParser {
33 add_parser(name: string, options?: SubArgumentParserOptions): ArgumentParser;
34}
35
36export class ArgumentGroup {
37 add_argument(arg: string, options?: ArgumentOptions): void;
38 add_argument(arg1: string, arg2: string, options?: ArgumentOptions): void;
39 add_argument_group(options?: ArgumentGroupOptions): ArgumentGroup;
40 add_mutually_exclusive_group(options?: { required: boolean }): ArgumentGroup;
41 set_defaults(options?: {}): void;
42 get_default(dest: string): any;
43}
44
45export interface SubparserOptions {
46 title?: string | undefined;
47 description?: string | undefined;
48 prog?: string | undefined;
49 parser_class?: { new (): any } | undefined;
50 action?: string | undefined;
51 dest?: string | undefined;
52 help?: string | undefined;
53 metavar?: string | undefined;
54 required?: boolean | undefined;
55}
56
57export interface SubArgumentParserOptions extends ArgumentParserOptions {
58 aliases?: string[] | undefined;
59 help?: string | undefined;
60}
61
62export interface ArgumentParserOptions {
63 description?: string | undefined;
64 epilog?: string | undefined;
65 add_help?: boolean | undefined;
66 argument_default?: any;
67 parents?: ArgumentParser[] | undefined;
68 prefix_chars?: string | undefined;
69 formatter_class?: {
70 new (): HelpFormatter | ArgumentDefaultsHelpFormatter | RawDescriptionHelpFormatter | RawTextHelpFormatter;
71 } | undefined;
72 prog?: string | undefined;
73 usage?: string | undefined;
74 exit_on_error?: boolean | undefined;
75}
76
77export interface ArgumentGroupOptions {
78 prefix_chars?: string | undefined;
79 argument_default?: any;
80 title?: string | undefined;
81 description?: string | undefined;
82}
83
84export abstract class Action {
85 protected dest: string;
86 constructor(options: ActionConstructorOptions);
87 abstract call(
88 parser: ArgumentParser,
89 namespace: Namespace,
90 values: string | string[],
91 optionString: string | null,
92 ): void;
93}
94
95// Can be used in conjunction with the exit_on_error flag to save the error message
96// and use it in a fashion other than printing to stdout.
97export class ArgumentError extends Error {
98 constructor(argument: Action, message: string);
99 str(): string;
100}
101
102// An error from trying to convert a command line string to a type.
103export class ArgumentTypeError extends Error {
104 constructor(message: string);
105}
106
107// Passed to the Action constructor. Subclasses are just expected to relay this to
108// the super() constructor, so using an "opaque type" pattern is probably fine.
109// Someone may want to fill this out in the future.
110export type ActionConstructorOptions = number & { _: "ActionConstructorOptions" };
111
112export class HelpFormatter {}
113export class ArgumentDefaultsHelpFormatter {}
114export class RawDescriptionHelpFormatter {}
115export class RawTextHelpFormatter {}
116
117export interface ArgumentOptions {
118 action?: string | { new (options: ActionConstructorOptions): Action } | undefined;
119 option_strings?: string[] | undefined;
120 dest?: string | undefined;
121 nargs?: string | number | undefined;
122 const?: any;
123 default?: any;
124 // type may be a string (primitive) or a Function (constructor)
125 type?: string | Function | undefined; // tslint:disable-line:ban-types
126 choices?: string | string[] | undefined;
127 required?: boolean | undefined;
128 help?: string | undefined;
129 metavar?: string | string[] | undefined;
130 version?: string | undefined;
131}
132
133export class BooleanOptionalAction extends Action {
134 call(parser: ArgumentParser, namespace: Namespace, values: string | string[], optionString: string | null): void;
135}
136
137export const SUPPRESS: string;
138export const OPTIONAL: string;
139export const ZERO_OR_MORE: string;
140export const ONE_OR_MORE: string;
141export const REMAINDER: string;
142export const PARSER: string;