UNPKG

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