commander 5.0.0-2 → 5.1.0

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 CHANGED
@@ -7,33 +7,28 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
7
7
 
8
8
  <!-- markdownlint-disable MD024 -->
9
9
 
10
- ## [5.0.0-2] (2020-02-10)
10
+ ## [5.1.0] (2020-04-25)
11
11
 
12
12
  ### Added
13
13
 
14
- - suggest help option along with unknown command error ([#1179])
15
-
16
- ### Changed
14
+ - support for multiple command aliases, the first of which is shown in the auto-generated help ([#531], [#1236])
15
+ - configuration support in `addCommand()` for `hidden` and `isDefault` ([#1232])
17
16
 
18
- - TypeScript fluent return types changed to be more subclass friendly, return `this` rather than `Command` ([#1180])
19
-
20
- ## [5.0.0-1] (2020-02-08)
17
+ ### Fixed
21
18
 
22
- ### Added
23
-
24
- * `.helpInformation()` returns help text as a string, previously a private routine ([#1169])
25
- * `.parse()` implicitly uses `process.argv` if arguments not specified ([#1172])
26
- * optionally specify where `.parse()` arguments "from", if not following node conventions ([#512] [#1172])
19
+ - omit masked help flags from the displayed help ([#645], [#1247])
20
+ - remove old short help flag when change help flags using `helpOption` ([#1248])
27
21
 
28
22
  ### Changed
29
23
 
30
- * changes to error handling ([#1165])
31
- * throw for author error, not just display message
32
- * preflight for variadic error
33
- * add tips to missing subcommand executable
34
- * update dependencies
24
+ - remove use of `arguments` to improve auto-generated help in editors ([#1235])
25
+ - rename `.command()` configuration `noHelp` to `hidden` (but not remove old support) ([#1232])
26
+ - improvements to documentation
27
+ - update dependencies
28
+ - update tested versions of node
29
+ - eliminate lint errors in TypeScript ([#1208])
35
30
 
36
- ## [5.0.0-0] (2020-02-02)
31
+ ## [5.0.0] (2020-03-14)
37
32
 
38
33
  ### Added
39
34
 
@@ -46,6 +41,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
46
41
  * display help for missing subcommand, by default ([#1088] [#1149])
47
42
  * combined short options as single argument may include boolean flags and value flag and value (e.g. `-a -b -p 80` can be written as `-abp80`) ([#1145])
48
43
  * `.parseOption()` includes short flag and long flag expansions ([#1145])
44
+ * `.helpInformation()` returns help text as a string, previously a private routine ([#1169])
45
+ * `.parse()` implicitly uses `process.argv` if arguments not specified ([#1172])
46
+ * optionally specify where `.parse()` arguments "from", if not following node conventions ([#512] [#1172])
47
+ * suggest help option along with unknown command error ([#1179])
48
+ * TypeScript definition for `commands` property of `Command` ([#1184])
49
+ * export `program` property ([#1195])
50
+ * `createCommand` factory method to simplify subclassing ([#1191])
49
51
 
50
52
  ### Fixed
51
53
 
@@ -72,6 +74,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
72
74
  * *Breaking* `.on('command:*', callback)` and other command events passed (changed) results from `.parseOptions`, i.e. operands and unknown ([#1138])
73
75
  * refactor Option from prototype to class ([#1133])
74
76
  * refactor Command from prototype to class ([#1159])
77
+ * changes to error handling ([#1165])
78
+ * throw for author error, not just display message
79
+ * preflight for variadic error
80
+ * add tips to missing subcommand executable
81
+ * TypeScript fluent return types changed to be more subclass friendly, return `this` rather than `Command` ([#1180])
82
+ * `.parseAsync` returns `Promise<this>` to be consistent with `.parse()` ([#1180])
83
+ * update dependencies
75
84
 
76
85
  ### Removed
77
86
 
@@ -87,6 +96,26 @@ If you use `program.args` or more complicated tests to detect a missing subcomma
87
96
 
88
97
  If you use `.command('*')` to add a default command, you may be be able to switch to `isDefault:true` with a named command.
89
98
 
99
+ ## [5.0.0-4] (2020-03-03)
100
+
101
+ (Released in 5.0.0)
102
+
103
+ ## [5.0.0-3] (2020-02-20)
104
+
105
+ (Released in 5.0.0)
106
+
107
+ ## [5.0.0-2] (2020-02-10)
108
+
109
+ (Released in 5.0.0)
110
+
111
+ ## [5.0.0-1] (2020-02-08)
112
+
113
+ (Released in 5.0.0)
114
+
115
+ ## [5.0.0-0] (2020-02-02)
116
+
117
+ (Released in 5.0.0)
118
+
90
119
  ## [4.1.1] (2020-02-02)
91
120
 
92
121
  ### Fixed
@@ -266,8 +295,10 @@ program
266
295
  [#432]: https://github.com/tj/commander.js/issues/432
267
296
  [#508]: https://github.com/tj/commander.js/issues/508
268
297
  [#512]: https://github.com/tj/commander.js/issues/512
298
+ [#531]: https://github.com/tj/commander.js/issues/531
269
299
  [#599]: https://github.com/tj/commander.js/issues/599
270
300
  [#611]: https://github.com/tj/commander.js/issues/611
301
+ [#645]: https://github.com/tj/commander.js/issues/645
271
302
  [#697]: https://github.com/tj/commander.js/issues/697
272
303
  [#742]: https://github.com/tj/commander.js/issues/742
273
304
  [#764]: https://github.com/tj/commander.js/issues/764
@@ -324,8 +355,21 @@ program
324
355
  [#1172]: https://github.com/tj/commander.js/pull/1172
325
356
  [#1179]: https://github.com/tj/commander.js/pull/1179
326
357
  [#1180]: https://github.com/tj/commander.js/pull/1180
358
+ [#1184]: https://github.com/tj/commander.js/pull/1184
359
+ [#1191]: https://github.com/tj/commander.js/pull/1191
360
+ [#1195]: https://github.com/tj/commander.js/pull/1195
361
+ [#1208]: https://github.com/tj/commander.js/pull/1208
362
+ [#1232]: https://github.com/tj/commander.js/pull/1232
363
+ [#1235]: https://github.com/tj/commander.js/pull/1235
364
+ [#1236]: https://github.com/tj/commander.js/pull/1236
365
+ [#1247]: https://github.com/tj/commander.js/pull/1247
366
+ [#1248]: https://github.com/tj/commander.js/pull/1248
327
367
 
328
368
  [Unreleased]: https://github.com/tj/commander.js/compare/master...develop
369
+ [5.1.0]: https://github.com/tj/commander.js/compare/v5.0.0..v5.1.0
370
+ [5.0.0]: https://github.com/tj/commander.js/compare/v4.1.1..v5.0.0
371
+ [5.0.0-4]: https://github.com/tj/commander.js/compare/v5.0.0-3..v5.0.0-4
372
+ [5.0.0-3]: https://github.com/tj/commander.js/compare/v5.0.0-2..v5.0.0-3
329
373
  [5.0.0-2]: https://github.com/tj/commander.js/compare/v5.0.0-1..v5.0.0-2
330
374
  [5.0.0-1]: https://github.com/tj/commander.js/compare/v5.0.0-0..v5.0.0-1
331
375
  [5.0.0-0]: https://github.com/tj/commander.js/compare/v4.1.1..v5.0.0-0
package/Readme.md CHANGED
@@ -36,6 +36,7 @@ Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
36
36
  - [.parse() and .parseAsync()](#parse-and-parseasync)
37
37
  - [Avoiding option name clashes](#avoiding-option-name-clashes)
38
38
  - [TypeScript](#typescript)
39
+ - [createCommand()](#createcommand)
39
40
  - [Node options such as `--harmony`](#node-options-such-as---harmony)
40
41
  - [Debugging stand-alone executable subcommands](#debugging-stand-alone-executable-subcommands)
41
42
  - [Override exit handling](#override-exit-handling)
@@ -56,15 +57,15 @@ Commander exports a global object which is convenient for quick programs.
56
57
  This is used in the examples in this README for brevity.
57
58
 
58
59
  ```js
59
- const program = require('commander');
60
+ const { program } = require('commander');
60
61
  program.version('0.0.1');
61
62
  ```
62
63
 
63
64
  For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local Command object to use.
64
65
 
65
66
  ```js
66
- const commander = require('commander');
67
- const program = new commander.Command();
67
+ const { Command } = require('commander');
68
+ const program = new Command();
68
69
  program.version('0.0.1');
69
70
  ```
70
71
 
@@ -88,7 +89,7 @@ Options on the command line are not positional, and can be specified before or a
88
89
  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.
89
90
 
90
91
  ```js
91
- const program = require('commander');
92
+ const { program } = require('commander');
92
93
 
93
94
  program
94
95
  .option('-d, --debug', 'output extra debugging')
@@ -126,7 +127,7 @@ pizza details:
126
127
  You can specify a default value for an option which takes a value.
127
128
 
128
129
  ```js
129
- const program = require('commander');
130
+ const { program } = require('commander');
130
131
 
131
132
  program
132
133
  .option('-c, --cheese <type>', 'add the specified type of cheese', 'blue');
@@ -152,7 +153,7 @@ If you define `--foo` first, adding `--no-foo` does not change the default value
152
153
  otherwise be. You can specify a default boolean value for a boolean flag and it can be overridden on command line.
153
154
 
154
155
  ```js
155
- const program = require('commander');
156
+ const { program } = require('commander');
156
157
 
157
158
  program
158
159
  .option('--no-sauce', 'Remove sauce')
@@ -179,7 +180,7 @@ You ordered a pizza with no sauce and no cheese
179
180
  You can specify an option which functions as a flag but may also take a value (declared using square brackets).
180
181
 
181
182
  ```js
182
- const program = require('commander');
183
+ const { program } = require('commander');
183
184
 
184
185
  program
185
186
  .option('-c, --cheese [type]', 'Add cheese with optional type');
@@ -210,7 +211,7 @@ This allows you to coerce the option value to the desired type, or accumulate va
210
211
  You can optionally specify the default/starting value for the option after the function.
211
212
 
212
213
  ```js
213
- const program = require('commander');
214
+ const { program } = require('commander');
214
215
 
215
216
  function myParseInt(value, dummyPrevious) {
216
217
  // parseInt takes a string and an optional radix
@@ -264,7 +265,7 @@ $ custom --list x,y,z
264
265
  You may specify a required (mandatory) option using `.requiredOption`. The option must have a value after parsing, usually specified on the command line, or perhaps from a default value (say from environment). The method is otherwise the same as `.option` in format, taking flags and description, and optional default value or custom processing.
265
266
 
266
267
  ```js
267
- const program = require('commander');
268
+ const { program } = require('commander');
268
269
 
269
270
  program
270
271
  .requiredOption('-c, --cheese <type>', 'pizza must have cheese');
@@ -299,7 +300,7 @@ program.version('0.0.1', '-v, --vers', 'output the current version');
299
300
 
300
301
  ## Commands
301
302
 
302
- You can specify (sub)commands for your top-level command using `.command()` or `.addCommand()`. There are two ways these can be implemented: using an action handler attached to the command, or as a stand-alone executable file (described in more detail later). The subcommands may be nested ([example](./examples/nestedCommands.js)).
303
+ You can specify (sub)commands using `.command()` or `.addCommand()`. There are two ways these can be implemented: using an action handler attached to the command, or as a stand-alone executable file (described in more detail later). The subcommands may be nested ([example](./examples/nestedCommands.js)).
303
304
 
304
305
  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...`.
305
306
 
@@ -318,25 +319,25 @@ program
318
319
  });
319
320
 
320
321
  // Command implemented using stand-alone executable file (description is second parameter to `.command`)
321
- // Returns top-level command for adding more commands.
322
+ // Returns `this` for adding more commands.
322
323
  program
323
324
  .command('start <service>', 'start named service')
324
325
  .command('stop [service]', 'stop named service, or all if no name supplied');
325
326
 
326
327
  // Command prepared separately.
327
- // Returns top-level command for adding more commands.
328
+ // Returns `this` for adding more commands.
328
329
  program
329
330
  .addCommand(build.makeBuildCommand());
330
331
  ```
331
332
 
332
- 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 ([example](./examples/defaultCommand.js)).
333
+ Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `true` for `opts.hidden` will remove the command from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified ([example](./examples/defaultCommand.js)).
333
334
 
334
335
  ### Specify the argument syntax
335
336
 
336
- 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.
337
+ You use `.arguments` to specify the arguments for the top-level command, and for subcommands they are usually included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required input. Square brackets (e.g. `[optional]`) indicate optional input.
337
338
 
338
339
  ```js
339
- const program = require('commander');
340
+ const { program } = require('commander');
340
341
 
341
342
  program
342
343
  .version('0.1.0')
@@ -360,7 +361,7 @@ console.log('environment:', envValue || "no environment given");
360
361
  append `...` to the argument name. For example:
361
362
 
362
363
  ```js
363
- const program = require('commander');
364
+ const { program } = require('commander');
364
365
 
365
366
  program
366
367
  .version('0.1.0')
@@ -386,7 +387,7 @@ The action handler gets passed a parameter for each argument you declared, and o
386
387
  command object itself. This command argument has the values for the command-specific options added as properties.
387
388
 
388
389
  ```js
389
- const program = require('commander');
390
+ const { program } = require('commander');
390
391
 
391
392
  program
392
393
  .command('rm <dir>')
@@ -423,7 +424,7 @@ You handle the options for an executable (sub)command in the executable, and don
423
424
 
424
425
  ```js
425
426
  // file: ./examples/pm
426
- const program = require('commander');
427
+ const { program } = require('commander');
427
428
 
428
429
  program
429
430
  .version('0.1.0')
@@ -557,9 +558,7 @@ program.on('option:verbose', function () {
557
558
  program.on('command:*', function (operands) {
558
559
  console.error(`error: unknown command '${operands[0]}'`);
559
560
  const availableCommands = program.commands.map(cmd => cmd.name());
560
- const suggestion = didYouMean(operands[0], availableCommands);
561
- if (suggestion)
562
- console.error(`Did you mean '${suggestion}'?`);
561
+ mySuggestBestMatch(operands[0], availableCommands);
563
562
  process.exitCode = 1;
564
563
  });
565
564
  ```
@@ -632,6 +631,19 @@ If you use `ts-node` and stand-alone executable subcommands written as `.ts` fi
632
631
  node -r ts-node/register pm.ts
633
632
  ```
634
633
 
634
+ ### createCommand()
635
+
636
+ This factory function creates a new command. It is exported and may be used instead of using `new`, like:
637
+
638
+ ```js
639
+ const { createCommand } = require('commander');
640
+ const program = createCommand();
641
+ ```
642
+
643
+ `createCommand` is also a method of the Command object, and creates a new command rather than a subcommand. This gets used internally
644
+ when creating subcommands using `.command()`, and you may override it to
645
+ customise the new subcommand (examples using [subclass](./examples/custom-command-class.js) and [function](./examples/custom-command-function.js)).
646
+
635
647
  ### Node options such as `--harmony`
636
648
 
637
649
  You can enable `--harmony` option in two ways:
@@ -669,7 +681,7 @@ try {
669
681
  ## Examples
670
682
 
671
683
  ```js
672
- const program = require('commander');
684
+ const { program } = require('commander');
673
685
 
674
686
  program
675
687
  .version('0.1.0')
@@ -713,7 +725,7 @@ More Demos can be found in the [examples](https://github.com/tj/commander.js/tre
713
725
 
714
726
  ## Support
715
727
 
716
- Commander 4.x is supported on Node 8 and above, and is likely to work with Node 6 but not tested.
728
+ Commander 5.x is fully supported on Long Term Support versions of Node, and is likely to work with Node 6 but not tested.
717
729
  (For versions of Node below Node 6, use Commander 3.x or 2.x.)
718
730
 
719
731
  The main forum for free and community support is the project [Issues](https://github.com/tj/commander.js/issues) on GitHub.
package/index.js CHANGED
@@ -7,6 +7,8 @@ const spawn = require('child_process').spawn;
7
7
  const path = require('path');
8
8
  const fs = require('fs');
9
9
 
10
+ // @ts-check
11
+
10
12
  class Option {
11
13
  /**
12
14
  * Initialize a new `Option` with the given `flags` and `description`.
@@ -22,10 +24,11 @@ class Option {
22
24
  this.optional = flags.indexOf('[') >= 0; // A value is optional when the option is specified.
23
25
  this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line.
24
26
  this.negate = flags.indexOf('-no-') !== -1;
25
- flags = flags.split(/[ ,|]+/);
26
- if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();
27
- this.long = flags.shift();
27
+ const flagParts = flags.split(/[ ,|]+/);
28
+ if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1])) this.short = flagParts.shift();
29
+ this.long = flagParts.shift();
28
30
  this.description = description || '';
31
+ this.defaultValue = undefined;
29
32
  }
30
33
 
31
34
  /**
@@ -83,6 +86,7 @@ class CommanderError extends Error {
83
86
  this.name = this.constructor.name;
84
87
  this.code = code;
85
88
  this.exitCode = exitCode;
89
+ this.nestedError = undefined;
86
90
  }
87
91
  }
88
92
 
@@ -98,6 +102,7 @@ class Command extends EventEmitter {
98
102
  super();
99
103
  this.commands = [];
100
104
  this.options = [];
105
+ this.parent = null;
101
106
  this._allowUnknownOption = false;
102
107
  this._args = [];
103
108
  this.rawArgs = null;
@@ -112,8 +117,9 @@ class Command extends EventEmitter {
112
117
  this._executableFile = null; // custom name for executable
113
118
  this._defaultCommandName = null;
114
119
  this._exitCallback = null;
120
+ this._aliases = [];
115
121
 
116
- this._noHelp = false;
122
+ this._hidden = false;
117
123
  this._helpFlags = '-h, --help';
118
124
  this._helpDescription = 'display help for command';
119
125
  this._helpShortFlag = '-h';
@@ -147,7 +153,7 @@ class Command extends EventEmitter {
147
153
  * @param {string} nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
148
154
  * @param {Object|string} [actionOptsOrExecDesc] - configuration options (for action), or description (for executable)
149
155
  * @param {Object} [execOpts] - configuration options (for executable)
150
- * @return {Command} returns new command for action handler, or top-level command for executable command
156
+ * @return {Command} returns new command for action handler, or `this` for executable command
151
157
  * @api public
152
158
  */
153
159
 
@@ -160,7 +166,7 @@ class Command extends EventEmitter {
160
166
  }
161
167
  opts = opts || {};
162
168
  const args = nameAndArgs.split(/ +/);
163
- const cmd = new Command(args.shift());
169
+ const cmd = this.createCommand(args.shift());
164
170
 
165
171
  if (desc) {
166
172
  cmd.description(desc);
@@ -168,7 +174,7 @@ class Command extends EventEmitter {
168
174
  }
169
175
  if (opts.isDefault) this._defaultCommandName = cmd._name;
170
176
 
171
- cmd._noHelp = !!opts.noHelp;
177
+ cmd._hidden = !!(opts.noHelp || opts.hidden);
172
178
  cmd._helpFlags = this._helpFlags;
173
179
  cmd._helpDescription = this._helpDescription;
174
180
  cmd._helpShortFlag = this._helpShortFlag;
@@ -189,15 +195,33 @@ class Command extends EventEmitter {
189
195
  return cmd;
190
196
  };
191
197
 
198
+ /**
199
+ * Factory routine to create a new unattached command.
200
+ *
201
+ * See .command() for creating an attached subcommand, which uses this routine to
202
+ * create the command. You can override createCommand to customise subcommands.
203
+ *
204
+ * @param {string} [name]
205
+ * @return {Command} new command
206
+ * @api public
207
+ */
208
+
209
+ createCommand(name) {
210
+ return new Command(name);
211
+ };
212
+
192
213
  /**
193
214
  * Add a prepared subcommand.
194
215
  *
216
+ * See .command() for creating an attached subcommand which inherits settings from its parent.
217
+ *
195
218
  * @param {Command} cmd - new subcommand
196
- * @return {Command} parent command for chaining
219
+ * @param {Object} [opts] - configuration options
220
+ * @return {Command} `this` command for chaining
197
221
  * @api public
198
222
  */
199
223
 
200
- addCommand(cmd) {
224
+ addCommand(cmd, opts) {
201
225
  if (!cmd._name) throw new Error('Command passed to .addCommand() must have a name');
202
226
 
203
227
  // To keep things simple, block automatic name generation for deeply nested executables.
@@ -212,13 +236,17 @@ class Command extends EventEmitter {
212
236
  }
213
237
  checkExplicitNames(cmd.commands);
214
238
 
239
+ opts = opts || {};
240
+ if (opts.isDefault) this._defaultCommandName = cmd._name;
241
+ if (opts.noHelp || opts.hidden) cmd._hidden = true; // modifying passed command due to existing implementation
242
+
215
243
  this.commands.push(cmd);
216
244
  cmd.parent = this;
217
245
  return this;
218
246
  };
219
247
 
220
248
  /**
221
- * Define argument syntax for the top-level command.
249
+ * Define argument syntax for the command.
222
250
  *
223
251
  * @api public
224
252
  */
@@ -234,7 +262,7 @@ class Command extends EventEmitter {
234
262
  * addHelpCommand(false); // force off
235
263
  * addHelpCommand('help [cmd]', 'display help for [cmd]'); // force on with custom detais
236
264
  *
237
- * @return {Command} for chaining
265
+ * @return {Command} `this` command for chaining
238
266
  * @api public
239
267
  */
240
268
 
@@ -270,7 +298,7 @@ class Command extends EventEmitter {
270
298
  * For example `["[type]"]` becomes `[{ required: false, name: 'type' }]`.
271
299
  *
272
300
  * @param {Array} args
273
- * @return {Command} for chaining
301
+ * @return {Command} `this` command for chaining
274
302
  * @api private
275
303
  */
276
304
 
@@ -313,7 +341,7 @@ class Command extends EventEmitter {
313
341
  * Register callback to use as replacement for calling process.exit.
314
342
  *
315
343
  * @param {Function} [fn] optional callback which will be passed a CommanderError, defaults to throwing
316
- * @return {Command} for chaining
344
+ * @return {Command} `this` command for chaining
317
345
  * @api public
318
346
  */
319
347
 
@@ -363,7 +391,7 @@ class Command extends EventEmitter {
363
391
  * });
364
392
  *
365
393
  * @param {Function} fn
366
- * @return {Command} for chaining
394
+ * @return {Command} `this` command for chaining
367
395
  * @api public
368
396
  */
369
397
 
@@ -402,7 +430,7 @@ class Command extends EventEmitter {
402
430
  * @param {string} description
403
431
  * @param {Function|*} [fn] - custom option processing function or default vaue
404
432
  * @param {*} [defaultValue]
405
- * @return {Command} for chaining
433
+ * @return {Command} `this` command for chaining
406
434
  * @api private
407
435
  */
408
436
 
@@ -520,7 +548,7 @@ class Command extends EventEmitter {
520
548
  * @param {string} description
521
549
  * @param {Function|*} [fn] - custom option processing function or default vaue
522
550
  * @param {*} [defaultValue]
523
- * @return {Command} for chaining
551
+ * @return {Command} `this` command for chaining
524
552
  * @api public
525
553
  */
526
554
 
@@ -538,7 +566,7 @@ class Command extends EventEmitter {
538
566
  * @param {string} description
539
567
  * @param {Function|*} [fn] - custom option processing function or default vaue
540
568
  * @param {*} [defaultValue]
541
- * @return {Command} for chaining
569
+ * @return {Command} `this` command for chaining
542
570
  * @api public
543
571
  */
544
572
 
@@ -554,7 +582,7 @@ class Command extends EventEmitter {
554
582
  * @api public
555
583
  */
556
584
  allowUnknownOption(arg) {
557
- this._allowUnknownOption = arguments.length === 0 || arg;
585
+ this._allowUnknownOption = (arg === undefined) || arg;
558
586
  return this;
559
587
  };
560
588
 
@@ -563,7 +591,7 @@ class Command extends EventEmitter {
563
591
  * or store separately (specify false). In both cases the option values can be accessed using .opts().
564
592
  *
565
593
  * @param {boolean} value
566
- * @return {Command} Command for chaining
594
+ * @return {Command} `this` command for chaining
567
595
  * @api public
568
596
  */
569
597
 
@@ -580,7 +608,7 @@ class Command extends EventEmitter {
580
608
  * or just the options (specify false).
581
609
  *
582
610
  * @param {boolean} value
583
- * @return {Command} Command for chaining
611
+ * @return {Command} `this` command for chaining
584
612
  * @api public
585
613
  */
586
614
 
@@ -634,8 +662,8 @@ class Command extends EventEmitter {
634
662
  *
635
663
  * @param {string[]} [argv] - optional, defaults to process.argv
636
664
  * @param {Object} [parseOptions] - optionally specify style of options with from: node/user/electron
637
- * @param {string} parseOptions.from - where the args are from: 'node', 'user', 'electron'
638
- * @return {Command} for chaining
665
+ * @param {string} [parseOptions.from] - where the args are from: 'node', 'user', 'electron'
666
+ * @return {Command} `this` command for chaining
639
667
  * @api public
640
668
  */
641
669
 
@@ -648,6 +676,7 @@ class Command extends EventEmitter {
648
676
  // Default to using process.argv
649
677
  if (argv === undefined) {
650
678
  argv = process.argv;
679
+ // @ts-ignore
651
680
  if (process.versions && process.versions.electron) {
652
681
  parseOptions.from = 'electron';
653
682
  }
@@ -663,6 +692,7 @@ class Command extends EventEmitter {
663
692
  userArgs = argv.slice(2);
664
693
  break;
665
694
  case 'electron':
695
+ // @ts-ignore
666
696
  if (process.defaultApp) {
667
697
  this._scriptPath = argv[1];
668
698
  userArgs = argv.slice(2);
@@ -780,6 +810,7 @@ class Command extends EventEmitter {
780
810
 
781
811
  const signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
782
812
  signals.forEach((signal) => {
813
+ // @ts-ignore
783
814
  process.on(signal, () => {
784
815
  if (proc.killed === false && proc.exitCode === null) {
785
816
  proc.kill(signal);
@@ -798,11 +829,13 @@ class Command extends EventEmitter {
798
829
  });
799
830
  }
800
831
  proc.on('error', (err) => {
832
+ // @ts-ignore
801
833
  if (err.code === 'ENOENT') {
802
834
  const executableMissing = `'${bin}' does not exist
803
835
  - if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
804
836
  - if the default executable name is not suitable, use the executableFile option to supply a custom name`;
805
837
  throw new Error(executableMissing);
838
+ // @ts-ignore
806
839
  } else if (err.code === 'EACCES') {
807
840
  throw new Error(`'${bin}' not executable`);
808
841
  }
@@ -904,7 +937,7 @@ class Command extends EventEmitter {
904
937
  */
905
938
  _findCommand(name) {
906
939
  if (!name) return undefined;
907
- return this.commands.find(cmd => cmd._name === name || cmd._alias === name);
940
+ return this.commands.find(cmd => cmd._name === name || cmd._aliases.includes(name));
908
941
  };
909
942
 
910
943
  /**
@@ -1119,7 +1152,6 @@ class Command extends EventEmitter {
1119
1152
  /**
1120
1153
  * Unknown command.
1121
1154
  *
1122
- * @param {string} flag
1123
1155
  * @api private
1124
1156
  */
1125
1157
 
@@ -1145,12 +1177,12 @@ class Command extends EventEmitter {
1145
1177
  * @param {string} str
1146
1178
  * @param {string} [flags]
1147
1179
  * @param {string} [description]
1148
- * @return {Command} for chaining
1180
+ * @return {this | string} `this` command for chaining, or version string if no arguments
1149
1181
  * @api public
1150
1182
  */
1151
1183
 
1152
1184
  version(str, flags, description) {
1153
- if (arguments.length === 0) return this._version;
1185
+ if (str === undefined) return this._version;
1154
1186
  this._version = str;
1155
1187
  flags = flags || '-V, --version';
1156
1188
  description = description || 'output the version number';
@@ -1169,36 +1201,57 @@ class Command extends EventEmitter {
1169
1201
  *
1170
1202
  * @param {string} str
1171
1203
  * @param {Object} [argsDescription]
1172
- * @return {String|Command}
1204
+ * @return {string|Command}
1173
1205
  * @api public
1174
1206
  */
1175
1207
 
1176
1208
  description(str, argsDescription) {
1177
- if (arguments.length === 0) return this._description;
1209
+ if (str === undefined && argsDescription === undefined) return this._description;
1178
1210
  this._description = str;
1179
1211
  this._argsDescription = argsDescription;
1180
1212
  return this;
1181
1213
  };
1182
1214
 
1183
1215
  /**
1184
- * Set an alias for the command
1216
+ * Set an alias for the command.
1185
1217
  *
1186
- * @param {string} alias
1187
- * @return {String|Command}
1218
+ * You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.
1219
+ *
1220
+ * @param {string} [alias]
1221
+ * @return {string|Command}
1188
1222
  * @api public
1189
1223
  */
1190
1224
 
1191
1225
  alias(alias) {
1226
+ if (alias === undefined) return this._aliases[0]; // just return first, for backwards compatibility
1227
+
1192
1228
  let command = this;
1193
- if (this.commands.length !== 0) {
1229
+ if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
1230
+ // assume adding alias for last added executable subcommand, rather than this
1194
1231
  command = this.commands[this.commands.length - 1];
1195
1232
  }
1196
1233
 
1197
- if (arguments.length === 0) return command._alias;
1198
-
1199
1234
  if (alias === command._name) throw new Error('Command alias can\'t be the same as its name');
1200
1235
 
1201
- command._alias = alias;
1236
+ command._aliases.push(alias);
1237
+ return this;
1238
+ };
1239
+
1240
+ /**
1241
+ * Set aliases for the command.
1242
+ *
1243
+ * Only the first alias is shown in the auto-generated help.
1244
+ *
1245
+ * @param {string[]} [aliases]
1246
+ * @return {string[]|Command}
1247
+ * @api public
1248
+ */
1249
+
1250
+ aliases(aliases) {
1251
+ // Getter for the array of aliases is the main reason for having aliases() in addition to alias().
1252
+ if (aliases === undefined) return this._aliases;
1253
+
1254
+ aliases.forEach((alias) => this.alias(alias));
1202
1255
  return this;
1203
1256
  };
1204
1257
 
@@ -1211,17 +1264,18 @@ class Command extends EventEmitter {
1211
1264
  */
1212
1265
 
1213
1266
  usage(str) {
1214
- const args = this._args.map((arg) => {
1215
- return humanReadableArgName(arg);
1216
- });
1267
+ if (str === undefined) {
1268
+ if (this._usage) return this._usage;
1217
1269
 
1218
- const usage = '[options]' +
1219
- (this.commands.length ? ' [command]' : '') +
1220
- (this._args.length ? ' ' + args.join(' ') : '');
1270
+ const args = this._args.map((arg) => {
1271
+ return humanReadableArgName(arg);
1272
+ });
1273
+ return '[options]' +
1274
+ (this.commands.length ? ' [command]' : '') +
1275
+ (this._args.length ? ' ' + args.join(' ') : '');
1276
+ }
1221
1277
 
1222
- if (arguments.length === 0) return this._usage || usage;
1223
1278
  this._usage = str;
1224
-
1225
1279
  return this;
1226
1280
  };
1227
1281
 
@@ -1234,7 +1288,7 @@ class Command extends EventEmitter {
1234
1288
  */
1235
1289
 
1236
1290
  name(str) {
1237
- if (arguments.length === 0) return this._name;
1291
+ if (str === undefined) return this._name;
1238
1292
  this._name = str;
1239
1293
  return this;
1240
1294
  };
@@ -1248,7 +1302,7 @@ class Command extends EventEmitter {
1248
1302
 
1249
1303
  prepareCommands() {
1250
1304
  const commandDetails = this.commands.filter((cmd) => {
1251
- return !cmd._noHelp;
1305
+ return !cmd._hidden;
1252
1306
  }).map((cmd) => {
1253
1307
  const args = cmd._args.map((arg) => {
1254
1308
  return humanReadableArgName(arg);
@@ -1256,7 +1310,7 @@ class Command extends EventEmitter {
1256
1310
 
1257
1311
  return [
1258
1312
  cmd._name +
1259
- (cmd._alias ? '|' + cmd._alias : '') +
1313
+ (cmd._aliases[0] ? '|' + cmd._aliases[0] : '') +
1260
1314
  (cmd.options.length ? ' [options]' : '') +
1261
1315
  (args ? ' ' + args : ''),
1262
1316
  cmd._description
@@ -1347,17 +1401,33 @@ class Command extends EventEmitter {
1347
1401
 
1348
1402
  optionHelp() {
1349
1403
  const width = this.padWidth();
1350
-
1351
1404
  const columns = process.stdout.columns || 80;
1352
1405
  const descriptionWidth = columns - width - 4;
1406
+ function padOptionDetails(flags, description) {
1407
+ return pad(flags, width) + ' ' + optionalWrap(description, descriptionWidth, width + 2);
1408
+ };
1353
1409
 
1354
- // Append the help information
1355
- return this.options.map((option) => {
1410
+ // Explicit options (including version)
1411
+ const help = this.options.map((option) => {
1356
1412
  const fullDesc = option.description +
1357
1413
  ((!option.negate && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');
1358
- return pad(option.flags, width) + ' ' + optionalWrap(fullDesc, descriptionWidth, width + 2);
1359
- }).concat([pad(this._helpFlags, width) + ' ' + optionalWrap(this._helpDescription, descriptionWidth, width + 2)])
1360
- .join('\n');
1414
+ return padOptionDetails(option.flags, fullDesc);
1415
+ });
1416
+
1417
+ // Implicit help
1418
+ const showShortHelpFlag = this._helpShortFlag && !this._findOption(this._helpShortFlag);
1419
+ const showLongHelpFlag = !this._findOption(this._helpLongFlag);
1420
+ if (showShortHelpFlag || showLongHelpFlag) {
1421
+ let helpFlags = this._helpFlags;
1422
+ if (!showShortHelpFlag) {
1423
+ helpFlags = this._helpLongFlag;
1424
+ } else if (!showLongHelpFlag) {
1425
+ helpFlags = this._helpShortFlag;
1426
+ }
1427
+ help.push(padOptionDetails(helpFlags, this._helpDescription));
1428
+ }
1429
+
1430
+ return help.join('\n');
1361
1431
  };
1362
1432
 
1363
1433
  /**
@@ -1416,8 +1486,8 @@ class Command extends EventEmitter {
1416
1486
  }
1417
1487
 
1418
1488
  let cmdName = this._name;
1419
- if (this._alias) {
1420
- cmdName = cmdName + '|' + this._alias;
1489
+ if (this._aliases[0]) {
1490
+ cmdName = cmdName + '|' + this._aliases[0];
1421
1491
  }
1422
1492
  let parentCmdNames = '';
1423
1493
  for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
@@ -1474,7 +1544,7 @@ class Command extends EventEmitter {
1474
1544
  *
1475
1545
  * @param {string} [flags]
1476
1546
  * @param {string} [description]
1477
- * @return {Command}
1547
+ * @return {Command} `this` command for chaining
1478
1548
  * @api public
1479
1549
  */
1480
1550
 
@@ -1484,6 +1554,7 @@ class Command extends EventEmitter {
1484
1554
 
1485
1555
  const splitFlags = this._helpFlags.split(/[ ,|]+/);
1486
1556
 
1557
+ this._helpShortFlag = undefined;
1487
1558
  if (splitFlags.length > 1) this._helpShortFlag = splitFlags.shift();
1488
1559
 
1489
1560
  this._helpLongFlag = splitFlags.shift();
@@ -1523,6 +1594,7 @@ class Command extends EventEmitter {
1523
1594
  */
1524
1595
 
1525
1596
  exports = module.exports = new Command();
1597
+ exports.program = exports; // More explicit access to global command.
1526
1598
 
1527
1599
  /**
1528
1600
  * Expose classes
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "commander",
3
- "version": "5.0.0-2",
3
+ "version": "5.1.0",
4
4
  "description": "the complete solution for node.js command-line programs",
5
5
  "keywords": [
6
6
  "commander",
7
7
  "command",
8
8
  "option",
9
- "parser"
9
+ "parser",
10
+ "cli",
11
+ "argument",
12
+ "args",
13
+ "argv"
10
14
  ],
11
15
  "author": "TJ Holowaychuk <tj@vision-media.ca>",
12
16
  "license": "MIT",
@@ -16,6 +20,7 @@
16
20
  },
17
21
  "scripts": {
18
22
  "lint": "eslint index.js \"tests/**/*.js\"",
23
+ "typescript-lint": "eslint typings/*.ts",
19
24
  "test": "jest && npm run test-typings",
20
25
  "test-typings": "tsc -p tsconfig.json"
21
26
  },
@@ -26,12 +31,14 @@
26
31
  ],
27
32
  "dependencies": {},
28
33
  "devDependencies": {
29
- "@types/jest": "^25.1.1",
30
- "@types/node": "^12.12.26",
34
+ "@types/jest": "^25.2.1",
35
+ "@types/node": "^12.12.36",
36
+ "@typescript-eslint/eslint-plugin": "^2.29.0",
31
37
  "eslint": "^6.8.0",
32
- "eslint-plugin-jest": "^23.6.0",
33
- "jest": "^25.1.0",
34
- "standard": "^14.3.1",
38
+ "eslint-config-standard-with-typescript": "^15.0.1",
39
+ "eslint-plugin-jest": "^23.8.2",
40
+ "jest": "^25.4.0",
41
+ "standard": "^14.3.3",
35
42
  "typescript": "^3.7.5"
36
43
  },
37
44
  "typings": "typings/index.d.ts",
@@ -9,7 +9,7 @@ declare namespace commander {
9
9
  message: string;
10
10
  nestedError?: string;
11
11
  }
12
- type CommanderErrorConstructor = { new (exitCode: number, code: string, message: string): CommanderError };
12
+ type CommanderErrorConstructor = new (exitCode: number, code: string, message: string) => CommanderError;
13
13
 
14
14
  interface Option {
15
15
  flags: string;
@@ -21,10 +21,10 @@ declare namespace commander {
21
21
  long: string;
22
22
  description: string;
23
23
  }
24
- type OptionConstructor = { new (flags: string, description?: string): Option };
24
+ type OptionConstructor = new (flags: string, description?: string) => Option;
25
25
 
26
26
  interface ParseOptions {
27
- from: "node" | "electron" | "user";
27
+ from: 'node' | 'electron' | 'user';
28
28
  }
29
29
 
30
30
  interface Command {
@@ -32,22 +32,24 @@ declare namespace commander {
32
32
 
33
33
  args: string[];
34
34
 
35
+ commands: Command[];
36
+
35
37
  /**
36
- * Set the program version to `str`.
38
+ * Set the program version to `str`.
37
39
  *
38
40
  * This method auto-registers the "-V, --version" flag
39
41
  * which will print the version number when passed.
40
- *
42
+ *
41
43
  * You can optionally supply the flags and description to override the defaults.
42
44
  */
43
45
  version(str: string, flags?: string, description?: string): this;
44
46
 
45
47
  /**
46
48
  * Define a command, implemented using an action handler.
47
- *
49
+ *
48
50
  * @remarks
49
51
  * The command description is supplied using `.description`, not as a parameter to `.command`.
50
- *
52
+ *
51
53
  * @example
52
54
  * ```ts
53
55
  * program
@@ -57,43 +59,53 @@ declare namespace commander {
57
59
  * console.log('clone command called');
58
60
  * });
59
61
  * ```
60
- *
62
+ *
61
63
  * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
62
64
  * @param opts - configuration options
63
65
  * @returns new command
64
66
  */
65
- command(nameAndArgs: string, opts?: CommandOptions): Command;
67
+ command(nameAndArgs: string, opts?: CommandOptions): ReturnType<this['createCommand']>;
66
68
  /**
67
69
  * Define a command, implemented in a separate executable file.
68
- *
70
+ *
69
71
  * @remarks
70
72
  * The command description is supplied as the second parameter to `.command`.
71
- *
73
+ *
72
74
  * @example
73
75
  * ```ts
74
76
  * program
75
77
  * .command('start <service>', 'start named service')
76
78
  * .command('stop [service]', 'stop named serice, or all if no name supplied');
77
79
  * ```
78
- *
80
+ *
79
81
  * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
80
82
  * @param description - description of executable command
81
83
  * @param opts - configuration options
82
- * @returns top level command for chaining more command definitions
84
+ * @returns `this` command for chaining
85
+ */
86
+ command(nameAndArgs: string, description: string, opts?: commander.ExecutableCommandOptions): this;
87
+
88
+ /**
89
+ * Factory routine to create a new unattached command.
90
+ *
91
+ * See .command() for creating an attached subcommand, which uses this routine to
92
+ * create the command. You can override createCommand to customise subcommands.
83
93
  */
84
- command(nameAndArgs: string, description: string, opts?: commander.CommandOptions): this;
94
+ createCommand(name?: string): Command;
85
95
 
86
96
  /**
87
97
  * Add a prepared subcommand.
88
- *
89
- * @returns parent command for chaining
98
+ *
99
+ * See .command() for creating an attached subcommand which inherits settings from its parent.
100
+ *
101
+ * @returns `this` command for chaining
90
102
  */
91
- addCommand(cmd: Command): this;
103
+ addCommand(cmd: Command, opts?: CommandOptions): this;
92
104
 
93
105
  /**
94
- * Define argument syntax for the top-level command.
106
+ * Define argument syntax for command.
95
107
  *
96
- * @returns Command for chaining
108
+ * @returns `this` command for chaining
97
109
  */
98
110
  arguments(desc: string): this;
99
111
 
@@ -113,7 +125,7 @@ declare namespace commander {
113
125
  * // output help here
114
126
  * });
115
127
  *
116
- * @returns Command for chaining
128
+ * @returns `this` command for chaining
117
129
  */
118
130
  action(fn: (...args: any[]) => void | Promise<void>): this;
119
131
 
@@ -157,7 +169,7 @@ declare namespace commander {
157
169
  * // optional argument
158
170
  * program.option('-c, --cheese [type]', 'add cheese [marble]');
159
171
  *
160
- * @returns Command for chaining
172
+ * @returns `this` command for chaining
161
173
  */
162
174
  option(flags: string, description?: string, defaultValue?: string | boolean): this;
163
175
  option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;
@@ -173,20 +185,19 @@ declare namespace commander {
173
185
  requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;
174
186
  requiredOption<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this;
175
187
 
176
-
177
188
  /**
178
189
  * Whether to store option values as properties on command object,
179
190
  * or store separately (specify false). In both cases the option values can be accessed using .opts().
180
191
  *
181
- * @return Command for chaining
192
+ * @returns `this` command for chaining
182
193
  */
183
194
  storeOptionsAsProperties(value?: boolean): this;
184
195
 
185
196
  /**
186
197
  * Whether to pass command to action handler,
187
198
  * or just the options (specify false).
188
- *
189
- * @return Command for chaining
199
+ *
200
+ * @returns `this` command for chaining
190
201
  */
191
202
  passCommandToAction(value?: boolean): this;
192
203
 
@@ -194,13 +205,13 @@ declare namespace commander {
194
205
  * Allow unknown options on the command line.
195
206
  *
196
207
  * @param [arg] if `true` or omitted, no error will be thrown for unknown options.
197
- * @returns Command for chaining
208
+ * @returns `this` command for chaining
198
209
  */
199
210
  allowUnknownOption(arg?: boolean): this;
200
211
 
201
212
  /**
202
213
  * Parse `argv`, setting options and invoking commands when defined.
203
- *
214
+ *
204
215
  * The default expectation is that the arguments are from node and have the application as argv[0]
205
216
  * and the script being run in argv[1], with user parameters after that.
206
217
  *
@@ -209,16 +220,16 @@ declare namespace commander {
209
220
  * program.parse(process.argv);
210
221
  * program.parse(); // implicitly use process.argv and auto-detect node vs electron conventions
211
222
  * program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
212
- *
213
- * @returns Command for chaining
223
+ *
224
+ * @returns `this` command for chaining
214
225
  */
215
226
  parse(argv?: string[], options?: ParseOptions): this;
216
227
 
217
228
  /**
218
229
  * Parse `argv`, setting options and invoking commands when defined.
219
- *
230
+ *
220
231
  * Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.
221
- *
232
+ *
222
233
  * The default expectation is that the arguments are from node and have the application as argv[0]
223
234
  * and the script being run in argv[1], with user parameters after that.
224
235
  *
@@ -252,8 +263,8 @@ declare namespace commander {
252
263
 
253
264
  /**
254
265
  * Set the description.
255
- *
256
- * @returns Command for chaining
266
+ *
267
+ * @returns `this` command for chaining
257
268
  */
258
269
  description(str: string, argsDescription?: {[argName: string]: string}): this;
259
270
  /**
@@ -263,8 +274,10 @@ declare namespace commander {
263
274
 
264
275
  /**
265
276
  * Set an alias for the command.
266
- *
267
- * @returns Command for chaining
277
+ *
278
+ * You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.
279
+ *
280
+ * @returns `this` command for chaining
268
281
  */
269
282
  alias(alias: string): this;
270
283
  /**
@@ -272,10 +285,23 @@ declare namespace commander {
272
285
  */
273
286
  alias(): string;
274
287
 
288
+ /**
289
+ * Set aliases for the command.
290
+ *
291
+ * Only the first alias is shown in the auto-generated help.
292
+ *
293
+ * @returns `this` command for chaining
294
+ */
295
+ aliases(aliases: string[]): this;
296
+ /**
297
+ * Get aliases for the command.
298
+ */
299
+ aliases(): string[];
300
+
275
301
  /**
276
302
  * Set the command usage.
277
- *
278
- * @returns Command for chaining
303
+ *
304
+ * @returns `this` command for chaining
279
305
  */
280
306
  usage(str: string): this;
281
307
  /**
@@ -285,8 +311,8 @@ declare namespace commander {
285
311
 
286
312
  /**
287
313
  * Set the name of the command.
288
- *
289
- * @returns Command for chaining
314
+ *
315
+ * @returns `this` command for chaining
290
316
  */
291
317
  name(str: string): this;
292
318
  /**
@@ -306,21 +332,21 @@ declare namespace commander {
306
332
  * Return command help documentation.
307
333
  */
308
334
  helpInformation(): string;
309
-
335
+
310
336
  /**
311
337
  * You can pass in flags and a description to override the help
312
338
  * flags and help description for your command.
313
339
  */
314
340
  helpOption(flags?: string, description?: string): this;
315
341
 
316
- /**
342
+ /**
317
343
  * Output help information and exit.
318
344
  */
319
345
  help(cb?: (str: string) => string): never;
320
346
 
321
347
  /**
322
348
  * Add a listener (callback) for when events occur. (Implemented using EventEmitter.)
323
- *
349
+ *
324
350
  * @example
325
351
  * program
326
352
  * .on('--help', () -> {
@@ -329,27 +355,32 @@ declare namespace commander {
329
355
  */
330
356
  on(event: string | symbol, listener: (...args: any[]) => void): this;
331
357
  }
332
- type CommandConstructor = { new (name?: string): Command };
358
+ type CommandConstructor = new (name?: string) => Command;
333
359
 
360
+ interface CommandOptions {
361
+ noHelp?: boolean; // old name for hidden
362
+ hidden?: boolean;
363
+ isDefault?: boolean;
364
+ }
365
+ interface ExecutableCommandOptions extends CommandOptions {
366
+ executableFile?: string;
367
+ }
334
368
 
335
- interface CommandOptions {
336
- noHelp?: boolean;
337
- isDefault?: boolean;
338
- executableFile?: string;
339
- }
340
-
341
- interface ParseOptionsResult {
342
- operands: string[];
343
- unknown: string[];
344
- }
369
+ interface ParseOptionsResult {
370
+ operands: string[];
371
+ unknown: string[];
372
+ }
345
373
 
346
- interface CommanderStatic extends Command {
347
- Command: CommandConstructor;
348
- Option: OptionConstructor;
349
- CommanderError:CommanderErrorConstructor;
350
- }
374
+ interface CommanderStatic extends Command {
375
+ program: Command;
376
+ Command: CommandConstructor;
377
+ Option: OptionConstructor;
378
+ CommanderError: CommanderErrorConstructor;
379
+ }
351
380
 
352
381
  }
353
382
 
383
+ // Declaring namespace AND global
384
+ // eslint-disable-next-line no-redeclare
354
385
  declare const commander: commander.CommanderStatic;
355
386
  export = commander;