Skip to content
Myr

Refactor headings (#2028)

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

authored by John Gee · committed by GitHub · parent 67c9180 · Browse files · 3 files+207 added−204 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+207 added−204 removed

  • other:+207 added−204 removed

3 files+207 added−204 removed

added:docs/options-in-depth.md+204 added
1<!-- 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)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)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.7879#### Alternative: Make `--` part of your syntax8081Rather 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: 3173$ collect -o3 174other 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,197you may change the behavior.198199To 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```
deleted:docs/options-taking-varying-arguments.md-201 removed
1# Options taking varying numbers of option-arguments23The README covers declaring and using options, and mostly parsing will work the way you and your users expect. This page covers some special cases4and subtle issues in depth.56- [Options taking varying numbers of option-arguments](#options-taking-varying-numbers-of-option-arguments)7 - [Parsing ambiguity](#parsing-ambiguity)8 - [Alternative: Make `--` part of your syntax](#alternative-make-----part-of-your-syntax)9 - [Alternative: Put options last](#alternative-put-options-last)10 - [Alternative: Use options instead of command-arguments](#alternative-use-options-instead-of-command-arguments)11 - [Combining short options, and options taking arguments](#combining-short-options-and-options-taking-arguments)12 - [Combining short options as if boolean](#combining-short-options-as-if-boolean)1314Certain options take a varying number of arguments:1516```js17program18 .option('-c, --compress [percentage]') // 0 or 119 .option('--preprocess <file...>') // 1 or more20 .option('--test [name...]') // 0 or more21```2223This page uses examples with options taking 0 or 1 arguments, but the discussions also apply to variadic options taking more arguments.2425For information about terms used in this document see: [terminology](./terminology.md)2627## Parsing ambiguity2829There is a potential downside to be aware of. If a command has both30command-arguments and options with varying option-arguments, this introduces a parsing ambiguity which may affect the user of your program.31Commander looks for option-arguments first, but the user may32intend the argument following the option as a command or command-argument.3334```js35program36 .name('cook')37 .argument('[technique]')38 .option('-i, --ingredient [ingredient]', 'add cheese or given ingredient')39 .action((technique, options) => {40 console.log(`technique: ${technique}`);41 const ingredient = (options.ingredient === true) ? 'cheese' : options.ingredient;42 console.log(`ingredient: ${ingredient}`);43 });4445program.parse();46```4748```sh49$ cook scrambled50technique: scrambled51ingredient: undefined5253$ cook -i54technique: undefined55ingredient: cheese5657$ cook -i egg58technique: undefined59ingredient: egg6061$ cook -i scrambled # oops62technique: undefined63ingredient: scrambled64```6566The explicit way to resolve this is use `--` to indicate the end of the options and option-arguments:6768```sh69$ node cook.js -i -- scrambled70technique: scrambled71ingredient: cheese72```7374If you want to avoid your users needing to learn when to use `--`, there are a few approaches you could take.7576### Alternative: Make `--` part of your syntax7778Rather than trying to teach your users what `--` does, you could just make it part of your syntax.7980```js81program.usage('[options] -- [technique]');82```8384```sh85$ cook --help86Usage: cook [options] -- [technique]8788Options:89 -i, --ingredient [ingredient] add cheese or given ingredient90 -h, --help display help for command9192$ cook -- scrambled93technique: scrambled94ingredient: undefined9596$ cook -i -- scrambled97technique: scrambled98ingredient: cheese99```100101### Alternative: Put options last102103Commander follows the GNU convention for parsing and allows options before or after the command-arguments, or intermingled.104So by putting the options last, the command-arguments do not get confused with the option-arguments.105106```js107program.usage('[technique] [options]');108```109110```sh111$ cook --help112Usage: cook [technique] [options]113114Options:115 -i, --ingredient [ingredient] add cheese or given ingredient116 -h, --help display help for command117118$ node cook.js scrambled -i119technique: scrambled120ingredient: cheese121```122123### Alternative: Use options instead of command-arguments124125This is a bit more radical, but completely avoids the parsing ambiguity!126127```js128program129 .name('cook')130 .option('-t, --technique <technique>', 'cooking technique')131 .option('-i, --ingredient [ingredient]', 'add cheese or given ingredient')132 .action((options) => {133 console.log(`technique: ${options.technique}`);134 const ingredient = (options.ingredient === true) ? 'cheese' : options.ingredient;135 console.log(`ingredient: ${ingredient}`);136 });137```138139```sh140$ cook -i -t scrambled141technique: scrambled142ingredient: cheese143```144145## Combining short options, and options taking arguments146147Multiple boolean short options can be combined after a single `-`, like `ls -al`. You can also include just148a single short option which might take a value, as any following characters will149be taken as the value.150151This means that by default you can not combine short options which may take an argument.152153```js154program155 .name('collect')156 .option("-o, --other [count]", "other serving(s)")157 .option("-v, --vegan [count]", "vegan serving(s)")158 .option("-l, --halal [count]", "halal serving(s)");159program.parse(process.argv);160161const opts = program.opts();162if (opts.other) console.log(`other servings: ${opts.other}`);163if (opts.vegan) console.log(`vegan servings: ${opts.vegan}`);164if (opts.halal) console.log(`halal servings: ${opts.halal}`);165```166167```sh168$ collect -o 3169other servings: 3170$ collect -o3 171other servings: 3172$ collect -l -v173vegan servings: true174halal servings: true175$ collect -lv # oops176halal servings: v177```178179If you wish to use options taking varying arguments as boolean options, you need to specify them separately.180181```182$ collect -a -v -l183any servings: true184vegan servings: true185halal servings: true186```187188### Combining short options as if boolean189190Before Commander v5, combining a short option and the value was not supported, and combined short flags were always expanded.191So `-avl` expanded to `-a -v -l`.192193If you want backwards compatible behaviour, or prefer combining short options as booleans to combining short option and value, 194you may change the behavior.195196To modify the parsing of options taking an optional value:197198```js199.combineFlagAndOptionalValue(true) // `-v45` is treated like `--vegan=45`, this is the default behaviour200.combineFlagAndOptionalValue(false) // `-vl` is treated like `-v -l`201```
modified:Readme.md+3 added-3 removed
325 unmodified lines
326327Options with an optional option-argument are not greedy and will ignore arguments starting with a dash.328So `id` behaves as a boolean option for `--id -5`, but you can use a combined form if needed like `--id=-5`.329330For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-taking-varying-arguments.md).330For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-in-depth.md).331332### Required option333334You 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.
43 unmodified lines
378Options: { number: [ '1', '2', '3' ], letter: true }379Remaining arguments: [ 'operand' ]380```381382For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-taking-varying-arguments.md).382For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-in-depth.md).383384### Version option385386The optional `version` method adds handling for displaying the command version. The default option flags are `-V` and `--version`, and when present the command prints the version number and exits.
743 unmodified lines
11301131There is more information available about:11321133- [deprecated](./docs/deprecated.md) features still supported for backwards compatibility1134- [options taking varying arguments](./docs/options-taking-varying-arguments.md)1134- [options taking varying arguments](./docs/options-in-depth.md)1135- [parsing life cycle and hooks](./docs/parsing-and-hooks.md)11361137## Support1138
10 unmodified lines