Skip to content

Commit 0a9f304

Browse files
committed
Add TypeScript type declaration file
This provides completion information for users of both JavaScript and TypeScript with supported editors. This also allows TypeScript users to have type checking and be able to use the library when strict checking is enabled (which is the default for new TS projects).
1 parent d893868 commit 0a9f304

File tree

2 files changed

+262
-0
lines changed

2 files changed

+262
-0
lines changed

lib/dashdash.d.ts

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
// Type definitions for dashdash 1.14
2+
// Project: https://github.com/trentm/node-dashdash#readme
3+
4+
/// <reference types="node" />
5+
6+
export class Parser {
7+
constructor(config: ParseConfiguration);
8+
9+
bashCompletion(args: BashCompletionConfiguration): string;
10+
11+
help(config?: HelpConfiguration): string;
12+
13+
parse(inputs?: string[]): Results;
14+
}
15+
16+
export function addOptionType(optionType: OptionType): void;
17+
18+
export function bashCompletionFromOptions(args: BashCompletionConfiguration): string;
19+
20+
export function bashCompletionSpecFromOptions(args: BashCompletionSpecConfiguration): string;
21+
22+
export function createParser(config: ParseConfiguration): Parser;
23+
24+
export function getOptionType(name: string): OptionType;
25+
26+
export function parse(config: ParseConfiguration): Results;
27+
28+
export interface Results {
29+
[key: string]: any;
30+
_order: Arg[];
31+
_args: string[];
32+
}
33+
34+
export interface Arg {
35+
name: string;
36+
value: any;
37+
from: string;
38+
}
39+
40+
/**
41+
* Used by node-cmdln to put together a synopsis of options for a command
42+
*/
43+
export function synopsisFromOpt(o: Option): string;
44+
45+
export type Option = OptionWithoutAliases | OptionWithAliases;
46+
47+
export interface ParseConfiguration {
48+
/**
49+
* The argv to parse. Defaults to `process.argv`.
50+
*/
51+
argv?: string[];
52+
53+
/**
54+
* The index into argv at which options/args begin. Default is 2, as appropriate for `process.argv`.
55+
*/
56+
slice?: number;
57+
58+
/**
59+
* The env to use for 'env' entries in the option specs. Defaults to `process.env`.
60+
*/
61+
env?: any; // NodeJS.ProcessEnv;
62+
63+
options?: Array<Option | Group>;
64+
}
65+
66+
export interface OptionWithoutAliases extends OptionBase {
67+
/**
68+
* The option name
69+
*/
70+
name: string;
71+
}
72+
73+
export interface OptionWithAliases extends OptionBase {
74+
/**
75+
* The option name and aliases. The first name (if more than one given) is the key for the parsed `opts` object.
76+
*/
77+
names: string[];
78+
}
79+
80+
export interface OptionBase {
81+
/**
82+
* One of: bool, string, number, integer, positiveInteger, arrayOfBool, arrayOfString,
83+
* arrayOfNumber, arrayOfInteger, arrayOfPositiveInteger, arrayOfDate,
84+
* 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").
85+
* You can add your own custom option types with `dashdash.addOptionType`
86+
* These names attempt to match with asserts on `assert-plus`.
87+
*/
88+
type: string;
89+
90+
/**
91+
* This is used for Bash completion for an option argument.
92+
* If not specified, then the value of type is used. Any string may be specified, but only the following values have meaning:
93+
* - none: Provide no completions.
94+
* - file: Bash's default completion (i.e. complete -o default), which includes filenames.
95+
* - Any string FOO for which a function complete_FOO Bash function is defined.
96+
* This is for custom completions for a given tool.
97+
* Typically these custom functions are provided in the specExtra argument to dashdash.bashCompletionFromOptions().
98+
*/
99+
completionType?: string;
100+
101+
/**
102+
* An environment variable name (or names) that can be used as a fallback for this option.
103+
* An environment variable is only used as a fallback, i.e. it is ignored if the associated option is given in `argv`.
104+
*/
105+
env?: string | string[];
106+
107+
/**
108+
* Used for parser.help() output.
109+
*/
110+
help?: string;
111+
112+
/**
113+
* Used in help output as the placeholder for the option argument.
114+
*/
115+
helpArg?: string;
116+
117+
/**
118+
* Set this to false to have that option's help not be text wrapped in <parser>.help() output.
119+
*/
120+
helpWrap?: boolean;
121+
122+
/**
123+
* A default value used for this option, if the option isn't specified in argv.
124+
*/
125+
default?: string;
126+
127+
/**
128+
* If true, help output will not include this option.
129+
*/
130+
hidden?: boolean;
131+
}
132+
133+
export interface Group {
134+
group: string;
135+
}
136+
137+
export interface OptionType {
138+
name: string;
139+
takesArg: boolean;
140+
helpArg: string;
141+
parseArg(option: Option, optstr: string, arg: string): any;
142+
array?: boolean;
143+
arrayFlatten?: boolean;
144+
default?: any;
145+
completionType?: any;
146+
}
147+
148+
export interface BashCompletionConfiguration {
149+
/**
150+
* The tool name.
151+
*/
152+
name: string;
153+
154+
/**
155+
* The array of dashdash option specs.
156+
*/
157+
options?: Array<Option | Group>;
158+
159+
/**
160+
* Extra Bash code content to add
161+
* to the end of the "spec". Typically this is used to append Bash
162+
* "complete_TYPE" functions for custom option types.
163+
*/
164+
specExtra?: string;
165+
166+
/**
167+
* Array of completion types for positional args (i.e. non-options).
168+
* If not given, positional args will use Bash's 'default' completion.
169+
*/
170+
argtypes?: string[];
171+
}
172+
173+
export interface BashCompletionSpecConfiguration {
174+
/**
175+
* The array of dashdash option specs.
176+
*/
177+
options: Array<Option | Group>;
178+
179+
/**
180+
* A context string for the "local cmd*"
181+
* vars in the spec. By default it is the empty string. When used to
182+
* scope for completion on a *sub-command*.
183+
*/
184+
context?: string;
185+
186+
/**
187+
* By default
188+
* hidden options and subcmds are "excluded". Here excluded means they
189+
* won't be offered as a completion, but if used, their argument type
190+
* will be completed. "Hidden" options and subcmds are ones with the
191+
* `hidden: true` attribute to exclude them from default help output.
192+
*/
193+
includeHidden?: boolean;
194+
195+
/**
196+
* Array of completion types for positional args (i.e. non-options).
197+
* If not given, positional args will use Bash's 'default' completion.
198+
*/
199+
argtypes?: string[];
200+
}
201+
202+
export interface HelpConfiguration {
203+
/**
204+
* Set to a number (for that many spaces) or a string for the literal indent.
205+
* Default: 4
206+
*/
207+
indent?: number | string;
208+
209+
/**
210+
* Set to a number (for that many spaces) or a string for the literal indent.
211+
* This indent applies to group heading lines, between normal option lines.
212+
* Default: half length of `indent`
213+
*/
214+
headingIndent?: number | string;
215+
216+
/**
217+
* By default the names are sorted to put the short opts first (i.e. '-h, --help' preferred to '--help, -h').
218+
* Set to 'none' to not do this sorting.
219+
* Default: 'length'
220+
*/
221+
nameSort?: string;
222+
223+
/**
224+
* Note that reflow is just done on whitespace so a long token in the option help can overflow maxCol.
225+
* Default: 80
226+
*/
227+
maxCol?: number;
228+
229+
/**
230+
* If not set a reasonable value will be determined between minHelpCol and maxHelpCol.
231+
*/
232+
helpCol?: number;
233+
234+
/**
235+
* Default: 20
236+
*/
237+
minHelpCol?: number;
238+
239+
/**
240+
* Default: 40
241+
*/
242+
maxHelpCol?: number;
243+
244+
/**
245+
* Set to `false` to have option `help` strings not be textwrapped to the helpCol..maxCol range.
246+
* Default: true
247+
*/
248+
helpWrap?: boolean;
249+
250+
/**
251+
* If the option has associated environment variables (via the env option spec attribute), then append mentioned of those envvars to the help string.
252+
* Default: false
253+
*/
254+
includeEnv?: boolean;
255+
256+
/**
257+
* 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.
258+
* Default: false
259+
*/
260+
includeDefault?: boolean;
261+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"url": "git://github.com/trentm/node-dashdash.git"
1111
},
1212
"main": "./lib/dashdash.js",
13+
"types": "./lib/dashdash.d.ts",
1314
"dependencies": {
1415
"assert-plus": "^1.0.0"
1516
},

0 commit comments

Comments
 (0)