commander 2.20.3 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +104 -8
- package/Readme.md +343 -159
- package/index.js +231 -122
- package/package.json +2 -2
- package/typings/index.d.ts +55 -65
package/Readme.md
CHANGED
|
@@ -1,187 +1,319 @@
|
|
|
1
1
|
# Commander.js
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
[](http://travis-ci.org/tj/commander.js)
|
|
5
4
|
[](https://www.npmjs.org/package/commander)
|
|
6
5
|
[](https://npmcharts.com/compare/commander?minimal=true)
|
|
7
6
|
[](https://packagephobia.now.sh/result?p=commander)
|
|
8
|
-
[](https://gitter.im/tj/commander.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
9
|
-
|
|
10
|
-
The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/commander-rb/commander).
|
|
11
|
-
[API documentation](http://tj.github.com/commander.js/)
|
|
12
7
|
|
|
8
|
+
The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/commander-rb/commander).
|
|
9
|
+
|
|
10
|
+
- [Commander.js](#commanderjs)
|
|
11
|
+
- [Installation](#installation)
|
|
12
|
+
- [Declaring _program_ variable](#declaring-program-variable)
|
|
13
|
+
- [Options](#options)
|
|
14
|
+
- [Common option types, boolean and value](#common-option-types-boolean-and-value)
|
|
15
|
+
- [Default option value](#default-option-value)
|
|
16
|
+
- [Other option types, negatable boolean and flag|value](#other-option-types-negatable-boolean-and-flagvalue)
|
|
17
|
+
- [Custom option processing](#custom-option-processing)
|
|
18
|
+
- [Version option](#version-option)
|
|
19
|
+
- [Commands](#commands)
|
|
20
|
+
- [Specify the argument syntax](#specify-the-argument-syntax)
|
|
21
|
+
- [Action handler (sub)commands](#action-handler-subcommands)
|
|
22
|
+
- [Git-style executable (sub)commands](#git-style-executable-subcommands)
|
|
23
|
+
- [Automated --help](#automated---help)
|
|
24
|
+
- [Custom help](#custom-help)
|
|
25
|
+
- [.usage and .name](#usage-and-name)
|
|
26
|
+
- [.outputHelp(cb)](#outputhelpcb)
|
|
27
|
+
- [.helpOption(flags, description)](#helpoptionflags-description)
|
|
28
|
+
- [.help(cb)](#helpcb)
|
|
29
|
+
- [Custom event listeners](#custom-event-listeners)
|
|
30
|
+
- [Bits and pieces](#bits-and-pieces)
|
|
31
|
+
- [TypeScript](#typescript)
|
|
32
|
+
- [Node options such as `--harmony`](#node-options-such-as---harmony)
|
|
33
|
+
- [Node debugging](#node-debugging)
|
|
34
|
+
- [Examples](#examples)
|
|
35
|
+
- [License](#license)
|
|
36
|
+
- [Support](#support)
|
|
13
37
|
|
|
14
38
|
## Installation
|
|
15
39
|
|
|
16
|
-
|
|
40
|
+
```bash
|
|
41
|
+
npm install commander
|
|
42
|
+
```
|
|
17
43
|
|
|
18
|
-
##
|
|
44
|
+
## Declaring _program_ variable
|
|
19
45
|
|
|
20
|
-
|
|
46
|
+
Commander exports a global object which is convenient for quick programs.
|
|
47
|
+
This is used in the examples in this README for brevity.
|
|
21
48
|
|
|
22
49
|
```js
|
|
23
|
-
|
|
50
|
+
const program = require('commander');
|
|
51
|
+
program.version('0.0.1');
|
|
52
|
+
```
|
|
24
53
|
|
|
25
|
-
|
|
26
|
-
* Module dependencies.
|
|
27
|
-
*/
|
|
54
|
+
For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local Command object to use.
|
|
28
55
|
|
|
29
|
-
|
|
56
|
+
```js
|
|
57
|
+
const commander = require('commander');
|
|
58
|
+
const program = new commander.Command();
|
|
59
|
+
program.version('0.0.1');
|
|
60
|
+
```
|
|
30
61
|
|
|
31
|
-
|
|
32
|
-
.version('0.1.0')
|
|
33
|
-
.option('-p, --peppers', 'Add peppers')
|
|
34
|
-
.option('-P, --pineapple', 'Add pineapple')
|
|
35
|
-
.option('-b, --bbq-sauce', 'Add bbq sauce')
|
|
36
|
-
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
|
|
37
|
-
.parse(process.argv);
|
|
62
|
+
## Options
|
|
38
63
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (program.bbqSauce) console.log(' - bbq');
|
|
43
|
-
console.log(' - %s cheese', program.cheese);
|
|
44
|
-
```
|
|
64
|
+
Options are defined with the `.option()` method, also serving as documentation for the options. Each option can have a short flag (single character) and a long name, separated by a comma or space.
|
|
65
|
+
|
|
66
|
+
The options can be accessed as properties on the Command object. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc. Multiple short flags may be combined as a single arg, for example `-abc` is equivalent to `-a -b -c`.
|
|
45
67
|
|
|
46
|
-
|
|
68
|
+
### Common option types, boolean and value
|
|
47
69
|
|
|
48
|
-
|
|
70
|
+
The two most used option types are a boolean flag, and an option which takes a value (declared using angle brackets). Both are `undefined` unless specified on command line.
|
|
49
71
|
|
|
50
72
|
```js
|
|
51
|
-
|
|
73
|
+
const program = require('commander');
|
|
52
74
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
75
|
+
program
|
|
76
|
+
.option('-d, --debug', 'output extra debugging')
|
|
77
|
+
.option('-s, --small', 'small pizza size')
|
|
78
|
+
.option('-p, --pizza-type <type>', 'flavour of pizza');
|
|
56
79
|
|
|
57
|
-
|
|
80
|
+
program.parse(process.argv);
|
|
58
81
|
|
|
59
|
-
program
|
|
60
|
-
|
|
61
|
-
|
|
82
|
+
if (program.debug) console.log(program.opts());
|
|
83
|
+
console.log('pizza details:');
|
|
84
|
+
if (program.small) console.log('- small pizza size');
|
|
85
|
+
if (program.pizzaType) console.log(`- ${program.pizzaType}`);
|
|
86
|
+
```
|
|
62
87
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
88
|
+
```bash
|
|
89
|
+
$ pizza-options -d
|
|
90
|
+
{ debug: true, small: undefined, pizzaType: undefined }
|
|
91
|
+
pizza details:
|
|
92
|
+
$ pizza-options -p
|
|
93
|
+
error: option '-p, --pizza-type <type>' argument missing
|
|
94
|
+
$ pizza-options -ds -p vegetarian
|
|
95
|
+
{ debug: true, small: true, pizzaType: 'vegetarian' }
|
|
96
|
+
pizza details:
|
|
97
|
+
- small pizza size
|
|
98
|
+
- vegetarian
|
|
99
|
+
$ pizza-options --pizza-type=cheese
|
|
100
|
+
pizza details:
|
|
101
|
+
- cheese
|
|
66
102
|
```
|
|
67
103
|
|
|
68
|
-
|
|
104
|
+
`program.parse(arguments)` processes the arguments, leaving any args not consumed by the options as the `program.args` array.
|
|
69
105
|
|
|
70
|
-
|
|
106
|
+
### Default option value
|
|
71
107
|
|
|
72
|
-
|
|
108
|
+
You can specify a default value for an option which takes a value.
|
|
73
109
|
|
|
74
|
-
|
|
110
|
+
```js
|
|
111
|
+
const program = require('commander');
|
|
75
112
|
|
|
76
|
-
|
|
113
|
+
program
|
|
114
|
+
.option('-c, --cheese <type>', 'add the specified type of cheese', 'blue');
|
|
77
115
|
|
|
116
|
+
program.parse(process.argv);
|
|
78
117
|
|
|
79
|
-
|
|
118
|
+
console.log(`cheese: ${program.cheese}`);
|
|
119
|
+
```
|
|
80
120
|
|
|
81
|
-
|
|
82
|
-
|
|
121
|
+
```bash
|
|
122
|
+
$ pizza-options
|
|
123
|
+
cheese: blue
|
|
124
|
+
$ pizza-options --cheese stilton
|
|
125
|
+
cheese: stilton
|
|
126
|
+
```
|
|
83
127
|
|
|
84
|
-
|
|
85
|
-
0.0.1
|
|
128
|
+
### Other option types, negatable boolean and flag|value
|
|
86
129
|
|
|
87
|
-
|
|
130
|
+
You can specify a boolean option long name with a leading `no-` to set the option value to false when used.
|
|
131
|
+
Defined alone this also makes the option true by default.
|
|
132
|
+
|
|
133
|
+
If you define `--foo` first, adding `--no-foo` does not change the default value from what it would
|
|
134
|
+
otherwise be. You can specify a default boolean value for a boolean flag and it can be overridden on command line.
|
|
88
135
|
|
|
89
136
|
```js
|
|
137
|
+
const program = require('commander');
|
|
138
|
+
|
|
90
139
|
program
|
|
91
|
-
.
|
|
92
|
-
|
|
140
|
+
.option('--no-sauce', 'Remove sauce')
|
|
141
|
+
.option('--cheese <flavour>', 'cheese flavour', 'mozzarella')
|
|
142
|
+
.option('--no-cheese', 'plain with no cheese')
|
|
143
|
+
.parse(process.argv);
|
|
93
144
|
|
|
94
|
-
|
|
145
|
+
const sauceStr = program.sauce ? 'sauce' : 'no sauce';
|
|
146
|
+
const cheeseStr = (program.cheese === false) ? 'no cheese' : `${program.cheese} cheese`;
|
|
147
|
+
console.log(`You ordered a pizza with ${sauceStr} and ${cheeseStr}`);
|
|
148
|
+
```
|
|
95
149
|
|
|
96
|
-
|
|
150
|
+
```bash
|
|
151
|
+
$ pizza-options
|
|
152
|
+
You ordered a pizza with sauce and mozzarella cheese
|
|
153
|
+
$ pizza-options --sauce
|
|
154
|
+
error: unknown option '--sauce'
|
|
155
|
+
$ pizza-options --cheese=blue
|
|
156
|
+
You ordered a pizza with sauce and blue cheese
|
|
157
|
+
$ pizza-options --no-sauce --no-cheese
|
|
158
|
+
You ordered a pizza with no sauce and no cheese
|
|
159
|
+
```
|
|
97
160
|
|
|
98
|
-
You can
|
|
161
|
+
You can specify an option which functions as a flag but may also take a value (declared using square brackets).
|
|
99
162
|
|
|
100
163
|
```js
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
var program = require('commander');
|
|
164
|
+
const program = require('commander');
|
|
104
165
|
|
|
105
166
|
program
|
|
106
|
-
.
|
|
107
|
-
.option('-r, --recursive', 'Remove recursively')
|
|
108
|
-
.action(function (dir, cmd) {
|
|
109
|
-
console.log('remove ' + dir + (cmd.recursive ? ' recursively' : ''))
|
|
110
|
-
})
|
|
167
|
+
.option('-c, --cheese [type]', 'Add cheese with optional type');
|
|
111
168
|
|
|
112
|
-
program.parse(process.argv)
|
|
169
|
+
program.parse(process.argv);
|
|
170
|
+
|
|
171
|
+
if (program.cheese === undefined) console.log('no cheese');
|
|
172
|
+
else if (program.cheese === true) console.log('add cheese');
|
|
173
|
+
else console.log(`add cheese type ${program.cheese}`);
|
|
113
174
|
```
|
|
114
175
|
|
|
115
|
-
|
|
176
|
+
```bash
|
|
177
|
+
$ pizza-options
|
|
178
|
+
no cheese
|
|
179
|
+
$ pizza-options --cheese
|
|
180
|
+
add cheese
|
|
181
|
+
$ pizza-options --cheese mozzarella
|
|
182
|
+
add cheese type mozzarella
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Custom option processing
|
|
186
|
+
|
|
187
|
+
You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the
|
|
188
|
+
previous value for the option. It returns the new value for the option.
|
|
116
189
|
|
|
117
|
-
|
|
190
|
+
This allows you to coerce the option value to the desired type, or accumulate values, or do entirely custom processing.
|
|
191
|
+
|
|
192
|
+
You can optionally specify the default/starting value for the option after the function.
|
|
118
193
|
|
|
119
194
|
```js
|
|
120
|
-
|
|
121
|
-
|
|
195
|
+
const program = require('commander');
|
|
196
|
+
|
|
197
|
+
function myParseInt(value, dummyPrevious) {
|
|
198
|
+
// parseInt takes a string and an optional radix
|
|
199
|
+
return parseInt(value);
|
|
122
200
|
}
|
|
123
201
|
|
|
124
|
-
function
|
|
125
|
-
return
|
|
202
|
+
function increaseVerbosity(dummyValue, previous) {
|
|
203
|
+
return previous + 1;
|
|
126
204
|
}
|
|
127
205
|
|
|
128
|
-
function collect(
|
|
129
|
-
|
|
130
|
-
return memo;
|
|
206
|
+
function collect(value, previous) {
|
|
207
|
+
return previous.concat([value]);
|
|
131
208
|
}
|
|
132
209
|
|
|
133
|
-
function
|
|
134
|
-
return
|
|
210
|
+
function commaSeparatedList(value, dummyPrevious) {
|
|
211
|
+
return value.split(',');
|
|
135
212
|
}
|
|
136
213
|
|
|
137
214
|
program
|
|
138
|
-
.
|
|
139
|
-
.
|
|
140
|
-
.option('-
|
|
141
|
-
.option('-
|
|
142
|
-
.option('-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
215
|
+
.option('-f, --float <number>', 'float argument', parseFloat)
|
|
216
|
+
.option('-i, --integer <number>', 'integer argument', myParseInt)
|
|
217
|
+
.option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0)
|
|
218
|
+
.option('-c, --collect <value>', 'repeatable value', collect, [])
|
|
219
|
+
.option('-l, --list <items>', 'comma separated list', commaSeparatedList)
|
|
220
|
+
;
|
|
221
|
+
|
|
222
|
+
program.parse(process.argv);
|
|
223
|
+
|
|
224
|
+
if (program.float !== undefined) console.log(`float: ${program.float}`);
|
|
225
|
+
if (program.integer !== undefined) console.log(`integer: ${program.integer}`);
|
|
226
|
+
if (program.verbose > 0) console.log(`verbosity: ${program.verbose}`);
|
|
227
|
+
if (program.collect.length > 0) console.log(program.collect);
|
|
228
|
+
if (program.list !== undefined) console.log(program.list);
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
$ custom -f 1e2
|
|
233
|
+
float: 100
|
|
234
|
+
$ custom --integer 2
|
|
235
|
+
integer: 2
|
|
236
|
+
$ custom -v -v -v
|
|
237
|
+
verbose: 3
|
|
238
|
+
$ custom -c a -c b -c c
|
|
239
|
+
[ 'a', 'b', 'c' ]
|
|
240
|
+
$ custom --list x,y,z
|
|
241
|
+
[ 'x', 'y', 'z' ]
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Version option
|
|
245
|
+
|
|
246
|
+
The optional `version` method adds handling for displaying the command version. The default option flags are `-V` and `--version`, and when present the command prints the version number and exits.
|
|
247
|
+
|
|
248
|
+
```js
|
|
249
|
+
program.version('0.0.1');
|
|
250
|
+
```
|
|
148
251
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
program.range = program.range || [];
|
|
153
|
-
console.log(' range: %j..%j', program.range[0], program.range[1]);
|
|
154
|
-
console.log(' list: %j', program.list);
|
|
155
|
-
console.log(' collect: %j', program.collect);
|
|
156
|
-
console.log(' verbosity: %j', program.verbose);
|
|
157
|
-
console.log(' args: %j', program.args);
|
|
252
|
+
```bash
|
|
253
|
+
$ ./examples/pizza -V
|
|
254
|
+
0.0.1
|
|
158
255
|
```
|
|
159
256
|
|
|
160
|
-
|
|
257
|
+
You may change the flags and description by passing additional parameters to the `version` method, using
|
|
258
|
+
the same syntax for flags as the `option` method. The version flags can be named anything, but a long name is required.
|
|
259
|
+
|
|
161
260
|
```js
|
|
261
|
+
program.version('0.0.1', '-v, --vers', 'output the current version');
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Commands
|
|
265
|
+
|
|
266
|
+
You can specify (sub)commands for your top-level command using `.command`. There are two ways these can be implemented: using an action handler attached to the command, or as a separate executable file (described in more detail later). In the first parameter to `.command` you specify the command name and any command arguments. The arguments may be `<required>` or `[optional]`, and the last argument may also be `variadic...`.
|
|
267
|
+
|
|
268
|
+
For example:
|
|
269
|
+
|
|
270
|
+
```js
|
|
271
|
+
// Command implemented using action handler (description is supplied separately to `.command`)
|
|
272
|
+
// Returns new command for configuring.
|
|
162
273
|
program
|
|
163
|
-
.
|
|
164
|
-
.
|
|
165
|
-
.
|
|
166
|
-
|
|
274
|
+
.command('clone <source> [destination]')
|
|
275
|
+
.description('clone a repository into a newly created directory')
|
|
276
|
+
.action((source, destination) => {
|
|
277
|
+
console.log('clone command called');
|
|
278
|
+
});
|
|
167
279
|
|
|
168
|
-
|
|
169
|
-
|
|
280
|
+
// Command implemented using separate executable file (description is second parameter to `.command`)
|
|
281
|
+
// Returns top-level command for adding more commands.
|
|
282
|
+
program
|
|
283
|
+
.command('start <service>', 'start named service')
|
|
284
|
+
.command('stop [service]', 'stop named service, or all if no name supplied');
|
|
170
285
|
```
|
|
171
286
|
|
|
172
|
-
|
|
287
|
+
### Specify the argument syntax
|
|
173
288
|
|
|
174
|
-
|
|
175
|
-
append `...` to the argument name. Here is an example:
|
|
289
|
+
You use `.arguments` to specify the arguments for the top-level command, and for subcommands they are included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required input. Square brackets (e.g. `[optional]`) indicate optional input.
|
|
176
290
|
|
|
177
291
|
```js
|
|
178
|
-
|
|
292
|
+
const program = require('commander');
|
|
293
|
+
|
|
294
|
+
program
|
|
295
|
+
.version('0.1.0')
|
|
296
|
+
.arguments('<cmd> [env]')
|
|
297
|
+
.action(function (cmd, env) {
|
|
298
|
+
cmdValue = cmd;
|
|
299
|
+
envValue = env;
|
|
300
|
+
});
|
|
179
301
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
302
|
+
program.parse(process.argv);
|
|
303
|
+
|
|
304
|
+
if (typeof cmdValue === 'undefined') {
|
|
305
|
+
console.error('no command given!');
|
|
306
|
+
process.exit(1);
|
|
307
|
+
}
|
|
308
|
+
console.log('command:', cmdValue);
|
|
309
|
+
console.log('environment:', envValue || "no environment given");
|
|
310
|
+
```
|
|
183
311
|
|
|
184
|
-
|
|
312
|
+
The last argument of a command can be variadic, and only the last argument. To make an argument variadic you
|
|
313
|
+
append `...` to the argument name. For example:
|
|
314
|
+
|
|
315
|
+
```js
|
|
316
|
+
const program = require('commander');
|
|
185
317
|
|
|
186
318
|
program
|
|
187
319
|
.version('0.1.0')
|
|
@@ -198,83 +330,78 @@ program
|
|
|
198
330
|
program.parse(process.argv);
|
|
199
331
|
```
|
|
200
332
|
|
|
201
|
-
|
|
202
|
-
to your action as demonstrated above.
|
|
333
|
+
The variadic argument is passed to the action handler as an array. (And this also applies to `program.args`.)
|
|
203
334
|
|
|
204
|
-
|
|
335
|
+
### Action handler (sub)commands
|
|
205
336
|
|
|
206
|
-
|
|
207
|
-
|
|
337
|
+
You can add options to a command that uses an action handler.
|
|
338
|
+
The action handler gets passed a parameter for each argument you declared, and one additional argument which is the
|
|
339
|
+
command object itself. This command argument has the values for the command-specific options added as properties.
|
|
208
340
|
|
|
209
|
-
|
|
341
|
+
```js
|
|
342
|
+
const program = require('commander');
|
|
210
343
|
|
|
211
344
|
program
|
|
212
|
-
.
|
|
213
|
-
.
|
|
214
|
-
.action(function (
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
program.parse(process.argv);
|
|
345
|
+
.command('rm <dir>')
|
|
346
|
+
.option('-r, --recursive', 'Remove recursively')
|
|
347
|
+
.action(function (dir, cmdObj) {
|
|
348
|
+
console.log('remove ' + dir + (cmdObj.recursive ? ' recursively' : ''))
|
|
349
|
+
})
|
|
220
350
|
|
|
221
|
-
|
|
222
|
-
console.error('no command given!');
|
|
223
|
-
process.exit(1);
|
|
224
|
-
}
|
|
225
|
-
console.log('command:', cmdValue);
|
|
226
|
-
console.log('environment:', envValue || "no environment given");
|
|
351
|
+
program.parse(process.argv)
|
|
227
352
|
```
|
|
228
|
-
Angled brackets (e.g. `<cmd>`) indicate required input. Square brackets (e.g. `[env]`) indicate optional input.
|
|
229
353
|
|
|
230
|
-
|
|
354
|
+
A command's options on the command line are validated when the command is used. Any unknown options will be reported as an error. However, if an action-based command does not define an action, then the options are not validated.
|
|
355
|
+
|
|
356
|
+
Configuration options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the command from the generated help output.
|
|
357
|
+
|
|
358
|
+
### Git-style executable (sub)commands
|
|
359
|
+
|
|
360
|
+
When `.command()` is invoked with a description argument, this tells commander that you're going to use separate executables for sub-commands, much like `git(1)` and other popular tools.
|
|
361
|
+
Commander will search the executables in the directory of the entry script (like `./examples/pm`) with the name `program-subcommand`, like `pm-install`, `pm-search`.
|
|
362
|
+
You can specify a custom name with the `executableFile` configuration option.
|
|
363
|
+
|
|
364
|
+
You handle the options for an executable (sub)command in the executable, and don't declare them at the top-level.
|
|
231
365
|
|
|
232
366
|
```js
|
|
233
367
|
// file: ./examples/pm
|
|
234
|
-
|
|
368
|
+
const program = require('commander');
|
|
235
369
|
|
|
236
370
|
program
|
|
237
371
|
.version('0.1.0')
|
|
238
372
|
.command('install [name]', 'install one or more packages')
|
|
239
373
|
.command('search [query]', 'search with optional query')
|
|
374
|
+
.command('update', 'update installed packages', {executableFile: 'myUpdateSubCommand'})
|
|
240
375
|
.command('list', 'list packages installed', {isDefault: true})
|
|
241
376
|
.parse(process.argv);
|
|
242
377
|
```
|
|
243
378
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
Options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the subcommand from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified.
|
|
379
|
+
Configuration options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the command from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified.
|
|
380
|
+
Specifying a name with `executableFile` will override the default constructed name.
|
|
248
381
|
|
|
249
382
|
If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.
|
|
250
383
|
|
|
251
|
-
### `--harmony`
|
|
252
|
-
|
|
253
|
-
You can enable `--harmony` option in two ways:
|
|
254
|
-
* Use `#! /usr/bin/env node --harmony` in the sub-commands scripts. Note some os version don’t support this pattern.
|
|
255
|
-
* Use the `--harmony` option when call the command, like `node --harmony examples/pm publish`. The `--harmony` option will be preserved when spawning sub-command process.
|
|
256
|
-
|
|
257
384
|
## Automated --help
|
|
258
385
|
|
|
259
386
|
The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:
|
|
260
387
|
|
|
261
|
-
```
|
|
388
|
+
```bash
|
|
262
389
|
$ ./examples/pizza --help
|
|
263
390
|
Usage: pizza [options]
|
|
264
391
|
|
|
265
392
|
An application for pizzas ordering
|
|
266
393
|
|
|
267
394
|
Options:
|
|
268
|
-
-h, --help output usage information
|
|
269
395
|
-V, --version output the version number
|
|
270
396
|
-p, --peppers Add peppers
|
|
271
397
|
-P, --pineapple Add pineapple
|
|
272
398
|
-b, --bbq Add bbq sauce
|
|
273
|
-
-c, --cheese <type> Add the specified type of cheese
|
|
399
|
+
-c, --cheese <type> Add the specified type of cheese (default: "marble")
|
|
274
400
|
-C, --no-cheese You do not want any cheese
|
|
401
|
+
-h, --help output usage information
|
|
275
402
|
```
|
|
276
403
|
|
|
277
|
-
|
|
404
|
+
### Custom help
|
|
278
405
|
|
|
279
406
|
You can display arbitrary `-h, --help` information
|
|
280
407
|
by listening for "--help". Commander will automatically
|
|
@@ -286,11 +413,7 @@ Options:
|
|
|
286
413
|
```js
|
|
287
414
|
#!/usr/bin/env node
|
|
288
415
|
|
|
289
|
-
|
|
290
|
-
* Module dependencies.
|
|
291
|
-
*/
|
|
292
|
-
|
|
293
|
-
var program = require('commander');
|
|
416
|
+
const program = require('commander');
|
|
294
417
|
|
|
295
418
|
program
|
|
296
419
|
.version('0.1.0')
|
|
@@ -315,7 +438,7 @@ console.log('stuff');
|
|
|
315
438
|
|
|
316
439
|
Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:
|
|
317
440
|
|
|
318
|
-
```
|
|
441
|
+
```Text
|
|
319
442
|
Usage: custom-help [options]
|
|
320
443
|
|
|
321
444
|
Options:
|
|
@@ -330,7 +453,24 @@ Examples:
|
|
|
330
453
|
$ custom-help -h
|
|
331
454
|
```
|
|
332
455
|
|
|
333
|
-
|
|
456
|
+
### .usage and .name
|
|
457
|
+
|
|
458
|
+
These allow you to customise the usage description in the first line of the help. The name is otherwise
|
|
459
|
+
deduced from the (full) program arguments. Given:
|
|
460
|
+
|
|
461
|
+
```js
|
|
462
|
+
program
|
|
463
|
+
.name("my-command")
|
|
464
|
+
.usage("[global options] command")
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
The help will start with:
|
|
468
|
+
|
|
469
|
+
```Text
|
|
470
|
+
Usage: my-command [global options] command
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
### .outputHelp(cb)
|
|
334
474
|
|
|
335
475
|
Output help information without exiting.
|
|
336
476
|
Optional callback cb allows post-processing of help text before it is displayed.
|
|
@@ -338,8 +478,8 @@ Optional callback cb allows post-processing of help text before it is displayed.
|
|
|
338
478
|
If you want to display help by default (e.g. if no command was provided), you can use something like:
|
|
339
479
|
|
|
340
480
|
```js
|
|
341
|
-
|
|
342
|
-
|
|
481
|
+
const program = require('commander');
|
|
482
|
+
const colors = require('colors');
|
|
343
483
|
|
|
344
484
|
program
|
|
345
485
|
.version('0.1.0')
|
|
@@ -355,13 +495,22 @@ function make_red(txt) {
|
|
|
355
495
|
}
|
|
356
496
|
```
|
|
357
497
|
|
|
358
|
-
|
|
498
|
+
### .helpOption(flags, description)
|
|
499
|
+
|
|
500
|
+
Override the default help flags and description.
|
|
501
|
+
|
|
502
|
+
```js
|
|
503
|
+
program
|
|
504
|
+
.helpOption('-e, --HELP', 'read more information');
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
### .help(cb)
|
|
359
508
|
|
|
360
509
|
Output help information and exit immediately.
|
|
361
510
|
Optional callback cb allows post-processing of help text before it is displayed.
|
|
362
511
|
|
|
363
|
-
|
|
364
512
|
## Custom event listeners
|
|
513
|
+
|
|
365
514
|
You can execute custom actions by listening to command and option events.
|
|
366
515
|
|
|
367
516
|
```js
|
|
@@ -376,10 +525,39 @@ program.on('command:*', function () {
|
|
|
376
525
|
});
|
|
377
526
|
```
|
|
378
527
|
|
|
528
|
+
## Bits and pieces
|
|
529
|
+
|
|
530
|
+
### TypeScript
|
|
531
|
+
|
|
532
|
+
The Commander package includes its TypeScript Definition file, but also requires the node types which you need to install yourself. e.g.
|
|
533
|
+
|
|
534
|
+
```bash
|
|
535
|
+
npm install commander
|
|
536
|
+
npm install --save-dev @types/node
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
If you use `ts-node` and git-style sub-commands written as `.ts` files, you need to call your program through node to get the sub-commands called correctly. e.g.
|
|
540
|
+
|
|
541
|
+
```bash
|
|
542
|
+
node -r ts-node/register pm.ts
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
### Node options such as `--harmony`
|
|
546
|
+
|
|
547
|
+
You can enable `--harmony` option in two ways:
|
|
548
|
+
|
|
549
|
+
- Use `#! /usr/bin/env node --harmony` in the sub-commands scripts. (Note Windows does not support this pattern.)
|
|
550
|
+
- Use the `--harmony` option when call the command, like `node --harmony examples/pm publish`. The `--harmony` option will be preserved when spawning sub-command process.
|
|
551
|
+
|
|
552
|
+
### Node debugging
|
|
553
|
+
|
|
554
|
+
If you are using the node inspector for [debugging](https://nodejs.org/en/docs/guides/debugging-getting-started/) git-style executable (sub)commands using `node -inspect` et al,
|
|
555
|
+
the inspector port is incremented by 1 for the spawned subcommand.
|
|
556
|
+
|
|
379
557
|
## Examples
|
|
380
558
|
|
|
381
559
|
```js
|
|
382
|
-
|
|
560
|
+
const program = require('commander');
|
|
383
561
|
|
|
384
562
|
program
|
|
385
563
|
.version('0.1.0')
|
|
@@ -392,7 +570,7 @@ program
|
|
|
392
570
|
.description('run setup commands for all envs')
|
|
393
571
|
.option("-s, --setup_mode [mode]", "Which setup mode to use")
|
|
394
572
|
.action(function(env, options){
|
|
395
|
-
|
|
573
|
+
const mode = options.setup_mode || "normal";
|
|
396
574
|
env = env || 'all';
|
|
397
575
|
console.log('setup for %s env(s) with %s mode', env, mode);
|
|
398
576
|
});
|
|
@@ -426,3 +604,9 @@ More Demos can be found in the [examples](https://github.com/tj/commander.js/tre
|
|
|
426
604
|
## License
|
|
427
605
|
|
|
428
606
|
[MIT](https://github.com/tj/commander.js/blob/master/LICENSE)
|
|
607
|
+
|
|
608
|
+
## Support
|
|
609
|
+
|
|
610
|
+
[Professionally supported commander is now available](https://tidelift.com/subscription/pkg/npm-commander?utm_source=npm-commander&utm_medium=referral&utm_campaign=readme)
|
|
611
|
+
|
|
612
|
+
Tidelift gives software development teams a single source for purchasing and maintaining their software, with professional grade assurances from the experts who know it best, while seamlessly integrating with existing tools.
|