Skip to content
Myr

Merge branch 'fix/grammar' of github.com:aweebit/commander.js into aweebit-fix/grammar

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by John Gee · committed by John Gee · parents 03dea00, 3b31a1f · Browse files · 3 files+23 added−23 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+23 added−23 removed

  • other:+23 added−23 removed

3 files+23 added−23 removed

modified:docs/deprecated.md+1 added-1 removed
32 unmodified lines
3334This was an option passed to `.command()` to hide the command from the built-in help:3536```js37program.command('example', 'examnple command', { noHelp: true });37program.command('example', 'example command', { noHelp: true });38```3940The option was renamed `hidden` in Commander v5.1. Deprecated from Commander v7.41
185 unmodified lines
modified:docs/options-in-depth.md+4 added-4 removed
4 unmodified lines
5and subtle issues in depth.67- [Options taking varying numbers of option-arguments](#options-taking-varying-numbers-of-option-arguments)8 - [Parsing ambiguity](#parsing-ambiguity)9 - [Alternative: Make `--` part of your syntax](#alternative-make-----part-of-your-syntax)9 - [Alternative: Make `--` part of your syntax](#alternative-make----part-of-your-syntax)10 - [Alternative: Put options last](#alternative-put-options-last)11 - [Alternative: Use options instead of command-arguments](#alternative-use-options-instead-of-command-arguments)12- [Combining short options, and options taking arguments](#combining-short-options-and-options-taking-arguments)13 - [Combining short options as if boolean](#combining-short-options-as-if-boolean)
61 unmodified lines
75```7677If you want to avoid your users needing to learn when to use `--`, there are a few approaches you could take.7879#### Alternative: Make `--` part of your syntax79#### Alternative: Make `--` part of your syntax8081Rather than trying to teach your users what `--` does, you could just make it part of your syntax.8283```js
85 unmodified lines
169170```sh171$ collect -o 3172other servings: 3173$ collect -o3 173$ collect -o3174other servings: 3175$ collect -l -v176vegan servings: true177halal servings: true
15 unmodified lines
193Before Commander v5, combining a short option and the value was not supported, and combined short flags were always expanded.194So `-avl` expanded to `-a -v -l`.195196If you want backwards compatible behaviour, or prefer combining short options as booleans to combining short option and value,197you may change the behavior.197you may change the behaviour.198199To modify the parsing of options taking an optional value:200201```js
3 unmodified lines
modified:Readme.md+18 added-18 removed
189 unmodified lines
190```191192You can use `--` to indicate the end of the options, and any remaining arguments will be used without being interpreted.193194By default options on the command line are not positional, and can be specified before or after other arguments.194By default, options on the command line are not positional, and can be specified before or after other arguments.195196There are additional related routines for when `.opts()` is not enough:197198- `.optsWithGlobals()` returns merged local and global option values
34 unmodified lines
233pizza details:234- cheese235```236237Multiple boolean short options may be combined together following the dash, and may be followed by a single short option taking a value.237Multiple boolean short options may be combined following the dash, and may be followed by a single short option taking a value.238For example `-d -s -p cheese` may be written as `-ds -p cheese` or even `-dsp cheese`.239240Options with an expected option-argument are greedy and will consume the following argument whatever the value.241So `--id -xyz` reads `-xyz` as the option-argument.
435 unmodified lines
677 await program.parseAsync(process.argv);678}679```680681A command's options and arguments on the command line are validated when the command is used. Any unknown options or missing arguments will be reported as an error. You can suppress the unknown option checks with `.allowUnknownOption()`. By default it is not an error to681A command's options and arguments on the command line are validated when the command is used. Any unknown options or missing arguments will be reported as an error. You can suppress the unknown option checks with `.allowUnknownOption()`. By default, it is not an error to682pass more arguments than declared, but you can make this an error with `.allowExcessArguments(false)`.683684### Stand-alone executable (sub)commands685
79 unmodified lines
765shell help spawn766shell spawn --help767```768769Long descriptions are wrapped to fit the available width. (However, a description that includes a line-break followed by whitespace is assumed to be pre-formatted and not wrapped.)769Long descriptions are wrapped to fit the available width. (However, a description that includes a line-break followed by whitespace is assumed to be pre-formatted and not wrapped.)770771### Custom help772773You can add extra text to be displayed along with the built-in help.
79 unmodified lines
853854The command name appears in the help, and is also used for locating stand-alone executable subcommands.855856You may specify the program name using `.name()` or in the Command constructor. For the program, Commander will857fallback to using the script name from the full arguments passed into `.parse()`. However, the script name varies858depending on how your program is launched so you may wish to specify it explicitly.857fall back to using the script name from the full arguments passed into `.parse()`. However, the script name varies858depending on how your program is launched, so you may wish to specify it explicitly.859860```js861program.name('pizza');862const pm = new Command('pm');
33 unmodified lines
896```897898### .helpOption(flags, description)899900By 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.900By 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.901902```js903program904 .helpOption('-e, --HELP', 'read more information');
65 unmodified lines
970### Parsing Configuration971972If the default parsing does not suit your needs, there are some behaviours to support other usage patterns.973974By default program options are recognised before and after subcommands. To only look for program options before subcommands, use `.enablePositionalOptions()`. This lets you use974By default, program options are recognised before and after subcommands. To only look for program options before subcommands, use `.enablePositionalOptions()`. This lets you use975an option for a different purpose in subcommands.976977Example file: [positional-options.js](./examples/positional-options.js)978
3 unmodified lines
982program -b subcommand983program subcommand -b984```985986By default options are recognised before and after command-arguments. To only process options that come987before the command-arguments, use `.passThroughOptions()`. This lets you pass the arguments and following options through to another program986By default, options are recognised before and after command-arguments. To only process options that come987before the command-arguments, use `.passThroughOptions()`. This lets you pass the arguments and following options through to another program988without needing to use `--` to end the option processing.989To use pass through options in a subcommand, the program needs to enable positional options.990991Example file: [pass-through-options.js](./examples/pass-through-options.js)
4 unmodified lines
996program --port=80 arg997program arg --port=80998```9991000By 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.1000By 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.10011002By default the argument processing does not display an error for more command-arguments than expected.1002By default, the argument processing does not display an error for more command-arguments than expected.1003To display an error for excess arguments, use`.allowExcessArguments(false)`.10041005### Legacy options as properties10061007Before Commander 7, the option values were stored as properties on the command.1008This was convenient to code but the downside was possible clashes with1008This was convenient to code, but the downside was possible clashes with1009existing properties of `Command`. You can revert to the old behaviour to run unmodified legacy code by using `.storeOptionsAsProperties()`.10101011```js1012program
15 unmodified lines
1028```1029import { Command } from '@commander-js/extra-typings';1030```10311032ts-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.1032ts-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.10331034```sh1035node -r ts-node/register pm.ts1036```
21 unmodified lines
1058### Debugging stand-alone executable subcommands10591060An executable subcommand is launched as a separate child process.10611062If you are using the node inspector for [debugging](https://nodejs.org/en/docs/guides/debugging-getting-started/) executable subcommands using `node --inspect` et al,1062If you are using the node inspector for [debugging](https://nodejs.org/en/docs/guides/debugging-getting-started/) executable subcommands using `node --inspect` et al.,1063the inspector port is incremented by 1 for the spawned subcommand.10641065If you are using VSCode to debug executable subcommands you need to set the `"autoAttachChildProcesses": true` flag in your launch.json configuration.10661067### npm run-script10681069By 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. Use1069By 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. Use1070 `--` to stop the npm option parsing and pass through all the arguments.10711072 The synopsis for [npm run-script](https://docs.npmjs.com/cli/v9/commands/npm-run-script) explicitly shows the `--` for this reason:1073
14 unmodified lines
1088```10891090### Override exit and output handling10911092By default Commander calls `process.exit` when it detects errors, or after displaying the help or version. You can override1092By default, Commander calls `process.exit` when it detects errors, or after displaying the help or version. You can override1093this behaviour and optionally supply a callback. The default override throws a `CommanderError`.10941095The override callback is passed a `CommanderError` with properties `exitCode` number, `code` string, and `message`. The default override behaviour is to throw the error, except for async handling of executable subcommand completion which carries on. The normal display of error messages or version or help1096is not affected by the override which is called after the display.
7 unmodified lines
1104 // custom processing...1105}1106```11071108By default Commander is configured for a command-line application and writes to stdout and stderr.1108By default, Commander is configured for a command-line application and writes to stdout and stderr.1109You can modify this behaviour for custom applications. In addition, you can modify the display of error messages.11101111Example file: [configure-output.js](./examples/configure-output.js)1112
36 unmodified lines