1 | import Option from "./option";
|
2 | import Argument from "./argument";
|
3 | export default class Command<Opt, Arg> {
|
4 | /**
|
5 | * @private
|
6 | */
|
7 | _description?: string;
|
8 | /**
|
9 | * @private
|
10 | */
|
11 | _usage?: string;
|
12 | /**
|
13 | * @private
|
14 | */
|
15 | _help: Option;
|
16 | /**
|
17 | * @private
|
18 | */
|
19 | _version?: Option;
|
20 | /**
|
21 | * @private
|
22 | */
|
23 | _versionStr?: string;
|
24 | /**
|
25 | * @private
|
26 | */
|
27 | _action: (opts: Opt, args: Arg, rest: string[]) => any;
|
28 | /**
|
29 | * e.g. -abc --foo bar
|
30 | * @private
|
31 | */
|
32 | _rawArgs?: string[];
|
33 | /**
|
34 | * e.g. -a -b -c --foo bar
|
35 | * @private
|
36 | */
|
37 | _args?: string[];
|
38 | /**
|
39 | * e.g. bar
|
40 | * @private
|
41 | */
|
42 | _rest: string[];
|
43 | /**
|
44 | * @private
|
45 | */
|
46 | _allowUnknownOption?: boolean;
|
47 | /**
|
48 | * parent command.
|
49 | */
|
50 | parent?: Command<any, any>;
|
51 | /**
|
52 | * name of this command.
|
53 | */
|
54 | name: string;
|
55 | /**
|
56 | * e.g.
|
57 | * ```
|
58 | * git -p clone git@github.com:vvakame/dtsm.git
|
59 | * ↑ this!
|
60 | * ```
|
61 | * @type {Array}
|
62 | */
|
63 | options: Option[];
|
64 | /**
|
65 | * e.g.
|
66 | * ```
|
67 | * git -p clone git@github.com:vvakame/dtsm.git
|
68 | * ↑ this!
|
69 | * ```
|
70 | * @type {Array}
|
71 | */
|
72 | subCommands: Command<any, any>[];
|
73 | /**
|
74 | * e.g.
|
75 | * ```
|
76 | * git -p clone git@github.com:vvakame/dtsm.git
|
77 | * ↑ this!
|
78 | * ```
|
79 | * @type {Array}
|
80 | */
|
81 | args: Argument[];
|
82 | /**
|
83 | * parsed option values.
|
84 | * @type {any}
|
85 | */
|
86 | parsedOpts: Opt;
|
87 | /**
|
88 | * parsed option arguments.
|
89 | * @type {any}
|
90 | */
|
91 | parsedArgs: Arg;
|
92 | /**
|
93 | * unknown options.
|
94 | * @type {Array}
|
95 | */
|
96 | unknownOptions: string[];
|
97 | /**
|
98 | * class of command.
|
99 | * ```
|
100 | * cmd -v sub --path foo/bar buzz.txt
|
101 | * ↑ this one!
|
102 | * ```
|
103 | * @param name name and flags pass flags pass 'foo'(sub command) or 'foo <bar>'(sub command & required argument) or 'foo [bar]'(sub command & optional argument) or 'foo <bar...>'(sub command & required variadic argument) or 'foo [bar...]'(sub command & optional variadic argument).
|
104 | * @class
|
105 | */
|
106 | constructor(name: string);
|
107 | /**
|
108 | * set description for this command.
|
109 | * @param desc
|
110 | * @returns {Command}
|
111 | *
|
112 | */
|
113 | description(desc: string): Command<Opt, Arg>;
|
114 | /**
|
115 | * set usage for this command.
|
116 | * @param usage
|
117 | * @returns {Command}
|
118 | * @method
|
119 | */
|
120 | usage(usage: string): Command<Opt, Arg>;
|
121 | /**
|
122 | * add option for this command.
|
123 | * see {@link Option}.
|
124 | * @param flags
|
125 | * @param description
|
126 | * @param defaultValue
|
127 | * @returns {Command}
|
128 | */
|
129 | option(flags: string, description?: string, defaultValue?: any): Command<Opt, Arg>;
|
130 | /**
|
131 | * allow unknown option.
|
132 | * by default, An error occured if unknown option is included.
|
133 | * @param flag
|
134 | * @returns {Command}
|
135 | */
|
136 | allowUnknownOption(flag?: boolean): Command<Opt, Arg>;
|
137 | /**
|
138 | * add action at this command selected.
|
139 | * @param fn
|
140 | * @returns {Command}
|
141 | */
|
142 | action(fn: (opts: Opt, args: Arg, rest: string[]) => any): Command<Opt, Arg>;
|
143 | /**
|
144 | * create sub command.
|
145 | * @param name
|
146 | * @returns {Command<Opt2, Arg2>} new command instance
|
147 | */
|
148 | subCommand<Opt2, Arg2>(name: string): Command<Opt2, Arg2>;
|
149 | /**
|
150 | * check arg is matches this command.
|
151 | * @param arg
|
152 | * @returns {boolean}
|
153 | */
|
154 | is(arg: string): boolean;
|
155 | /**
|
156 | * add help this option.
|
157 | * in general case, use default help option.
|
158 | * @param flags
|
159 | * @param description
|
160 | * @returns {Command}
|
161 | */
|
162 | help(flags: string, description: string): this;
|
163 | /**
|
164 | * add show version option to this command.
|
165 | * @param version
|
166 | * @param flags
|
167 | * @param description
|
168 | * @returns {Command}
|
169 | */
|
170 | version(version: string, flags: string, description?: string): Command<Opt, Arg>;
|
171 | /**
|
172 | * exec action of command.
|
173 | * this method MUST call after parse process.
|
174 | * @returns {Promise<{}>}
|
175 | */
|
176 | exec(): Promise<{}>;
|
177 | /**
|
178 | * parse argv.
|
179 | * @param argv
|
180 | * @returns {Promise<{}>}
|
181 | */
|
182 | parse(argv: string[]): Promise<{}>;
|
183 | /**
|
184 | * @returns {*}
|
185 | * @private
|
186 | */
|
187 | _getAncestorsAndMe(): Command<any, any>[];
|
188 | /**
|
189 | * @param args
|
190 | * @returns {string[]}
|
191 | * @private
|
192 | */
|
193 | _parseRawArgs(args: string[]): string[];
|
194 | /**
|
195 | * @param rest
|
196 | * @returns {boolean}
|
197 | * @private
|
198 | */
|
199 | _matchSubCommand(rest: string[]): boolean;
|
200 | /**
|
201 | * @param args
|
202 | * @returns {string[]}
|
203 | * @private
|
204 | */
|
205 | _parseOptions(args: string[]): string[];
|
206 | /**
|
207 | * @param rest
|
208 | * @returns {string[]}
|
209 | * @private
|
210 | */
|
211 | _parseArgs(rest: string[]): string[];
|
212 | /**
|
213 | * @param args
|
214 | * @returns {string[]}
|
215 | * @private
|
216 | */
|
217 | _normalize(args: string[]): string[];
|
218 | /**
|
219 | * generate help text.
|
220 | * @returns {string}
|
221 | */
|
222 | helpText(): string;
|
223 | }
|