options-in-depth.md
This file
Path
docs/options-in-depth.md
204 lines · 6.0 KiB · Markdown
Last changed by
Merge branch 'fix/grammar' of github.com:aweebit/commander.js into aweebit-fix/grammarJohn Geebefore adoption591fc4b
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205<!-- omit from toc --># Options in Depth
The README covers declaring and using options, and mostly parsing will work the way you and your users expect. This page covers some special casesand subtle issues in depth.
- [Options taking varying numbers of option-arguments](#options-taking-varying-numbers-of-option-arguments) - [Parsing ambiguity](#parsing-ambiguity) - [Alternative: Make `--` part of your syntax](#alternative-make----part-of-your-syntax) - [Alternative: Put options last](#alternative-put-options-last) - [Alternative: Use options instead of command-arguments](#alternative-use-options-instead-of-command-arguments)- [Combining short options, and options taking arguments](#combining-short-options-and-options-taking-arguments) - [Combining short options as if boolean](#combining-short-options-as-if-boolean)
## Options taking varying numbers of option-arguments
Certain options take a varying number of arguments:
```jsprogram .option('-c, --compress [percentage]') // 0 or 1 .option('--preprocess <file...>') // 1 or more .option('--test [name...]') // 0 or more```
This section uses examples with options taking 0 or 1 arguments, but the discussions also apply to variadic options taking more arguments.
For information about terms used in this document see: [terminology](./terminology.md)
### Parsing ambiguity
There is a potential downside to be aware of. If a command has bothcommand-arguments and options with varying option-arguments, this introduces a parsing ambiguity which may affect the user of your program.Commander looks for option-arguments first, but the user mayintend the argument following the option as a command or command-argument.
```jsprogram .name('cook') .argument('[technique]') .option('-i, --ingredient [ingredient]', 'add cheese or given ingredient') .action((technique, options) => { console.log(`technique: ${technique}`); const ingredient = (options.ingredient === true) ? 'cheese' : options.ingredient; console.log(`ingredient: ${ingredient}`); });
program.parse();```
```sh$ cook scrambledtechnique: scrambledingredient: undefined
$ cook -itechnique: undefinedingredient: cheese
$ cook -i eggtechnique: undefinedingredient: egg
$ cook -i scrambled # oopstechnique: undefinedingredient: scrambled```
The explicit way to resolve this is use `--` to indicate the end of the options and option-arguments:
```sh$ node cook.js -i -- scrambledtechnique: scrambledingredient: cheese```
If you want to avoid your users needing to learn when to use `--`, there are a few approaches you could take.
#### Alternative: Make `--` part of your syntax
Rather than trying to teach your users what `--` does, you could just make it part of your syntax.
```jsprogram.usage('[options] -- [technique]');```
```sh$ cook --helpUsage: cook [options] -- [technique]
Options: -i, --ingredient [ingredient] add cheese or given ingredient -h, --help display help for command
$ cook -- scrambledtechnique: scrambledingredient: undefined
$ cook -i -- scrambledtechnique: scrambledingredient: cheese```
#### Alternative: Put options last
Commander follows the GNU convention for parsing and allows options before or after the command-arguments, or intermingled.So by putting the options last, the command-arguments do not get confused with the option-arguments.
```jsprogram.usage('[technique] [options]');```
```sh$ cook --helpUsage: cook [technique] [options]
Options: -i, --ingredient [ingredient] add cheese or given ingredient -h, --help display help for command
$ node cook.js scrambled -itechnique: scrambledingredient: cheese```
#### Alternative: Use options instead of command-arguments
This is a bit more radical, but completely avoids the parsing ambiguity!
```jsprogram .name('cook') .option('-t, --technique <technique>', 'cooking technique') .option('-i, --ingredient [ingredient]', 'add cheese or given ingredient') .action((options) => { console.log(`technique: ${options.technique}`); const ingredient = (options.ingredient === true) ? 'cheese' : options.ingredient; console.log(`ingredient: ${ingredient}`); });```
```sh$ cook -i -t scrambledtechnique: scrambledingredient: cheese```
## Combining short options, and options taking arguments
Multiple boolean short options can be combined after a single `-`, like `ls -al`. You can also include justa single short option which might take a value, as any following characters willbe taken as the value.
This means that by default you can not combine short options which may take an argument.
```jsprogram .name('collect') .option("-o, --other [count]", "other serving(s)") .option("-v, --vegan [count]", "vegan serving(s)") .option("-l, --halal [count]", "halal serving(s)");program.parse(process.argv);
const opts = program.opts();if (opts.other) console.log(`other servings: ${opts.other}`);if (opts.vegan) console.log(`vegan servings: ${opts.vegan}`);if (opts.halal) console.log(`halal servings: ${opts.halal}`);```
```sh$ collect -o 3other servings: 3$ collect -o3other servings: 3$ collect -l -vvegan servings: truehalal servings: true$ collect -lv # oopshalal servings: v```
If you wish to use options taking varying arguments as boolean options, you need to specify them separately.
```console$ collect -a -v -lany servings: truevegan servings: truehalal servings: true```
### Combining short options as if boolean
Before Commander v5, combining a short option and the value was not supported, and combined short flags were always expanded.So `-avl` expanded to `-a -v -l`.
If you want backwards compatible behaviour, or prefer combining short options as booleans to combining short option and value,you may change the behaviour.
To modify the parsing of options taking an optional value:
```js.combineFlagAndOptionalValue(true) // `-v45` is treated like `--vegan=45`, this is the default behaviour.combineFlagAndOptionalValue(false) // `-vl` is treated like `-v -l````