commander 14.0.1 → 14.0.3
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/Readme.md +82 -65
- package/lib/command.js +1 -1
- package/package-support.json +4 -1
- package/package.json +1 -1
- package/typings/index.d.ts +2 -2
package/Readme.md
CHANGED
|
@@ -154,7 +154,7 @@ This is used in the examples in this README for brevity.
|
|
|
154
154
|
const { program } = require('commander');
|
|
155
155
|
```
|
|
156
156
|
|
|
157
|
-
For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local Command object to use.
|
|
157
|
+
For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local `Command` object to use.
|
|
158
158
|
|
|
159
159
|
```js
|
|
160
160
|
// CommonJS (.cjs)
|
|
@@ -176,8 +176,7 @@ const program = new Command();
|
|
|
176
176
|
|
|
177
177
|
## Options
|
|
178
178
|
|
|
179
|
-
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
|
|
180
|
-
single characters, you may also have two long options. Examples:
|
|
179
|
+
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, a space, or a vertical bar (`|`). To allow a wider range of short-ish flags than just single characters, you may also have two long options.
|
|
181
180
|
|
|
182
181
|
```js
|
|
183
182
|
program
|
|
@@ -188,9 +187,9 @@ program
|
|
|
188
187
|
|
|
189
188
|
The parsed options can be accessed by calling `.opts()` on a `Command` object, and are passed to the action handler.
|
|
190
189
|
|
|
191
|
-
Multi-word options
|
|
190
|
+
Multi-word options like `--template-engine` are normalized to camelCase option names, resulting in properties such as `program.opts().templateEngine`.
|
|
192
191
|
|
|
193
|
-
An option and its option-argument can be separated by a space, or combined into the same argument. The option-argument can follow the short option directly or follow an `=` for a long option.
|
|
192
|
+
An option and its option-argument can be separated by a space, or combined into the same argument. The option-argument can follow the short option directly, or follow an `=` for a long option.
|
|
194
193
|
|
|
195
194
|
```sh
|
|
196
195
|
serve -p 80
|
|
@@ -245,7 +244,7 @@ pizza details:
|
|
|
245
244
|
```
|
|
246
245
|
|
|
247
246
|
Multiple boolean short options may be combined following the dash, and may be followed by a single short option taking a value.
|
|
248
|
-
For example `-d -s -p cheese` may be written as `-ds -p cheese` or even `-dsp cheese`.
|
|
247
|
+
For example, `-d -s -p cheese` may be written as `-ds -p cheese` or even `-dsp cheese`.
|
|
249
248
|
|
|
250
249
|
Options with an expected option-argument are greedy and will consume the following argument whatever the value.
|
|
251
250
|
So `--id -xyz` reads `-xyz` as the option-argument.
|
|
@@ -276,8 +275,8 @@ cheese: stilton
|
|
|
276
275
|
|
|
277
276
|
### Other option types, negatable boolean and boolean|value
|
|
278
277
|
|
|
279
|
-
You can define a boolean option long name with a leading `no-` to set the option value to false when used.
|
|
280
|
-
Defined alone this also makes the option true by default.
|
|
278
|
+
You can define a boolean option long name with a leading `no-` to set the option value to `false` when used.
|
|
279
|
+
Defined alone, this also makes the option `true` by default.
|
|
281
280
|
|
|
282
281
|
If you define `--foo` first, adding `--no-foo` does not change the default value from what it would
|
|
283
282
|
otherwise be.
|
|
@@ -309,7 +308,7 @@ You ordered a pizza with no sauce and no cheese
|
|
|
309
308
|
```
|
|
310
309
|
|
|
311
310
|
You can specify an option which may be used as a boolean option but may optionally take an option-argument
|
|
312
|
-
(declared with square brackets like `--optional [value]`).
|
|
311
|
+
(declared with square brackets, like `--optional [value]`).
|
|
313
312
|
|
|
314
313
|
Example file: [options-boolean-or-value.js](./examples/options-boolean-or-value.js)
|
|
315
314
|
|
|
@@ -342,7 +341,9 @@ For information about possible ambiguous cases, see [options taking varying argu
|
|
|
342
341
|
|
|
343
342
|
### Required option
|
|
344
343
|
|
|
345
|
-
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 (
|
|
344
|
+
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 (e.g., from environment).
|
|
345
|
+
|
|
346
|
+
The method is otherwise the same as `.option()` in format, taking flags and description, and optional default value or custom processing.
|
|
346
347
|
|
|
347
348
|
Example file: [options-required.js](./examples/options-required.js)
|
|
348
349
|
|
|
@@ -363,7 +364,7 @@ error: required option '-c, --cheese <type>' not specified
|
|
|
363
364
|
You may make an option variadic by appending `...` to the value placeholder when declaring the option. On the command line you
|
|
364
365
|
can then specify multiple option-arguments, and the parsed option value will be an array. The extra arguments
|
|
365
366
|
are read until the first argument starting with a dash. The special argument `--` stops option processing entirely. If a value
|
|
366
|
-
is specified in the same argument as the option then no further values are read.
|
|
367
|
+
is specified in the same argument as the option, then no further values are read.
|
|
367
368
|
|
|
368
369
|
Example file: [options-variadic.js](./examples/options-variadic.js)
|
|
369
370
|
|
|
@@ -394,7 +395,7 @@ For information about possible ambiguous cases, see [options taking varying argu
|
|
|
394
395
|
|
|
395
396
|
### Version option
|
|
396
397
|
|
|
397
|
-
The optional
|
|
398
|
+
The optional `.version()` method adds handling for displaying the command version. The default option flags are `-V` and `--version`. When used, the command prints the version number and exits.
|
|
398
399
|
|
|
399
400
|
```js
|
|
400
401
|
program.version('0.0.1');
|
|
@@ -405,8 +406,8 @@ $ ./examples/pizza -V
|
|
|
405
406
|
0.0.1
|
|
406
407
|
```
|
|
407
408
|
|
|
408
|
-
You may change the flags and description by passing additional parameters to the
|
|
409
|
-
the same syntax for flags as the
|
|
409
|
+
You may change the flags and description by passing additional parameters to the `.version()` method, using
|
|
410
|
+
the same syntax for flags as the `.option()` method.
|
|
410
411
|
|
|
411
412
|
```js
|
|
412
413
|
program.version('0.0.1', '-v, --vers', 'output the current version');
|
|
@@ -453,7 +454,7 @@ $ extra --disable-server --port 8000
|
|
|
453
454
|
error: option '--disable-server' cannot be used with option '-p, --port <number>'
|
|
454
455
|
```
|
|
455
456
|
|
|
456
|
-
Specify a required (mandatory) option using the `Option` method `.makeOptionMandatory()`. This matches the `Command` method [
|
|
457
|
+
Specify a required (mandatory) option using the `Option` method `.makeOptionMandatory()`. This matches the `Command` method [`.requiredOption()`](#required-option).
|
|
457
458
|
|
|
458
459
|
### Custom option processing
|
|
459
460
|
|
|
@@ -521,7 +522,9 @@ $ custom --list x,y,z
|
|
|
521
522
|
|
|
522
523
|
## Commands
|
|
523
524
|
|
|
524
|
-
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
|
|
525
|
+
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. (More detail about this later.)
|
|
526
|
+
|
|
527
|
+
Subcommands may be nested. Example file: [nestedCommands.js](./examples/nestedCommands.js).
|
|
525
528
|
|
|
526
529
|
In the first parameter to `.command()` you specify the command name. You may append the command-arguments after the command name, or specify them separately using `.argument()`. The arguments may be `<required>` or `[optional]`, and the last argument may also be `variadic...`.
|
|
527
530
|
|
|
@@ -553,21 +556,20 @@ program
|
|
|
553
556
|
|
|
554
557
|
Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `hidden: true` will
|
|
555
558
|
remove the command from the generated help output. Specifying `isDefault: true` will run the subcommand if no other
|
|
556
|
-
subcommand is specified ([
|
|
559
|
+
subcommand is specified. (Example file: [defaultCommand.js](./examples/defaultCommand.js).)
|
|
557
560
|
|
|
558
|
-
You can add alternative names for a command with `.alias()`. ([
|
|
561
|
+
You can add alternative names for a command with `.alias()`. (Example file: [alias.js](./examples/alias.js).)
|
|
559
562
|
|
|
560
|
-
`.command()` automatically copies the inherited settings from the parent command to the newly created subcommand. This is only done during creation
|
|
563
|
+
`.command()` automatically copies the inherited settings from the parent command to the newly created subcommand. This is only done during creation; any later setting changes to the parent are not inherited.
|
|
561
564
|
|
|
562
565
|
For safety, `.addCommand()` does not automatically copy the inherited settings from the parent command. There is a helper routine `.copyInheritedSettings()` for copying the settings when they are wanted.
|
|
563
566
|
|
|
564
567
|
### Command-arguments
|
|
565
568
|
|
|
566
569
|
For subcommands, you can specify the argument syntax in the call to `.command()` (as shown above). This
|
|
567
|
-
is the only method usable for subcommands implemented using a stand-alone executable
|
|
568
|
-
you can instead use the following method.
|
|
570
|
+
is the only method usable for subcommands implemented using a stand-alone executable.
|
|
569
571
|
|
|
570
|
-
To configure a command, you can use `.argument()` to specify each expected command-argument.
|
|
572
|
+
Alternatively, you can instead use the following method. To configure a command, you can use `.argument()` to specify each expected command-argument.
|
|
571
573
|
You supply the argument name and an optional description. The argument may be `<required>` or `[optional]`.
|
|
572
574
|
You can specify a default value for an optional command-argument.
|
|
573
575
|
|
|
@@ -584,8 +586,10 @@ program
|
|
|
584
586
|
});
|
|
585
587
|
```
|
|
586
588
|
|
|
587
|
-
|
|
588
|
-
|
|
589
|
+
The last argument of a command can be variadic, and _only_ the last argument. To make an argument variadic, simply
|
|
590
|
+
append `...` to the argument name.
|
|
591
|
+
|
|
592
|
+
A variadic argument is passed to the action handler as an array.
|
|
589
593
|
|
|
590
594
|
```js
|
|
591
595
|
program
|
|
@@ -662,7 +666,7 @@ program
|
|
|
662
666
|
});
|
|
663
667
|
```
|
|
664
668
|
|
|
665
|
-
If you prefer, you can work with the command directly and skip declaring the parameters for the action handler.
|
|
669
|
+
If you prefer, you can work with the command directly and skip declaring the parameters for the action handler. If you use a function expression (but not an arrow function), the `this` keyword is set to the running command.
|
|
666
670
|
|
|
667
671
|
Example file: [action-this.js](./examples/action-this.js)
|
|
668
672
|
|
|
@@ -676,7 +680,7 @@ program
|
|
|
676
680
|
});
|
|
677
681
|
```
|
|
678
682
|
|
|
679
|
-
You may supply an `async` action handler, in which case you call `.parseAsync` rather than `.parse`.
|
|
683
|
+
You may supply an `async` action handler, in which case you call `.parseAsync()` rather than `.parse()`.
|
|
680
684
|
|
|
681
685
|
```js
|
|
682
686
|
async function run() { /* code goes here */ }
|
|
@@ -689,12 +693,14 @@ async function main() {
|
|
|
689
693
|
}
|
|
690
694
|
```
|
|
691
695
|
|
|
692
|
-
A command's options and arguments on the command line are validated when the command is used. Any unknown options
|
|
696
|
+
A command's options and arguments on the command line are validated when the command is used. Any unknown options, missing arguments, or excess arguments will be reported as an error.
|
|
697
|
+
|
|
698
|
+
You can suppress the unknown option check with `.allowUnknownOption()`. You can suppress the excess arguments check with `.allowExcessArguments()`.
|
|
693
699
|
|
|
694
700
|
### Stand-alone executable (sub)commands
|
|
695
701
|
|
|
696
702
|
When `.command()` is invoked with a description argument, this tells Commander that you're going to use stand-alone executables for subcommands.
|
|
697
|
-
Commander will search the files in the directory of the entry script for a file with the name combination `command-subcommand
|
|
703
|
+
Commander will search the files in the directory of the entry script for a file with the name combination `command-subcommand` (like `pm-install` or `pm-search` in the example below). The search includes trying common file extensions, like `.js`.
|
|
698
704
|
You may specify a custom name (and path) with the `executableFile` configuration option.
|
|
699
705
|
You may specify a custom search directory for subcommands with `.executableDir()`.
|
|
700
706
|
|
|
@@ -734,7 +740,7 @@ program
|
|
|
734
740
|
});
|
|
735
741
|
```
|
|
736
742
|
|
|
737
|
-
The callback hook can be `async`, in which case you call `.parseAsync` rather than `.parse`. You can add multiple hooks per event.
|
|
743
|
+
The callback hook can be `async`, in which case you call `.parseAsync()` rather than `.parse()`. You can add multiple hooks per event.
|
|
738
744
|
|
|
739
745
|
The supported events are:
|
|
740
746
|
|
|
@@ -743,7 +749,7 @@ The supported events are:
|
|
|
743
749
|
| `preAction`, `postAction` | before/after action handler for this command and its nested subcommands | `(thisCommand, actionCommand)` |
|
|
744
750
|
| `preSubcommand` | before parsing direct subcommand | `(thisCommand, subcommand)` |
|
|
745
751
|
|
|
746
|
-
For an overview of the life cycle events see [parsing life cycle and hooks](./docs/parsing-and-hooks.md).
|
|
752
|
+
For an overview of the life cycle events, see [parsing life cycle and hooks](./docs/parsing-and-hooks.md).
|
|
747
753
|
|
|
748
754
|
## Automated help
|
|
749
755
|
|
|
@@ -818,8 +824,8 @@ The positions "beforeAll" and "afterAll" apply to the command and all its subcom
|
|
|
818
824
|
|
|
819
825
|
The second parameter can be a string, or a function returning a string. The function is passed a context object for your convenience. The properties are:
|
|
820
826
|
|
|
821
|
-
- error
|
|
822
|
-
- command
|
|
827
|
+
- `error`: a boolean for whether the help is being displayed due to a usage error
|
|
828
|
+
- `command`: the `Command` which is displaying the help
|
|
823
829
|
|
|
824
830
|
### Display help after errors
|
|
825
831
|
|
|
@@ -863,7 +869,7 @@ error: unknown option '--hepl'
|
|
|
863
869
|
|
|
864
870
|
The command name appears in the help, and is also used for locating stand-alone executable subcommands.
|
|
865
871
|
|
|
866
|
-
You may specify the program name using `.name()` or in the Command constructor. For the program, Commander will
|
|
872
|
+
You may specify the program name using `.name()` or in the `Command` constructor. For the program, Commander will
|
|
867
873
|
fall back to using the script name from the full arguments passed into `.parse()`. However, the script name varies
|
|
868
874
|
depending on how your program is launched, so you may wish to specify it explicitly.
|
|
869
875
|
|
|
@@ -873,7 +879,7 @@ const pm = new Command('pm');
|
|
|
873
879
|
```
|
|
874
880
|
|
|
875
881
|
Subcommands get a name when specified using `.command()`. If you create the subcommand yourself to use with `.addCommand()`,
|
|
876
|
-
then set the name using `.name()` or in the Command constructor.
|
|
882
|
+
then set the name using `.name()` or in the `Command` constructor.
|
|
877
883
|
|
|
878
884
|
### .usage
|
|
879
885
|
|
|
@@ -907,18 +913,18 @@ This may require additional disk space.
|
|
|
907
913
|
|
|
908
914
|
### .helpOption(flags, description)
|
|
909
915
|
|
|
910
|
-
By default, every command has a help option. You may change the default help flags and description. Pass false to disable the built-in help option.
|
|
916
|
+
By default, every command has a help option. You may change the default help flags and description. Pass `false` to disable the built-in help option.
|
|
911
917
|
|
|
912
918
|
```js
|
|
913
919
|
program
|
|
914
920
|
.helpOption('-e, --HELP', 'read more information');
|
|
915
921
|
```
|
|
916
922
|
|
|
917
|
-
|
|
923
|
+
Alternatively, use `.addHelpOption()` to add an option you construct yourself.
|
|
918
924
|
|
|
919
925
|
### .helpCommand()
|
|
920
926
|
|
|
921
|
-
A help command is added by default if your command has subcommands. You can explicitly turn
|
|
927
|
+
A help command is added by default if your command has subcommands. You can explicitly turn the implicit help command on or off with `.helpCommand(true)` and `.helpCommand(false)`.
|
|
922
928
|
|
|
923
929
|
You can both turn on and customise the help command by supplying the name and description:
|
|
924
930
|
|
|
@@ -926,28 +932,30 @@ You can both turn on and customise the help command by supplying the name and de
|
|
|
926
932
|
program.helpCommand('assist [command]', 'show assistance');
|
|
927
933
|
```
|
|
928
934
|
|
|
929
|
-
|
|
935
|
+
Alternatively, use `.addHelpCommand()` to add a command you construct yourself.
|
|
930
936
|
|
|
931
937
|
### Help Groups
|
|
932
938
|
|
|
933
939
|
The help by default lists options under the the heading `Options:` and commands under `Commands:`. You can create your own groups
|
|
934
|
-
with different headings.
|
|
935
|
-
|
|
940
|
+
with different headings.
|
|
941
|
+
|
|
942
|
+
- The high-level way is to set the desired group heading while adding the options and commands, using `.optionsGroup()` and `.commandsGroup()`.
|
|
943
|
+
- The low-level way is using `.helpGroup()` on an individual `Option` or `Command`.
|
|
936
944
|
|
|
937
945
|
Example file: [help-groups.js](./examples/help-groups.js)
|
|
938
946
|
|
|
939
947
|
### More configuration
|
|
940
948
|
|
|
941
|
-
The built-in help is formatted using the Help class.
|
|
942
|
-
You can configure the help by modifying data properties and methods using `.configureHelp()`, or by subclassing Help using `.createHelp()` .
|
|
949
|
+
The built-in help is formatted using the `Help` class.
|
|
950
|
+
You can configure the help by modifying data properties and methods using `.configureHelp()`, or by subclassing `Help` using `.createHelp()` .
|
|
943
951
|
|
|
944
|
-
Simple properties include `sortSubcommands`, `sortOptions`, and `showGlobalOptions`. You can add color using the style methods like
|
|
952
|
+
Simple properties include `sortSubcommands`, `sortOptions`, and `showGlobalOptions`. You can add color using the style methods like `.styleTitle()`.
|
|
945
953
|
|
|
946
|
-
For more detail and examples of changing the displayed text, color, and layout see [
|
|
954
|
+
For more detail and examples of changing the displayed text, color, and layout, see: [Help in Depth](./docs/help-in-depth.md).
|
|
947
955
|
|
|
948
956
|
## Custom event listeners
|
|
949
957
|
|
|
950
|
-
You can execute custom actions by listening to command and option events.
|
|
958
|
+
You can execute custom actions by listening to `command` and `option` events.
|
|
951
959
|
|
|
952
960
|
```js
|
|
953
961
|
program.on('option:verbose', function () {
|
|
@@ -961,7 +969,7 @@ program.on('option:verbose', function () {
|
|
|
961
969
|
|
|
962
970
|
Call with no parameters to parse `process.argv`. Detects Electron and special node options like `node --eval`. Easy mode!
|
|
963
971
|
|
|
964
|
-
Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:
|
|
972
|
+
Or, call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:
|
|
965
973
|
|
|
966
974
|
- `'node'`: default, `argv[0]` is the application and `argv[1]` is the script being run, with user arguments after that
|
|
967
975
|
- `'electron'`: `argv[0]` is the application and `argv[1]` varies depending on whether the electron application is packaged
|
|
@@ -975,7 +983,7 @@ program.parse(process.argv); // assume argv[0] is app and argv[1] is script
|
|
|
975
983
|
program.parse(['--port', '80'], { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
|
|
976
984
|
```
|
|
977
985
|
|
|
978
|
-
Use parseAsync instead of parse if any of your action handlers are async.
|
|
986
|
+
Use `.parseAsync()` instead of `.parse()` if any of your action handlers are async.
|
|
979
987
|
|
|
980
988
|
### Parsing Configuration
|
|
981
989
|
|
|
@@ -1010,13 +1018,15 @@ program arg --port=80
|
|
|
1010
1018
|
By default, the option processing shows an error for an unknown option. To have an unknown option treated as an ordinary command-argument and continue looking for options, use `.allowUnknownOption()`. This lets you mix known and unknown options.
|
|
1011
1019
|
|
|
1012
1020
|
By default, the argument processing displays an error for more command-arguments than expected.
|
|
1013
|
-
To suppress the error for excess arguments, use`.allowExcessArguments()`.
|
|
1021
|
+
To suppress the error for excess arguments, use `.allowExcessArguments()`.
|
|
1014
1022
|
|
|
1015
1023
|
### Legacy options as properties
|
|
1016
1024
|
|
|
1017
1025
|
Before Commander 7, the option values were stored as properties on the command.
|
|
1018
1026
|
This was convenient to code, but the downside was possible clashes with
|
|
1019
|
-
existing properties of `Command`.
|
|
1027
|
+
existing properties of `Command`.
|
|
1028
|
+
|
|
1029
|
+
You can revert to the old behaviour to run unmodified legacy code by using `.storeOptionsAsProperties()`.
|
|
1020
1030
|
|
|
1021
1031
|
```js
|
|
1022
1032
|
program
|
|
@@ -1031,15 +1041,15 @@ program
|
|
|
1031
1041
|
|
|
1032
1042
|
### TypeScript
|
|
1033
1043
|
|
|
1034
|
-
extra-typings
|
|
1044
|
+
**extra-typings:** There is an optional project to infer extra type information from the option and argument definitions.
|
|
1035
1045
|
This adds strong typing to the options returned by `.opts()` and the parameters to `.action()`.
|
|
1036
|
-
|
|
1046
|
+
For more, see the repo: [commander-js/extra-typings](https://github.com/commander-js/extra-typings).
|
|
1037
1047
|
|
|
1038
|
-
```
|
|
1048
|
+
```ts
|
|
1039
1049
|
import { Command } from '@commander-js/extra-typings';
|
|
1040
1050
|
```
|
|
1041
1051
|
|
|
1042
|
-
ts-node
|
|
1052
|
+
**ts-node:** If you use `ts-node` and stand-alone executable subcommands written as `.ts` files, you need to call your program through node to get the subcommands called correctly, e.g.:
|
|
1043
1053
|
|
|
1044
1054
|
```sh
|
|
1045
1055
|
node -r ts-node/register pm.ts
|
|
@@ -1054,15 +1064,15 @@ const { createCommand } = require('commander');
|
|
|
1054
1064
|
const program = createCommand();
|
|
1055
1065
|
```
|
|
1056
1066
|
|
|
1057
|
-
`createCommand` is also a method of the Command object, and creates a new command rather than a subcommand. This gets used internally
|
|
1067
|
+
`createCommand()` is also a method of the `Command` object, and creates a new command rather than a subcommand. This gets used internally
|
|
1058
1068
|
when creating subcommands using `.command()`, and you may override it to
|
|
1059
|
-
customise the new subcommand
|
|
1069
|
+
customise the new subcommand. Example file: [custom-command-class.js](./examples/custom-command-class.js).
|
|
1060
1070
|
|
|
1061
1071
|
### Node options such as `--harmony`
|
|
1062
1072
|
|
|
1063
1073
|
You can enable `--harmony` option in two ways:
|
|
1064
1074
|
|
|
1065
|
-
- Use `#! /usr/bin/env node --harmony` in the subcommands scripts. (Note Windows does not support this pattern.)
|
|
1075
|
+
- Use `#! /usr/bin/env node --harmony` in the subcommands scripts. (Note: Windows does not support this pattern.)
|
|
1066
1076
|
- Use the `--harmony` option when calling the command, like `node --harmony examples/pm publish`. The `--harmony` option will be preserved when spawning subcommand processes.
|
|
1067
1077
|
|
|
1068
1078
|
### Debugging stand-alone executable subcommands
|
|
@@ -1072,14 +1082,14 @@ An executable subcommand is launched as a separate child process.
|
|
|
1072
1082
|
If you are using the node inspector for [debugging](https://nodejs.org/en/docs/guides/debugging-getting-started/) executable subcommands using `node --inspect` et al.,
|
|
1073
1083
|
the inspector port is incremented by 1 for the spawned subcommand.
|
|
1074
1084
|
|
|
1075
|
-
If you are using VSCode to debug executable subcommands you need to set the `"autoAttachChildProcesses": true` flag in your launch.json configuration.
|
|
1085
|
+
If you are using VSCode to debug executable subcommands you need to set the `"autoAttachChildProcesses": true` flag in your `launch.json` configuration.
|
|
1076
1086
|
|
|
1077
1087
|
### npm run-script
|
|
1078
1088
|
|
|
1079
|
-
By default, when you call your program using run-script
|
|
1089
|
+
By default, when you call your program using `run-script`, `npm` will parse any options on the command-line and they will not reach your program. Use
|
|
1080
1090
|
`--` to stop the npm option parsing and pass through all the arguments.
|
|
1081
1091
|
|
|
1082
|
-
The synopsis for [npm run-script](https://docs.npmjs.com/cli/v9/commands/npm-run-script) explicitly shows the `--` for this reason:
|
|
1092
|
+
The synopsis for [`npm run-script`](https://docs.npmjs.com/cli/v9/commands/npm-run-script) explicitly shows the `--` for this reason:
|
|
1083
1093
|
|
|
1084
1094
|
```console
|
|
1085
1095
|
npm run-script <command> [-- <args>]
|
|
@@ -1089,7 +1099,7 @@ npm run-script <command> [-- <args>]
|
|
|
1089
1099
|
|
|
1090
1100
|
This routine is available to invoke the Commander error handling for your own error conditions. (See also the next section about exit handling.)
|
|
1091
1101
|
|
|
1092
|
-
As well as the error message, you can optionally specify the `exitCode` (used with `process.exit`)
|
|
1102
|
+
As well as the error message, you can optionally specify the `exitCode` (used with `process.exit()`)
|
|
1093
1103
|
and `code` (used with `CommanderError`).
|
|
1094
1104
|
|
|
1095
1105
|
```js
|
|
@@ -1099,12 +1109,16 @@ program.error('Custom processing has failed', { exitCode: 2, code: 'my.custom.er
|
|
|
1099
1109
|
|
|
1100
1110
|
### Override exit and output handling
|
|
1101
1111
|
|
|
1102
|
-
By default, Commander calls `process.exit` when it detects errors, or after displaying the help or version. You can override
|
|
1112
|
+
By default, Commander calls `process.exit()` when it detects errors, or after displaying the help or version. You can override
|
|
1103
1113
|
this behaviour and optionally supply a callback. The default override throws a `CommanderError`.
|
|
1104
1114
|
|
|
1105
|
-
The override callback is passed a `CommanderError` with properties
|
|
1106
|
-
|
|
1107
|
-
|
|
1115
|
+
The override callback is passed a `CommanderError` with the properties:
|
|
1116
|
+
- `exitCode`: number
|
|
1117
|
+
- `code`: string
|
|
1118
|
+
- `message`: string
|
|
1119
|
+
|
|
1120
|
+
Commander expects the callback to terminate the normal program flow, and will call `process.exit()` if the callback returns.
|
|
1121
|
+
The normal display of error messages or version or help is not affected by the override, which is called after the display.
|
|
1108
1122
|
|
|
1109
1123
|
```js
|
|
1110
1124
|
program.exitOverride();
|
|
@@ -1142,13 +1156,16 @@ program
|
|
|
1142
1156
|
There is more information available about:
|
|
1143
1157
|
|
|
1144
1158
|
- [deprecated](./docs/deprecated.md) features still supported for backwards compatibility
|
|
1159
|
+
- [Help in Depth](./docs/help-in-depth.md) configuring help output
|
|
1145
1160
|
- [options taking varying arguments](./docs/options-in-depth.md)
|
|
1146
1161
|
- [parsing life cycle and hooks](./docs/parsing-and-hooks.md)
|
|
1162
|
+
- [Release Policy](./docs/release-policy.md)
|
|
1147
1163
|
|
|
1148
1164
|
## Support
|
|
1149
1165
|
|
|
1150
1166
|
The current version of Commander is fully supported on Long Term Support versions of Node.js, and requires at least v20.
|
|
1151
|
-
|
|
1167
|
+
|
|
1168
|
+
Older major versions of Commander receive security updates for 12 months. For more see: [Release Policy](./docs/release-policy.md).
|
|
1152
1169
|
|
|
1153
1170
|
The main forum for free and community support is the project [Issues](https://github.com/tj/commander.js/issues) on GitHub.
|
|
1154
1171
|
|
package/lib/command.js
CHANGED
|
@@ -1756,7 +1756,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
|
|
|
1756
1756
|
|
|
1757
1757
|
const negativeNumberArg = (arg) => {
|
|
1758
1758
|
// return false if not a negative number
|
|
1759
|
-
if (
|
|
1759
|
+
if (!/^-(\d+|\d*\.\d+)(e[+-]?\d+)?$/.test(arg)) return false;
|
|
1760
1760
|
// negative number is ok unless digit used as an option in command hierarchy
|
|
1761
1761
|
return !this._getCommandAndAncestors().some((cmd) =>
|
|
1762
1762
|
cmd.options
|
package/package-support.json
CHANGED
package/package.json
CHANGED
package/typings/index.d.ts
CHANGED
|
@@ -1044,7 +1044,7 @@ export class Command {
|
|
|
1044
1044
|
*/
|
|
1045
1045
|
outputHelp(context?: HelpContext): void;
|
|
1046
1046
|
/** @deprecated since v7 */
|
|
1047
|
-
outputHelp(cb
|
|
1047
|
+
outputHelp(cb: (str: string) => string): void;
|
|
1048
1048
|
|
|
1049
1049
|
/**
|
|
1050
1050
|
* Return command help documentation.
|
|
@@ -1071,7 +1071,7 @@ export class Command {
|
|
|
1071
1071
|
*/
|
|
1072
1072
|
help(context?: HelpContext): never;
|
|
1073
1073
|
/** @deprecated since v7 */
|
|
1074
|
-
help(cb
|
|
1074
|
+
help(cb: (str: string) => string): never;
|
|
1075
1075
|
|
|
1076
1076
|
/**
|
|
1077
1077
|
* Add additional text to be displayed with the built-in help.
|