1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | import { Configuration, DetailedArguments } from "yargs-parser";
|
16 |
|
17 | declare namespace yargs {
|
18 | type BuilderCallback<T, R> =
|
19 | | ((args: Argv<T>) => PromiseLike<Argv<R>>)
|
20 | | ((args: Argv<T>) => Argv<R>)
|
21 | | ((args: Argv<T>) => void);
|
22 |
|
23 | type ParserConfigurationOptions = Configuration & {
|
24 | /** Sort commands alphabetically. Default is `false` */
|
25 | "sort-commands": boolean;
|
26 | };
|
27 |
|
28 | /**
|
29 | * The type parameter `T` is the expected shape of the parsed options.
|
30 | * `Arguments<T>` is those options plus `_` and `$0`, and an indexer falling
|
31 | * back to `unknown` for unknown options.
|
32 | *
|
33 | * For the return type / `argv` property, we create a mapped type over
|
34 | * `Arguments<T>` to simplify the inferred type signature in client code.
|
35 | */
|
36 | interface Argv<T = {}> {
|
37 | (): { [key in keyof Arguments<T>]: Arguments<T>[key] };
|
38 | (args: ReadonlyArray<string>, cwd?: string): Argv<T>;
|
39 |
|
40 | /**
|
41 | * Set key names as equivalent such that updates to a key will propagate to aliases and vice-versa.
|
42 | *
|
43 | * Optionally `.alias()` can take an object that maps keys to aliases.
|
44 | * Each key of this object should be the canonical version of the option, and each value should be a string or an array of strings.
|
45 | */
|
46 | // Aliases for previously declared options can inherit the types of those options.
|
47 | alias<K1 extends keyof T, K2 extends string>(
|
48 | shortName: K1,
|
49 | longName: K2 | ReadonlyArray<K2>,
|
50 | ): Argv<T & { [key in K2]: T[K1] }>;
|
51 | alias<K1 extends keyof T, K2 extends string>(
|
52 | shortName: K2,
|
53 | longName: K1 | ReadonlyArray<K1>,
|
54 | ): Argv<T & { [key in K2]: T[K1] }>;
|
55 | alias(shortName: string | ReadonlyArray<string>, longName: string | ReadonlyArray<string>): Argv<T>;
|
56 | alias(aliases: { [shortName: string]: string | ReadonlyArray<string> }): Argv<T>;
|
57 |
|
58 | /**
|
59 | * Get the arguments as a plain old object.
|
60 | *
|
61 | * Arguments without a corresponding flag show up in the `argv._` array.
|
62 | *
|
63 | * The script name or node command is available at `argv.$0` similarly to how `$0` works in bash or perl.
|
64 | *
|
65 | * If `yargs` is executed in an environment that embeds node and there's no script name (e.g. Electron or nw.js),
|
66 | * it will ignore the first parameter since it expects it to be the script name. In order to override
|
67 | * this behavior, use `.parse(process.argv.slice(1))` instead of .argv and the first parameter won't be ignored.
|
68 | */
|
69 | argv: { [key in keyof Arguments<T>]: Arguments<T>[key] };
|
70 |
|
71 | /**
|
72 | * Tell the parser to interpret `key` as an array.
|
73 | * If `.array('foo')` is set, `--foo foo bar` will be parsed as `['foo', 'bar']` rather than as `'foo'`.
|
74 | * Also, if you use the option multiple times all the values will be flattened in one array so `--foo foo --foo bar` will be parsed as `['foo', 'bar']`
|
75 | *
|
76 | * When the option is used with a positional, use `--` to tell `yargs` to stop adding values to the array.
|
77 | */
|
78 | array<K extends keyof T>(key: K | ReadonlyArray<K>): Argv<Omit<T, K> & { [key in K]: ToArray<T[key]> }>;
|
79 | array<K extends string>(
|
80 | key: K | ReadonlyArray<K>,
|
81 | ): Argv<T & { [key in K]: Array<string | number> | undefined }>;
|
82 |
|
83 | /**
|
84 | * Interpret `key` as a boolean. If a non-flag option follows `key` in `process.argv`, that string won't get set as the value of `key`.
|
85 | *
|
86 | * `key` will default to `false`, unless a `default(key, undefined)` is explicitly set.
|
87 | *
|
88 | * If `key` is an array, interpret all the elements as booleans.
|
89 | */
|
90 | boolean<K extends keyof T>(key: K | ReadonlyArray<K>): Argv<Omit<T, K> & { [key in K]: boolean | undefined }>;
|
91 | boolean<K extends string>(key: K | ReadonlyArray<K>): Argv<T & { [key in K]: boolean | undefined }>;
|
92 |
|
93 | /**
|
94 | * Check that certain conditions are met in the provided arguments.
|
95 | * @param func Called with two arguments, the parsed `argv` hash and an array of options and their aliases.
|
96 | * If `func` throws or returns a non-truthy value, show the thrown error, usage information, and exit.
|
97 | * @param global Indicates whether `check()` should be enabled both at the top-level and for each sub-command.
|
98 | */
|
99 | check(func: (argv: Arguments<T>, aliases: { [alias: string]: string }) => any, global?: boolean): Argv<T>;
|
100 |
|
101 | /**
|
102 | * Limit valid values for key to a predefined set of choices, given as an array or as an individual value.
|
103 | * If this method is called multiple times, all enumerated values will be merged together.
|
104 | * Choices are generally strings or numbers, and value matching is case-sensitive.
|
105 | *
|
106 | * Optionally `.choices()` can take an object that maps multiple keys to their choices.
|
107 | *
|
108 | * Choices can also be specified as choices in the object given to `option()`.
|
109 | */
|
110 | choices<K extends keyof T, C extends ReadonlyArray<any>>(
|
111 | key: K,
|
112 | values: C,
|
113 | ): Argv<Omit<T, K> & { [key in K]: C[number] | undefined }>;
|
114 | choices<K extends string, C extends ReadonlyArray<any>>(
|
115 | key: K,
|
116 | values: C,
|
117 | ): Argv<T & { [key in K]: C[number] | undefined }>;
|
118 | choices<C extends { [key: string]: ReadonlyArray<any> }>(
|
119 | choices: C,
|
120 | ): Argv<Omit<T, keyof C> & { [key in keyof C]: C[key][number] | undefined }>;
|
121 |
|
122 | /**
|
123 | * Provide a synchronous function to coerce or transform the value(s) given on the command line for `key`.
|
124 | *
|
125 | * The coercion function should accept one argument, representing the parsed value from the command line, and should return a new value or throw an error.
|
126 | * The returned value will be used as the value for `key` (or one of its aliases) in `argv`.
|
127 | *
|
128 | * If the function throws, the error will be treated as a validation failure, delegating to either a custom `.fail()` handler or printing the error message in the console.
|
129 | *
|
130 | * Coercion will be applied to a value after all other modifications, such as `.normalize()`.
|
131 | *
|
132 | * Optionally `.coerce()` can take an object that maps several keys to their respective coercion function.
|
133 | *
|
134 | * You can also map the same function to several keys at one time. Just pass an array of keys as the first argument to `.coerce()`.
|
135 | *
|
136 | * If you are using dot-notion or arrays, .e.g., `user.email` and `user.password`, coercion will be applied to the final object that has been parsed
|
137 | */
|
138 | coerce<K extends keyof T, V>(
|
139 | key: K | ReadonlyArray<K>,
|
140 | func: (arg: any) => V,
|
141 | ): Argv<Omit<T, K> & { [key in K]: V | undefined }>;
|
142 | coerce<K extends string, V>(
|
143 | key: K | ReadonlyArray<K>,
|
144 | func: (arg: any) => V,
|
145 | ): Argv<T & { [key in K]: V | undefined }>;
|
146 | coerce<O extends { [key: string]: (arg: any) => any }>(
|
147 | opts: O,
|
148 | ): Argv<Omit<T, keyof O> & { [key in keyof O]: ReturnType<O[key]> | undefined }>;
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | command<U = T>(
|
162 | command: string | ReadonlyArray<string>,
|
163 | description: string,
|
164 | builder?: BuilderCallback<T, U>,
|
165 | handler?: (args: Arguments<U>) => void,
|
166 | middlewares?: Array<MiddlewareFunction<U>>,
|
167 | deprecated?: boolean | string,
|
168 | ): Argv<U>;
|
169 | command<O extends { [key: string]: Options }>(
|
170 | command: string | ReadonlyArray<string>,
|
171 | description: string,
|
172 | builder?: O,
|
173 | handler?: (args: Arguments<InferredOptionTypes<O>>) => void,
|
174 | middlewares?: Array<MiddlewareFunction<O>>,
|
175 | deprecated?: boolean | string,
|
176 | ): Argv<T>;
|
177 | command<U>(command: string | ReadonlyArray<string>, description: string, module: CommandModule<T, U>): Argv<U>;
|
178 | command<U = T>(
|
179 | command: string | ReadonlyArray<string>,
|
180 | showInHelp: false,
|
181 | builder?: BuilderCallback<T, U>,
|
182 | handler?: (args: Arguments<U>) => void,
|
183 | middlewares?: Array<MiddlewareFunction<U>>,
|
184 | deprecated?: boolean | string,
|
185 | ): Argv<T>;
|
186 | command<O extends { [key: string]: Options }>(
|
187 | command: string | ReadonlyArray<string>,
|
188 | showInHelp: false,
|
189 | builder?: O,
|
190 | handler?: (args: Arguments<InferredOptionTypes<O>>) => void,
|
191 | ): Argv<T>;
|
192 | command<U>(command: string | ReadonlyArray<string>, showInHelp: false, module: CommandModule<T, U>): Argv<U>;
|
193 | command<U>(module: CommandModule<T, U>): Argv<U>;
|
194 |
|
195 | // Advanced API
|
196 | /** Apply command modules from a directory relative to the module calling this method. */
|
197 | commandDir(dir: string, opts?: RequireDirectoryOptions): Argv<T>;
|
198 |
|
199 | /**
|
200 | * Enable bash/zsh-completion shortcuts for commands and options.
|
201 | *
|
202 | * If invoked without parameters, `.completion()` will make completion the command to output the completion script.
|
203 | *
|
204 | * @param [cmd] When present in `argv._`, will result in the `.bashrc` or `.zshrc` completion script being outputted.
|
205 | * To enable bash/zsh completions, concat the generated script to your `.bashrc` or `.bash_profile` (or `.zshrc` for zsh).
|
206 | * @param [description] Provide a description in your usage instructions for the command that generates the completion scripts.
|
207 | * @param [func] Rather than relying on yargs' default completion functionality, which shiver me timbers is pretty awesome, you can provide your own completion method.
|
208 | */
|
209 | completion(): Argv<T>;
|
210 | completion(cmd: string, func?: AsyncCompletionFunction): Argv<T>;
|
211 | completion(cmd: string, func?: SyncCompletionFunction): Argv<T>;
|
212 | completion(cmd: string, func?: PromiseCompletionFunction): Argv<T>;
|
213 | completion(cmd: string, description?: string | false, func?: AsyncCompletionFunction): Argv<T>;
|
214 | completion(cmd: string, description?: string | false, func?: SyncCompletionFunction): Argv<T>;
|
215 | completion(cmd: string, description?: string | false, func?: PromiseCompletionFunction): Argv<T>;
|
216 |
|
217 | /**
|
218 | * Tells the parser that if the option specified by `key` is passed in, it should be interpreted as a path to a JSON config file.
|
219 | * The file is loaded and parsed, and its properties are set as arguments.
|
220 | * Because the file is loaded using Node's require(), the filename MUST end in `.json` to be interpreted correctly.
|
221 | *
|
222 | * If invoked without parameters, `.config()` will make --config the option to pass the JSON config file.
|
223 | *
|
224 | * @param [description] Provided to customize the config (`key`) option in the usage string.
|
225 | * @param [explicitConfigurationObject] An explicit configuration `object`
|
226 | */
|
227 | config(): Argv<T>;
|
228 | config(
|
229 | key: string | ReadonlyArray<string>,
|
230 | description?: string,
|
231 | parseFn?: (configPath: string) => object,
|
232 | ): Argv<T>;
|
233 | config(key: string | ReadonlyArray<string>, parseFn: (configPath: string) => object): Argv<T>;
|
234 | config(explicitConfigurationObject: object): Argv<T>;
|
235 |
|
236 | /**
|
237 | * Given the key `x` is set, the key `y` must not be set. `y` can either be a single string or an array of argument names that `x` conflicts with.
|
238 | *
|
239 | * Optionally `.conflicts()` can accept an object specifying multiple conflicting keys.
|
240 | */
|
241 | conflicts(key: string, value: string | ReadonlyArray<string>): Argv<T>;
|
242 | conflicts(conflicts: { [key: string]: string | ReadonlyArray<string> }): Argv<T>;
|
243 |
|
244 | |
245 |
|
246 |
|
247 | count<K extends keyof T>(key: K | ReadonlyArray<K>): Argv<Omit<T, K> & { [key in K]: number }>;
|
248 | count<K extends string>(key: K | ReadonlyArray<K>): Argv<T & { [key in K]: number }>;
|
249 |
|
250 | |
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | default<K extends keyof T, V>(key: K, value: V, description?: string): Argv<Omit<T, K> & { [key in K]: V }>;
|
260 | default<K extends string, V>(key: K, value: V, description?: string): Argv<T & { [key in K]: V }>;
|
261 | default<D extends { [key: string]: any }>(defaults: D, description?: string): Argv<Omit<T, keyof D> & D>;
|
262 |
|
263 | |
264 |
|
265 |
|
266 |
|
267 | demand<K extends keyof T>(key: K | ReadonlyArray<K>, msg?: string | true): Argv<Defined<T, K>>;
|
268 | demand<K extends string>(key: K | ReadonlyArray<K>, msg?: string | true): Argv<T & { [key in K]: unknown }>;
|
269 | demand(key: string | ReadonlyArray<string>, required?: boolean): Argv<T>;
|
270 | demand(positionals: number, msg: string): Argv<T>;
|
271 | demand(positionals: number, required?: boolean): Argv<T>;
|
272 | demand(positionals: number, max: number, msg?: string): Argv<T>;
|
273 |
|
274 | |
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 | demandOption<K extends keyof T>(key: K | ReadonlyArray<K>, msg?: string | true): Argv<Defined<T, K>>;
|
281 | demandOption<K extends string>(
|
282 | key: K | ReadonlyArray<K>,
|
283 | msg?: string | true,
|
284 | ): Argv<T & { [key in K]: unknown }>;
|
285 | demandOption(key: string | ReadonlyArray<string>, demand?: boolean): Argv<T>;
|
286 |
|
287 | |
288 |
|
289 |
|
290 |
|
291 | demandCommand(): Argv<T>;
|
292 | demandCommand(min: number, minMsg?: string): Argv<T>;
|
293 | demandCommand(min: number, max?: number, minMsg?: string, maxMsg?: string): Argv<T>;
|
294 |
|
295 | |
296 |
|
297 |
|
298 | deprecateOption(option: string, msg?: string): Argv<T>;
|
299 |
|
300 | |
301 |
|
302 |
|
303 |
|
304 |
|
305 | describe(key: string | ReadonlyArray<string>, description: string): Argv<T>;
|
306 | describe(descriptions: { [key: string]: string }): Argv<T>;
|
307 |
|
308 |
|
309 | detectLocale(detect: boolean): Argv<T>;
|
310 |
|
311 | |
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | env(): Argv<T>;
|
327 | env(prefix: string): Argv<T>;
|
328 | env(enable: boolean): Argv<T>;
|
329 |
|
330 |
|
331 | epilog(msg: string): Argv<T>;
|
332 |
|
333 | epilogue(msg: string): Argv<T>;
|
334 |
|
335 | |
336 |
|
337 |
|
338 |
|
339 |
|
340 | example(command: string, description: string): Argv<T>;
|
341 | example(command: ReadonlyArray<[string, string?]>): Argv<T>;
|
342 |
|
343 |
|
344 | exit(code: number, err: Error): void;
|
345 |
|
346 | |
347 |
|
348 |
|
349 |
|
350 | exitProcess(enabled: boolean): Argv<T>;
|
351 |
|
352 | |
353 |
|
354 |
|
355 |
|
356 | fail(func: (msg: string, err: Error, yargs: Argv<T>) => any): Argv<T>;
|
357 |
|
358 | |
359 |
|
360 |
|
361 |
|
362 |
|
363 | getCompletion(args: ReadonlyArray<string>, done: (completions: ReadonlyArray<string>) => void): Argv<T>;
|
364 |
|
365 | |
366 |
|
367 |
|
368 |
|
369 |
|
370 | global(key: string | ReadonlyArray<string>): Argv<T>;
|
371 |
|
372 |
|
373 | group(key: string | ReadonlyArray<string>, groupName: string): Argv<T>;
|
374 |
|
375 |
|
376 | hide(key: string): Argv<T>;
|
377 |
|
378 | |
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 | help(): Argv<T>;
|
391 | help(enableExplicit: boolean): Argv<T>;
|
392 | help(option: string, enableExplicit: boolean): Argv<T>;
|
393 | help(option: string, description?: string, enableExplicit?: boolean): Argv<T>;
|
394 |
|
395 | |
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 | implies(key: string, value: string | ReadonlyArray<string>): Argv<T>;
|
402 | implies(implies: { [key: string]: string | ReadonlyArray<string> }): Argv<T>;
|
403 |
|
404 | |
405 |
|
406 |
|
407 |
|
408 |
|
409 | locale(): string;
|
410 | |
411 |
|
412 |
|
413 |
|
414 | locale(loc: string): Argv<T>;
|
415 |
|
416 | |
417 |
|
418 |
|
419 |
|
420 |
|
421 | middleware(
|
422 | callbacks: MiddlewareFunction<T> | ReadonlyArray<MiddlewareFunction<T>>,
|
423 | applyBeforeValidation?: boolean,
|
424 | ): Argv<T>;
|
425 |
|
426 | |
427 |
|
428 |
|
429 |
|
430 |
|
431 | nargs(key: string, count: number): Argv<T>;
|
432 | nargs(nargs: { [key: string]: number }): Argv<T>;
|
433 |
|
434 |
|
435 | normalize<K extends keyof T>(key: K | ReadonlyArray<K>): Argv<Omit<T, K> & { [key in K]: ToString<T[key]> }>;
|
436 | normalize<K extends string>(key: K | ReadonlyArray<K>): Argv<T & { [key in K]: string | undefined }>;
|
437 |
|
438 | |
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 | number<K extends keyof T>(key: K | ReadonlyArray<K>): Argv<Omit<T, K> & { [key in K]: ToNumber<T[key]> }>;
|
450 | number<K extends string>(key: K | ReadonlyArray<K>): Argv<T & { [key in K]: number | undefined }>;
|
451 |
|
452 | |
453 |
|
454 |
|
455 |
|
456 | onFinishCommand(func: (result: any) => void): Argv<T>;
|
457 |
|
458 | |
459 |
|
460 |
|
461 |
|
462 | option<K extends keyof T, O extends Options>(
|
463 | key: K,
|
464 | options: O,
|
465 | ): Argv<Omit<T, K> & { [key in K]: InferredOptionType<O> }>;
|
466 | option<K extends string, O extends Options>(
|
467 | key: K,
|
468 | options: O,
|
469 | ): Argv<T & { [key in K]: InferredOptionType<O> }>;
|
470 | option<O extends { [key: string]: Options }>(options: O): Argv<Omit<T, keyof O> & InferredOptionTypes<O>>;
|
471 |
|
472 | |
473 |
|
474 |
|
475 |
|
476 | options<K extends keyof T, O extends Options>(
|
477 | key: K,
|
478 | options: O,
|
479 | ): Argv<Omit<T, K> & { [key in K]: InferredOptionType<O> }>;
|
480 | options<K extends string, O extends Options>(
|
481 | key: K,
|
482 | options: O,
|
483 | ): Argv<T & { [key in K]: InferredOptionType<O> }>;
|
484 | options<O extends { [key: string]: Options }>(options: O): Argv<Omit<T, keyof O> & InferredOptionTypes<O>>;
|
485 |
|
486 | |
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 | parse(): { [key in keyof Arguments<T>]: Arguments<T>[key] };
|
493 | parse(
|
494 | arg: string | ReadonlyArray<string>,
|
495 | context?: object,
|
496 | parseCallback?: ParseCallback<T>,
|
497 | ): { [key in keyof Arguments<T>]: Arguments<T>[key] };
|
498 |
|
499 | |
500 |
|
501 |
|
502 |
|
503 |
|
504 | parsed: DetailedArguments | false;
|
505 |
|
506 |
|
507 | parserConfiguration(configuration: Partial<ParserConfigurationOptions>): Argv<T>;
|
508 |
|
509 | |
510 |
|
511 |
|
512 |
|
513 | pkgConf(key: string | ReadonlyArray<string>, cwd?: string): Argv<T>;
|
514 |
|
515 | |
516 |
|
517 |
|
518 |
|
519 | positional<K extends keyof T, O extends PositionalOptions>(
|
520 | key: K,
|
521 | opt: O,
|
522 | ): Argv<Omit<T, K> & { [key in K]: InferredOptionType<O> }>;
|
523 | positional<K extends string, O extends PositionalOptions>(
|
524 | key: K,
|
525 | opt: O,
|
526 | ): Argv<T & { [key in K]: InferredOptionType<O> }>;
|
527 |
|
528 |
|
529 | recommendCommands(): Argv<T>;
|
530 |
|
531 | |
532 |
|
533 |
|
534 |
|
535 | require<K extends keyof T>(key: K | ReadonlyArray<K>, msg?: string | true): Argv<Defined<T, K>>;
|
536 | require(key: string, msg: string): Argv<T>;
|
537 | require(key: string, required: boolean): Argv<T>;
|
538 | require(keys: ReadonlyArray<number>, msg: string): Argv<T>;
|
539 | require(keys: ReadonlyArray<number>, required: boolean): Argv<T>;
|
540 | require(positionals: number, required: boolean): Argv<T>;
|
541 | require(positionals: number, msg: string): Argv<T>;
|
542 |
|
543 | |
544 |
|
545 |
|
546 |
|
547 | required<K extends keyof T>(key: K | ReadonlyArray<K>, msg?: string | true): Argv<Defined<T, K>>;
|
548 | required(key: string, msg: string): Argv<T>;
|
549 | required(key: string, required: boolean): Argv<T>;
|
550 | required(keys: ReadonlyArray<number>, msg: string): Argv<T>;
|
551 | required(keys: ReadonlyArray<number>, required: boolean): Argv<T>;
|
552 | required(positionals: number, required: boolean): Argv<T>;
|
553 | required(positionals: number, msg: string): Argv<T>;
|
554 |
|
555 | requiresArg(key: string | ReadonlyArray<string>): Argv<T>;
|
556 |
|
557 | |
558 |
|
559 |
|
560 |
|
561 | reset(): Argv<T>;
|
562 |
|
563 |
|
564 | scriptName($0: string): Argv<T>;
|
565 |
|
566 | |
567 |
|
568 |
|
569 |
|
570 | showCompletionScript(): Argv<T>;
|
571 |
|
572 | |
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 | showHidden(option?: string | boolean): Argv<T>;
|
579 | showHidden(option: string, description?: string): Argv<T>;
|
580 |
|
581 | |
582 |
|
583 |
|
584 |
|
585 | showHelp(consoleLevel?: string): Argv<T>;
|
586 |
|
587 | |
588 |
|
589 |
|
590 |
|
591 | showHelp(printCallback: (s: string) => void): Argv<T>;
|
592 |
|
593 | |
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 | showHelpOnFail(enable: boolean, message?: string): Argv<T>;
|
600 |
|
601 |
|
602 | skipValidation(key: string | ReadonlyArray<string>): Argv<T>;
|
603 |
|
604 | |
605 |
|
606 |
|
607 |
|
608 |
|
609 | strict(): Argv<T>;
|
610 | strict(enabled: boolean): Argv<T>;
|
611 |
|
612 | |
613 |
|
614 |
|
615 |
|
616 |
|
617 | strictCommands(): Argv<T>;
|
618 | strictCommands(enabled: boolean): Argv<T>;
|
619 |
|
620 | |
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 | string<K extends keyof T>(key: K | ReadonlyArray<K>): Argv<Omit<T, K> & { [key in K]: ToString<T[key]> }>;
|
628 | string<K extends string>(key: K | ReadonlyArray<K>): Argv<T & { [key in K]: string | undefined }>;
|
629 |
|
630 |
|
631 | terminalWidth(): number;
|
632 |
|
633 | updateLocale(obj: { [key: string]: string }): Argv<T>;
|
634 |
|
635 | |
636 |
|
637 |
|
638 |
|
639 |
|
640 | updateStrings(obj: { [key: string]: string }): Argv<T>;
|
641 |
|
642 | |
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 | usage(message: string): Argv<T>;
|
651 | usage<U>(
|
652 | command: string | ReadonlyArray<string>,
|
653 | description: string,
|
654 | builder?: (args: Argv<T>) => Argv<U>,
|
655 | handler?: (args: Arguments<U>) => void,
|
656 | ): Argv<T>;
|
657 | usage<U>(
|
658 | command: string | ReadonlyArray<string>,
|
659 | showInHelp: boolean,
|
660 | builder?: (args: Argv<T>) => Argv<U>,
|
661 | handler?: (args: Arguments<U>) => void,
|
662 | ): Argv<T>;
|
663 | usage<O extends { [key: string]: Options }>(
|
664 | command: string | ReadonlyArray<string>,
|
665 | description: string,
|
666 | builder?: O,
|
667 | handler?: (args: Arguments<InferredOptionTypes<O>>) => void,
|
668 | ): Argv<T>;
|
669 | usage<O extends { [key: string]: Options }>(
|
670 | command: string | ReadonlyArray<string>,
|
671 | showInHelp: boolean,
|
672 | builder?: O,
|
673 | handler?: (args: Arguments<InferredOptionTypes<O>>) => void,
|
674 | ): Argv<T>;
|
675 |
|
676 | |
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 | version(): Argv<T>;
|
685 | version(version: string): Argv<T>;
|
686 | version(enable: boolean): Argv<T>;
|
687 | version(optionKey: string, version: string): Argv<T>;
|
688 | version(optionKey: string, description: string, version: string): Argv<T>;
|
689 |
|
690 | |
691 |
|
692 |
|
693 |
|
694 |
|
695 |
|
696 | wrap(columns: number | null): Argv<T>;
|
697 | }
|
698 |
|
699 | type Arguments<T = {}> = T & {
|
700 |
|
701 | _: Array<string | number>;
|
702 |
|
703 | $0: string;
|
704 |
|
705 | [argName: string]: unknown;
|
706 | };
|
707 |
|
708 | interface RequireDirectoryOptions {
|
709 |
|
710 | recurse?: boolean | undefined;
|
711 |
|
712 | extensions?: ReadonlyArray<string> | undefined;
|
713 | |
714 |
|
715 |
|
716 |
|
717 |
|
718 | visit?: ((commandObject: any, pathToFile?: string, filename?: string) => any) | undefined;
|
719 | /** Whitelist certain modules */
|
720 | include?: RegExp | ((pathToFile: string) => boolean) | undefined;
|
721 | /** Blacklist certain modules. */
|
722 | exclude?: RegExp | ((pathToFile: string) => boolean) | undefined;
|
723 | }
|
724 |
|
725 | interface Options {
|
726 | /** string or array of strings, alias(es) for the canonical option key, see `alias()` */
|
727 | alias?: string | ReadonlyArray<string> | undefined;
|
728 | /** boolean, interpret option as an array, see `array()` */
|
729 | array?: boolean | undefined;
|
730 | /** boolean, interpret option as a boolean flag, see `boolean()` */
|
731 | boolean?: boolean | undefined;
|
732 | /** value or array of values, limit valid option arguments to a predefined set, see `choices()` */
|
733 | choices?: Choices | undefined;
|
734 | /** function, coerce or transform parsed command line values into another value, see `coerce()` */
|
735 | coerce?: ((arg: any) => any) | undefined;
|
736 | /** boolean, interpret option as a path to a JSON config file, see `config()` */
|
737 | config?: boolean | undefined;
|
738 | /** function, provide a custom config parsing function, see `config()` */
|
739 | configParser?: ((configPath: string) => object) | undefined;
|
740 | /** string or object, require certain keys not to be set, see `conflicts()` */
|
741 | conflicts?: string | ReadonlyArray<string> | { [key: string]: string | ReadonlyArray<string> } | undefined;
|
742 | /** boolean, interpret option as a count of boolean flags, see `count()` */
|
743 | count?: boolean | undefined;
|
744 | /** value, set a default value for the option, see `default()` */
|
745 | default?: any;
|
746 | /** string, use this description for the default value in help content, see `default()` */
|
747 | defaultDescription?: string | undefined;
|
748 | /**
|
749 | * @deprecated since version 6.6.0
|
750 | * Use 'demandOption' instead
|
751 | */
|
752 | demand?: boolean | string | undefined;
|
753 | /** boolean or string, mark the argument as deprecated, see `deprecateOption()` */
|
754 | deprecate?: boolean | string | undefined;
|
755 | /** boolean or string, mark the argument as deprecated, see `deprecateOption()` */
|
756 | deprecated?: boolean | string | undefined;
|
757 | /** boolean or string, demand the option be given, with optional error message, see `demandOption()` */
|
758 | demandOption?: boolean | string | undefined;
|
759 | /** string, the option description for help content, see `describe()` */
|
760 | desc?: string | undefined;
|
761 | /** string, the option description for help content, see `describe()` */
|
762 | describe?: string | undefined;
|
763 | /** string, the option description for help content, see `describe()` */
|
764 | description?: string | undefined;
|
765 | /** boolean, indicate that this key should not be reset when a command is invoked, see `global()` */
|
766 | global?: boolean | undefined;
|
767 | /** string, when displaying usage instructions place the option under an alternative group heading, see `group()` */
|
768 | group?: string | undefined;
|
769 | /** don't display option in help output. */
|
770 | hidden?: boolean | undefined;
|
771 | /** string or object, require certain keys to be set, see `implies()` */
|
772 | implies?: string | ReadonlyArray<string> | { [key: string]: string | ReadonlyArray<string> } | undefined;
|
773 | /** number, specify how many arguments should be consumed for the option, see `nargs()` */
|
774 | nargs?: number | undefined;
|
775 | /** boolean, apply path.normalize() to the option, see `normalize()` */
|
776 | normalize?: boolean | undefined;
|
777 | /** boolean, interpret option as a number, `number()` */
|
778 | number?: boolean | undefined;
|
779 | /**
|
780 | * @deprecated since version 6.6.0
|
781 | * Use 'demandOption' instead
|
782 | */
|
783 | require?: boolean | string | undefined;
|
784 | /**
|
785 | * @deprecated since version 6.6.0
|
786 | * Use 'demandOption' instead
|
787 | */
|
788 | required?: boolean | string | undefined;
|
789 | /** boolean, require the option be specified with a value, see `requiresArg()` */
|
790 | requiresArg?: boolean | undefined;
|
791 | /** boolean, skips validation if the option is present, see `skipValidation()` */
|
792 | skipValidation?: boolean | undefined;
|
793 | /** boolean, interpret option as a string, see `string()` */
|
794 | string?: boolean | undefined;
|
795 | type?: "array" | "count" | PositionalOptionsType | undefined;
|
796 | }
|
797 |
|
798 | interface PositionalOptions {
|
799 | /** string or array of strings, see `alias()` */
|
800 | alias?: string | ReadonlyArray<string> | undefined;
|
801 | /** boolean, interpret option as an array, see `array()` */
|
802 | array?: boolean | undefined;
|
803 | /** value or array of values, limit valid option arguments to a predefined set, see `choices()` */
|
804 | choices?: Choices | undefined;
|
805 | /** function, coerce or transform parsed command line values into another value, see `coerce()` */
|
806 | coerce?: ((arg: any) => any) | undefined;
|
807 | /** string or object, require certain keys not to be set, see `conflicts()` */
|
808 | conflicts?: string | ReadonlyArray<string> | { [key: string]: string | ReadonlyArray<string> } | undefined;
|
809 | /** value, set a default value for the option, see `default()` */
|
810 | default?: any;
|
811 | /** boolean or string, demand the option be given, with optional error message, see `demandOption()` */
|
812 | demandOption?: boolean | string | undefined;
|
813 | /** string, the option description for help content, see `describe()` */
|
814 | desc?: string | undefined;
|
815 | /** string, the option description for help content, see `describe()` */
|
816 | describe?: string | undefined;
|
817 | /** string, the option description for help content, see `describe()` */
|
818 | description?: string | undefined;
|
819 | /** string or object, require certain keys to be set, see `implies()` */
|
820 | implies?: string | ReadonlyArray<string> | { [key: string]: string | ReadonlyArray<string> } | undefined;
|
821 | /** boolean, apply path.normalize() to the option, see normalize() */
|
822 | normalize?: boolean | undefined;
|
823 | type?: PositionalOptionsType | undefined;
|
824 | }
|
825 |
|
826 | /** Remove keys K in T */
|
827 | type Omit<T, K> = { [key in Exclude<keyof T, K>]: T[key] };
|
828 |
|
829 | /** Remove undefined as a possible value for keys K in T */
|
830 | type Defined<T, K extends keyof T> = Omit<T, K> & { [key in K]: Exclude<T[key], undefined> };
|
831 |
|
832 | /** Convert T to T[] and T | undefined to T[] | undefined */
|
833 | type ToArray<T> = Array<Exclude<T, undefined>> | Extract<T, undefined>;
|
834 |
|
835 | /** Gives string[] if T is an array type, otherwise string. Preserves | undefined. */
|
836 | type ToString<T> = (Exclude<T, undefined> extends any[] ? string[] : string) | Extract<T, undefined>;
|
837 |
|
838 | /** Gives number[] if T is an array type, otherwise number. Preserves | undefined. */
|
839 | type ToNumber<T> = (Exclude<T, undefined> extends any[] ? number[] : number) | Extract<T, undefined>;
|
840 |
|
841 | type InferredOptionType<O extends Options | PositionalOptions> = O extends
|
842 | { default: any; coerce: (arg: any) => infer T } ? T
|
843 | : O extends { default: infer D } ? D
|
844 | : O extends { type: "count" } ? number
|
845 | : O extends { count: true } ? number
|
846 | : O extends { required: string | true } ? RequiredOptionType<O>
|
847 | : O extends { require: string | true } ? RequiredOptionType<O>
|
848 | : O extends { demand: string | true } ? RequiredOptionType<O>
|
849 | : O extends { demandOption: string | true } ? RequiredOptionType<O>
|
850 | : RequiredOptionType<O> | undefined;
|
851 |
|
852 | type RequiredOptionType<O extends Options | PositionalOptions> = O extends { type: "array"; string: true }
|
853 | ? string[]
|
854 | : O extends { type: "array"; number: true } ? number[]
|
855 | : O extends { type: "array"; normalize: true } ? string[]
|
856 | : O extends { type: "string"; array: true } ? string[]
|
857 | : O extends { type: "number"; array: true } ? number[]
|
858 | : O extends { string: true; array: true } ? string[]
|
859 | : O extends { number: true; array: true } ? number[]
|
860 | : O extends { normalize: true; array: true } ? string[]
|
861 | : O extends { type: "array" } ? Array<string | number>
|
862 | : O extends { type: "boolean" } ? boolean
|
863 | : O extends { type: "number" } ? number
|
864 | : O extends { type: "string" } ? string
|
865 | : O extends { array: true } ? Array<string | number>
|
866 | : O extends { boolean: true } ? boolean
|
867 | : O extends { number: true } ? number
|
868 | : O extends { string: true } ? string
|
869 | : O extends { normalize: true } ? string
|
870 | : O extends { choices: ReadonlyArray<infer C> } ? C
|
871 | : O extends { coerce: (arg: any) => infer T } ? T
|
872 | : unknown;
|
873 |
|
874 | type InferredOptionTypes<O extends { [key: string]: Options }> = { [key in keyof O]: InferredOptionType<O[key]> };
|
875 |
|
876 | interface CommandModule<T = {}, U = {}> {
|
877 |
|
878 | aliases?: ReadonlyArray<string> | string | undefined;
|
879 |
|
880 | builder?: CommandBuilder<T, U> | undefined;
|
881 |
|
882 | command?: ReadonlyArray<string> | string | undefined;
|
883 |
|
884 | deprecated?: boolean | string | undefined;
|
885 |
|
886 | describe?: string | false | undefined;
|
887 |
|
888 | handler: (args: Arguments<U>) => void;
|
889 | }
|
890 |
|
891 | type ParseCallback<T = {}> = (err: Error | undefined, argv: Arguments<T>, output: string) => void;
|
892 | type CommandBuilder<T = {}, U = {}> =
|
893 | | { [key: string]: Options }
|
894 | | ((args: Argv<T>) => Argv<U>)
|
895 | | ((args: Argv<T>) => PromiseLike<Argv<U>>);
|
896 | type SyncCompletionFunction = (current: string, argv: any) => string[];
|
897 | type AsyncCompletionFunction = (
|
898 | current: string,
|
899 | argv: any,
|
900 | done: (completion: ReadonlyArray<string>) => void,
|
901 | ) => void;
|
902 | type PromiseCompletionFunction = (current: string, argv: any) => Promise<string[]>;
|
903 | type MiddlewareFunction<T = {}> = (args: Arguments<T>) => void;
|
904 | type Choices = ReadonlyArray<string | number | true | undefined>;
|
905 | type PositionalOptionsType = "boolean" | "number" | "string";
|
906 | }
|
907 |
|
908 | declare var yargs: yargs.Argv;
|
909 | export = yargs;
|