Skip to content
Myr

Added documentation for #795, --no-* improvements

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 · parent 8ca4134 · Browse files · 3 files+32 added−16 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+32 added−16 removed

  • other:+32 added−16 removed

3 files+32 added−16 removed

modified:examples/options-negatable.js+12 added-7 removed
modified:CHANGELOG.md+4 added
9 unmodified lines
10 * e.g. `--harmony`11 * Add parent command as prefix of subcommand in help (#980)12 * Add optional custom description to `.version` (#963)13 * Add `.helpOption(flags, description)` routine to customise help flags and description (#963)14 * Fix behavior of --no-* options (#795)15 * can now define both `--foo` and `--no-foo`16 * custom event listeners: `--no-foo` on cli now emits `option:no-foo` (previously `option:foo`)17 * default value: defining `--no-foo` after defining `--foo` leaves the default value unchanged (previously set it to false)18192.20.0 / 2019-04-0220==================21
404 unmodified lines
modified:Readme.md+16 added-9 removed
97 unmodified lines
98```99100### Other option types, negatable boolean and flag|value101102You can specify a boolean option long name with a leading `no-` to make it true by default and able to be negated.102You can specify a boolean option long name with a leading `no-` to set the option value to false when used.103Defined alone this also makes the option true by default.104If you define `foo` first, adding `--no-foo` does not change the default value.105106107```js108const program = require('commander');109110program109 .option('-n, --no-sauce', 'Remove sauce')111 .option('--no-sauce', 'Remove sauce')112 .option('--cheese <flavour>', 'cheese flavour', 'mozzarella')113 .option('--no-cheese', 'plain with no cheese')114 .parse(process.argv);115112if (program.sauce) console.log('you ordered a pizza with sauce');113else console.log('you ordered a pizza without sauce');116const sauceStr = program.sauce ? 'sauce' : 'no sauce';117const cheeseStr = (program.cheese === false) ? 'no cheese' : `${program.cheese} cheese`;118console.log(`You ordered a pizza with ${sauceStr} and ${cheeseStr}`);119```120121```bash117$ pizza-options118you ordered a pizza with sauce122$ pizza-options 123You ordered a pizza with sauce and mozzarella cheese124$ pizza-options --sauce125error: unknown option '--sauce'121$ pizza-options --no-sauce122you ordered a pizza without sauce126$ pizza-options --cheese=blue127You ordered a pizza with sauce and blue cheese128$ pizza-options --no-sauce --no-cheese129You ordered a pizza with no sauce and no cheese130```131132You can specify an option which functions as a flag but may also take a value (declared using square brackets).133126134```js135const program = require('commander');136137program
401 unmodified lines