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
Provenance
- Commit
f1924dd- Tree
6327d00- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent
8ca4134
3 files changed+32 added−16 removed
modified:examples/options-negatable.js+12 added-7 removed
4 unmodified lines56789109101112131413141516171819202120222324252623242728294 unmodified lines// You can specify a boolean option long name with a leading `no-` to make it true by default and able to be negated.//// Example output pretending command called pizza-options (or try directly with `node options-negatable.js`)//// $ pizza-options// you ordered a pizza with sauce// $ pizza-options // You ordered a pizza with sauce and mozzarella cheese// $ pizza-options --sauce// error: unknown option '--sauce'// $ pizza-options --no-sauce// you ordered a pizza without sauce// $ pizza-options --cheese=blue// You ordered a pizza with sauce and blue cheese// $ pizza-options --no-sauce --no-cheese// You ordered a pizza with no sauce and no cheese
const commander = require('commander');const program = new commander.Command();
program .option('-n, --no-sauce', 'Remove sauce') .option('--no-sauce', 'Remove sauce') .option('--cheese <flavour>', 'cheese flavour', 'mozzarella') .option('--no-cheese', 'plain with no cheese') .parse(process.argv);
if (program.sauce) console.log('you ordered a pizza with sauce');else console.log('you ordered a pizza without sauce');const sauceStr = program.sauce ? 'sauce' : 'no sauce';const cheeseStr = (program.cheese === false) ? 'no cheese' : `${program.cheese} cheese`;console.log(`You ordered a pizza with ${sauceStr} and ${cheeseStr}`);modified:CHANGELOG.md+4 added
9 unmodified lines10 * 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)18 19 2.20.0 / 2019-04-0220 ==================21 404 unmodified linesmodified:Readme.md+16 added-9 removed
97 unmodified lines98 ```99 100 ### Other option types, negatable boolean and flag|value101 102-You can specify a boolean option long name with a leading `no-` to make it true by default and able to be negated.102+You can specify a boolean option long name with a leading `no-` to set the option value to false when used.103+Defined alone this also makes the option true by default.104+If you define `foo` first, adding `--no-foo` does not change the default value.105 106 107 ```js108 const program = require('commander');109 110 program109- .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);115 112-if (program.sauce) console.log('you ordered a pizza with sauce');113-else console.log('you ordered a pizza without sauce');116+const sauceStr = program.sauce ? 'sauce' : 'no sauce';117+const cheeseStr = (program.cheese === false) ? 'no cheese' : `${program.cheese} cheese`;118+console.log(`You ordered a pizza with ${sauceStr} and ${cheeseStr}`);119 ```120 121 ```bash117-$ pizza-options118-you ordered a pizza with sauce122+$ pizza-options 123+You ordered a pizza with sauce and mozzarella cheese124 $ pizza-options --sauce125 error: unknown option '--sauce'121-$ pizza-options --no-sauce122-you ordered a pizza without sauce126+$ pizza-options --cheese=blue127+You ordered a pizza with sauce and blue cheese128+$ pizza-options --no-sauce --no-cheese129+You ordered a pizza with no sauce and no cheese130 ```131+132 You can specify an option which functions as a flag but may also take a value (declared using square brackets).133 126-134 ```js135 const program = require('commander');136 137 program401 unmodified lines