UNPKG

3.41 kBTypeScriptView Raw
1/**
2 * Returns an object containing option values parsed from the command line. By default it parses the global `process.argv` array.
3 * Parsing is strict by default. To be more permissive, enable `partial` or `stopAtFirstUnknown` modes.
4 */
5declare function commandLineArgs(
6 optionDefinitions: commandLineArgs.OptionDefinition[],
7 options?: commandLineArgs.ParseOptions,
8): commandLineArgs.CommandLineOptions;
9
10declare namespace commandLineArgs {
11 interface CommandLineOptions {
12 /**
13 * Command-line arguments not parsed by `commandLineArgs`.
14 */
15 _unknown?: string[] | undefined;
16 [propName: string]: any;
17 }
18
19 interface ParseOptions {
20 /**
21 * An array of strings which if present will be parsed instead of `process.argv`.
22 */
23 argv?: string[] | undefined;
24
25 /**
26 * If `true`, `commandLineArgs` will not throw on unknown options or values, instead returning them in the `_unknown` property of the output.
27 */
28 partial?: boolean | undefined;
29
30 /**
31 * If `true`, `commandLineArgs` will not throw on unknown options or values. Instead, parsing will stop at the first unknown argument
32 * and the remaining arguments returned in the `_unknown` property of the output. If set, `partial: true` is implied.
33 */
34 stopAtFirstUnknown?: boolean | undefined;
35
36 /**
37 * If `true`, options with hypenated names (e.g. `move-to`) will be returned in camel-case (e.g. `moveTo`).
38 */
39 camelCase?: boolean | undefined;
40
41 /**
42 * If `true`, the case of each option name or alias parsed is insignificant. For example, `--Verbose` and
43 * `--verbose` would be parsed identically, as would the aliases `-V` and `-v`. Defaults to false.
44 */
45 caseInsensitive?: boolean | undefined;
46 }
47
48 interface OptionDefinition {
49 /**
50 * The long option name.
51 */
52 name: string;
53
54 /**
55 * A setter function (you receive the output from this) enabling you to be specific about the type and value received. Typical values
56 * are `String` (the default), `Number` and `Boolean` but you can use a custom function. If no option value was set you will receive `null`.
57 */
58 type?: ((input: string) => any) | undefined;
59
60 /**
61 * A getopt-style short option name. Can be any single character except a digit or hyphen.
62 */
63 alias?: string | undefined;
64
65 /**
66 * Set this flag if the option accepts multiple values. In the output, you will receive an array of values each passed through the `type` function.
67 */
68 multiple?: boolean | undefined;
69
70 /**
71 * Identical to `multiple` but with greedy parsing disabled.
72 */
73 lazyMultiple?: boolean | undefined;
74
75 /**
76 * Any values unaccounted for by an option definition will be set on the `defaultOption`. This flag is typically set
77 * on the most commonly-used option to enable more concise usage.
78 */
79 defaultOption?: boolean | undefined;
80
81 /**
82 * An initial value for the option.
83 */
84 defaultValue?: any;
85
86 /**
87 * One or more group names the option belongs to.
88 */
89 group?: string | string[] | undefined;
90 }
91}
92
93export = commandLineArgs;
94
\No newline at end of file