UNPKG

10.1 kBTypeScriptView Raw
1export class Parser {
2 /** Allow interspersed arguments. @default true */
3 interpersed: boolean;
4
5 /** Don't allow unknown flags. @default true */
6 allowUnknown: boolean;
7
8 constructor(config: ParserConfiguration);
9
10 /** Return a string suitable for a Bash completion file for this tool. */
11 bashCompletion(args: BashCompletionConfiguration): string;
12
13 /**
14 * Return help output for the current options.
15 *
16 * E.g.: if the current options are:
17 * [{names: ['help', 'h'], type: 'bool', help: 'Show help and exit.'}]
18 * then this would return:
19 * ' -h, --help Show help and exit.\n'
20 */
21 help(config?: HelpConfiguration): string;
22
23 /** Parse options from the given argv. */
24 parse(inputs?: string[] | ParsingConfiguration): Results;
25}
26
27/**
28 * Add a new option type.
29 */
30export function addOptionType(optionType: OptionType): void;
31
32export function bashCompletionFromOptions(args: BashCompletionConfiguration): string;
33
34export function bashCompletionSpecFromOptions(args: BashCompletionSpecConfiguration): string;
35
36export function createParser(config: ParserConfiguration): Parser;
37
38export function getOptionType(name: string): OptionType;
39
40/**
41 * Parse argv with the given options.
42 *
43 * @param config A merge of all the available fields from
44 * `dashdash.Parser` and `dashdash.Parser.parse`: options, interspersed,
45 * argv, env, slice.
46 */
47export function parse(config: ParsingConfiguration): Results;
48
49export interface Results {
50 [key: string]: any;
51 _order: Arg[];
52 _args: string[];
53}
54
55export interface Arg {
56 name: string;
57 value: any;
58 from: string;
59}
60
61/**
62 * Return a synopsis string for the given option spec.
63 *
64 * Examples:
65 * > synopsisFromOpt({names: ['help', 'h'], type: 'bool'});
66 * '[ --help | -h ]'
67 * > synopsisFromOpt({name: 'file', type: 'string', helpArg: 'FILE'});
68 * '[ --file=FILE ]'
69 */
70export function synopsisFromOpt(o: Option): string;
71
72export type Option = OptionWithoutAliases | OptionWithAliases;
73
74export interface ParsingConfiguration {
75 /**
76 * The argv to parse. Defaults to `process.argv`.
77 */
78 argv?: string[] | undefined;
79
80 /**
81 * The index into argv at which options/args begin. Default is 2, as appropriate for `process.argv`.
82 */
83 slice?: number | undefined;
84
85 /**
86 * The env to use for 'env' entries in the option specs. Defaults to `process.env`.
87 */
88 env?: any; // NodeJS.ProcessEnv;
89
90 options?: Array<Option | Group> | undefined;
91}
92
93export interface ParserConfiguration {
94 /**
95 * Array of option specs.
96 */
97 options: Array<Option | Group>;
98
99 /**
100 * Whether to throw on unknown options.
101 * If false, then unknown args are included in the _args array.
102 * Default: false
103 */
104 allowUnknown?: boolean | undefined;
105
106 /**
107 * Whether to allow interspersed arguments (non-options) and options.
108 *
109 * E.g.:
110 * node tool.js arg1 arg2 -v
111 *
112 * '-v' is after some args here. If `interspersed: false` then '-v'
113 * would not be parsed out. Note that regardless of `interspersed`
114 * the presence of '--' will stop option parsing, as all good
115 * option parsers should.
116 *
117 * Default: true
118 */
119 interspersed?: boolean | undefined;
120}
121
122export interface OptionWithoutAliases extends OptionBase {
123 /**
124 * The option name
125 */
126 name: string;
127}
128
129export interface OptionWithAliases extends OptionBase {
130 /**
131 * The option name and aliases. The first name (if more than one given) is the key for the parsed `opts` object.
132 */
133 names: string[];
134}
135
136export interface OptionBase {
137 /**
138 * One of: bool, string, number, integer, positiveInteger, arrayOfBool, arrayOfString,
139 * arrayOfNumber, arrayOfInteger, arrayOfPositiveInteger, arrayOfDate,
140 * date (epoch seconds, e.g. 1396031701, or ISO 8601 format YYYY-MM-DD[THH:MM:SS[.sss][Z]], e.g. "2014-03-28T18:35:01.489Z").
141 * You can add your own custom option types with `dashdash.addOptionType`
142 * These names attempt to match with asserts on `assert-plus`.
143 */
144 type: string;
145
146 /**
147 * This is used for Bash completion for an option argument.
148 * If not specified, then the value of type is used. Any string may be specified, but only the following values have meaning:
149 * - none: Provide no completions.
150 * - file: Bash's default completion (i.e. complete -o default), which includes filenames.
151 * - Any string FOO for which a function complete_FOO Bash function is defined.
152 * This is for custom completions for a given tool.
153 * Typically these custom functions are provided in the specExtra argument to dashdash.bashCompletionFromOptions().
154 */
155 completionType?: string | undefined;
156
157 /**
158 * An environment variable name (or names) that can be used as a fallback for this option.
159 * An environment variable is only used as a fallback, i.e. it is ignored if the associated option is given in `argv`.
160 */
161 env?: string | string[] | undefined;
162
163 /**
164 * Used for parser.help() output.
165 */
166 help?: string | undefined;
167
168 /**
169 * Used in help output as the placeholder for the option argument.
170 */
171 helpArg?: string | undefined;
172
173 /**
174 * Set this to false to have that option's help not be text wrapped in <parser>.help() output.
175 */
176 helpWrap?: boolean | undefined;
177
178 /**
179 * A default value used for this option, if the option isn't specified in argv.
180 */
181 default?: string | undefined;
182
183 /**
184 * If true, help output will not include this option.
185 */
186 hidden?: boolean | undefined;
187}
188
189export interface Group {
190 group: string;
191}
192
193export interface OptionType {
194 name: string;
195 /**
196 * Whether this type of option takes an
197 * argument on process.argv. Typically this is true for all but the
198 * "bool" type.
199 */
200 takesArg: boolean;
201 /**
202 * Required iff `takesArg === true`. The string to show in generated help for options of this type.
203 */
204 helpArg?: string | undefined;
205 /**
206 * parser that takes a string argument and returns an instance of the
207 * appropriate type, or throws an error if the arg is invalid.
208 */
209 parseArg(option: Option, optstr: string, arg: string): any;
210 /**
211 * Set to true if this is an 'arrayOf' type
212 * that collects multiple usages of the option in process.argv and puts results in an array.
213 */
214 array?: boolean | undefined;
215 arrayFlatten?: boolean | undefined;
216 /**
217 * Default value for options of this type, if no default is specified in the option type usage.
218 */
219 default?: any;
220 completionType?: any;
221}
222
223export interface BashCompletionConfiguration {
224 /**
225 * The tool name.
226 */
227 name: string;
228
229 /**
230 * The array of dashdash option specs.
231 */
232 options?: Array<Option | Group> | undefined;
233
234 /**
235 * Extra Bash code content to add
236 * to the end of the "spec". Typically this is used to append Bash
237 * "complete_TYPE" functions for custom option types.
238 */
239 specExtra?: string | undefined;
240
241 /**
242 * Array of completion types for positional args (i.e. non-options).
243 * If not given, positional args will use Bash's 'default' completion.
244 */
245 argtypes?: string[] | undefined;
246}
247
248export interface BashCompletionSpecConfiguration {
249 /**
250 * The array of dashdash option specs.
251 */
252 options: Array<Option | Group>;
253
254 /**
255 * A context string for the "local cmd*"
256 * vars in the spec. By default it is the empty string. When used to
257 * scope for completion on a *sub-command*.
258 */
259 context?: string | undefined;
260
261 /**
262 * By default
263 * hidden options and subcmds are "excluded". Here excluded means they
264 * won't be offered as a completion, but if used, their argument type
265 * will be completed. "Hidden" options and subcmds are ones with the
266 * `hidden: true` attribute to exclude them from default help output.
267 */
268 includeHidden?: boolean | undefined;
269
270 /**
271 * Array of completion types for positional args (i.e. non-options).
272 * If not given, positional args will use Bash's 'default' completion.
273 */
274 argtypes?: string[] | undefined;
275}
276
277export interface HelpConfiguration {
278 /**
279 * Set to a number (for that many spaces) or a string for the literal indent.
280 * Default: 4
281 */
282 indent?: number | string | undefined;
283
284 /**
285 * Set to a number (for that many spaces) or a string for the literal indent.
286 * This indent applies to group heading lines, between normal option lines.
287 * Default: half length of `indent`
288 */
289 headingIndent?: number | string | undefined;
290
291 /**
292 * By default the names are sorted to put the short opts first (i.e. '-h, --help' preferred to '--help, -h').
293 * Set to 'none' to not do this sorting.
294 * Default: 'length'
295 */
296 nameSort?: string | undefined;
297
298 /**
299 * Note that reflow is just done on whitespace so a long token in the option help can overflow maxCol.
300 * Default: 80
301 */
302 maxCol?: number | undefined;
303
304 /**
305 * If not set a reasonable value will be determined between minHelpCol and maxHelpCol.
306 */
307 helpCol?: number | undefined;
308
309 /**
310 * Default: 20
311 */
312 minHelpCol?: number | undefined;
313
314 /**
315 * Default: 40
316 */
317 maxHelpCol?: number | undefined;
318
319 /**
320 * Set to `false` to have option `help` strings not be textwrapped to the helpCol..maxCol range.
321 * Default: true
322 */
323 helpWrap?: boolean | undefined;
324
325 /**
326 * If the option has associated environment variables (via the env option spec attribute), then append mentioned of those envvars to the help string.
327 * Default: false
328 */
329 includeEnv?: boolean | undefined;
330
331 /**
332 * If the option has a default value (via the default option spec attribute, or a default on the option's type), then a "Default: VALUE" string will be appended to the help string.
333 * Default: false
334 */
335 includeDefault?: boolean | undefined;
336}