Skip to content

Commit

Permalink
Added documentation for #795, --no-* improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Jun 27, 2019
1 parent 8ca4134 commit f1924dd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,10 @@
* Add parent command as prefix of subcommand in help (#980)
* Add optional custom description to `.version` (#963)
* Add `.helpOption(flags, description)` routine to customise help flags and description (#963)
* Fix behavior of --no-* options (#795)
* can now define both `--foo` and `--no-foo`
* custom event listeners: `--no-foo` on cli now emits `option:no-foo` (previously `option:foo`)
* default value: defining `--no-foo` after defining `--foo` leaves the default value unchanged (previously set it to false)

2.20.0 / 2019-04-02
==================
Expand Down
25 changes: 16 additions & 9 deletions Readme.md
Expand Up @@ -99,30 +99,37 @@ cheese: stilton
### Other option types, negatable boolean and flag|value
You can specify a boolean option long name with a leading `no-` to make it true by default and able to be negated.
You can specify a boolean option long name with a leading `no-` to set the option value to false when used.
Defined alone this also makes the option true by default.
If you define `foo` first, adding `--no-foo` does not change the default value.
```js
const program = require('commander');

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}`);
```
```bash
$ 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
```
You can specify an option which functions as a flag but may also take a value (declared using square brackets).
You can specify an option which functions as a flag but may also take a value (declared using square brackets).
```js
const program = require('commander');
Expand Down
19 changes: 12 additions & 7 deletions examples/options-negatable.js
Expand Up @@ -6,19 +6,24 @@
//
// 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}`);

0 comments on commit f1924dd

Please sign in to comment.