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
Refactor headings (#2028)John Geebefore adoption03dea00
lines 1–8why, one step back1<!-- omit from toc -->2# Options in Depth34The README covers declaring and using options, and mostly parsing will work the way you and your users expect. This page covers some special cases5and subtle issues in depth.67- [Options taking varying numbers of option-arguments](#options-taking-varying-numbers-of-option-arguments)8 - [Parsing ambiguity](#parsing-ambiguity)Merge branch 'fix/grammar' of github.com:aweebit/commander.js into aweebit-fix/grammarJohn Geebefore adoption591fc4b
line 9why, one step back9 - [Alternative: Make `--` part of your syntax](#alternative-make----part-of-your-syntax)Refactor headings (#2028)John Geebefore adoption03dea00
lines 10–78why, one step back10 - [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)1415## Options taking varying numbers of option-arguments1617Certain options take a varying number of arguments:1819```js20program21 .option('-c, --compress [percentage]') // 0 or 122 .option('--preprocess <file...>') // 1 or more23 .option('--test [name...]') // 0 or more24```2526This section uses examples with options taking 0 or 1 arguments, but the discussions also apply to variadic options taking more arguments.2728For information about terms used in this document see: [terminology](./terminology.md)2930### Parsing ambiguity3132There is a potential downside to be aware of. If a command has both33command-arguments and options with varying option-arguments, this introduces a parsing ambiguity which may affect the user of your program.34Commander looks for option-arguments first, but the user may35intend the argument following the option as a command or command-argument.3637```js38program39 .name('cook')40 .argument('[technique]')41 .option('-i, --ingredient [ingredient]', 'add cheese or given ingredient')42 .action((technique, options) => {43 console.log(`technique: ${technique}`);44 const ingredient = (options.ingredient === true) ? 'cheese' : options.ingredient;45 console.log(`ingredient: ${ingredient}`);46 });4748program.parse();49```5051```sh52$ cook scrambled53technique: scrambled54ingredient: undefined5556$ cook -i57technique: undefined58ingredient: cheese5960$ cook -i egg61technique: undefined62ingredient: egg6364$ cook -i scrambled # oops65technique: undefined66ingredient: scrambled67```6869The explicit way to resolve this is use `--` to indicate the end of the options and option-arguments:7071```sh72$ node cook.js -i -- scrambled73technique: scrambled74ingredient: cheese75```7677If you want to avoid your users needing to learn when to use `--`, there are a few approaches you could take.78Merge branch 'fix/grammar' of github.com:aweebit/commander.js into aweebit-fix/grammarJohn Geebefore adoption591fc4b
line 79why, one step back79#### Alternative: Make `--` part of your syntaxRefactor headings (#2028)John Geebefore adoption03dea00
lines 80–172why, one step back8081Rather than trying to teach your users what `--` does, you could just make it part of your syntax.8283```js84program.usage('[options] -- [technique]');85```8687```sh88$ cook --help89Usage: cook [options] -- [technique]9091Options:92 -i, --ingredient [ingredient] add cheese or given ingredient93 -h, --help display help for command9495$ cook -- scrambled96technique: scrambled97ingredient: undefined9899$ cook -i -- scrambled100technique: scrambled101ingredient: cheese102```103104#### Alternative: Put options last105106Commander follows the GNU convention for parsing and allows options before or after the command-arguments, or intermingled.107So by putting the options last, the command-arguments do not get confused with the option-arguments.108109```js110program.usage('[technique] [options]');111```112113```sh114$ cook --help115Usage: cook [technique] [options]116117Options:118 -i, --ingredient [ingredient] add cheese or given ingredient119 -h, --help display help for command120121$ node cook.js scrambled -i122technique: scrambled123ingredient: cheese124```125126#### Alternative: Use options instead of command-arguments127128This is a bit more radical, but completely avoids the parsing ambiguity!129130```js131program132 .name('cook')133 .option('-t, --technique <technique>', 'cooking technique')134 .option('-i, --ingredient [ingredient]', 'add cheese or given ingredient')135 .action((options) => {136 console.log(`technique: ${options.technique}`);137 const ingredient = (options.ingredient === true) ? 'cheese' : options.ingredient;138 console.log(`ingredient: ${ingredient}`);139 });140```141142```sh143$ cook -i -t scrambled144technique: scrambled145ingredient: cheese146```147148## Combining short options, and options taking arguments149150Multiple boolean short options can be combined after a single `-`, like `ls -al`. You can also include just151a single short option which might take a value, as any following characters will152be taken as the value.153154This means that by default you can not combine short options which may take an argument.155156```js157program158 .name('collect')159 .option("-o, --other [count]", "other serving(s)")160 .option("-v, --vegan [count]", "vegan serving(s)")161 .option("-l, --halal [count]", "halal serving(s)");162program.parse(process.argv);163164const opts = program.opts();165if (opts.other) console.log(`other servings: ${opts.other}`);166if (opts.vegan) console.log(`vegan servings: ${opts.vegan}`);167if (opts.halal) console.log(`halal servings: ${opts.halal}`);168```169170```sh171$ collect -o 3172other servings: 3Merge branch 'fix/grammar' of github.com:aweebit/commander.js into aweebit-fix/grammarJohn Geebefore adoption591fc4b
line 173why, one step back173$ collect -o3Refactor headings (#2028)John Geebefore adoption03dea00
lines 174–196why, one step back174other servings: 3175$ collect -l -v176vegan servings: true177halal servings: true178$ collect -lv # oops179halal servings: v180```181182If you wish to use options taking varying arguments as boolean options, you need to specify them separately.183184```console185$ collect -a -v -l186any servings: true187vegan servings: true188halal servings: true189```190191### Combining short options as if boolean192193Before 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,Merge branch 'fix/grammar' of github.com:aweebit/commander.js into aweebit-fix/grammarJohn Geebefore adoption591fc4b
line 197why, one step back197you may change the behaviour.Refactor headings (#2028)John Geebefore adoption03dea00
lines 198–204why, one step back198199To modify the parsing of options taking an optional value:200201```js202.combineFlagAndOptionalValue(true) // `-v45` is treated like `--vegan=45`, this is the default behaviour203.combineFlagAndOptionalValue(false) // `-vl` is treated like `-v -l`204```