UNPKG

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