Skip to content
Myr

Allow boolean default for flag option (#987)

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

* Add support for default value for boolean flags * Expand testing for boolean flags to cover new features * Add written description of boolean default value for --foo/--no-foo * Avoid eslint warnings for requiring shouldjs in simple way

authored by John Gee · committed by GitHub · parent 55e88dc · Browse files · 7 files+99 added−67 removed
This commit

State

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

Diff stats

7 files+99 added−67 removed

  • tests:+94 added−63 removed
  • other:+5 added−4 removed

7 files+99 added−67 removed

modified:test/test.options.bool.js+19 added-12 removed
modified:test/test.options.bool.no.js+57 added-25 removed
modified:test/test.options.bool.small.js+2 added-6 removed
modified:test/test.options.defaults.given.js+8 added-9 removed
modified:test/test.options.defaults.js+8 added-11 removed
modified:index.js+3 added-3 removed
369 unmodified lines
370 fn = null;371 }372 }373374 // preassign default value only for --no-*, [optional], or <required>375 if (option.negate || option.optional || option.required) {374 // preassign default value for --no-*, [optional], <required>, or plain flag if boolean value375 if (option.negate || option.optional || option.required || typeof defaultValue === 'boolean') {376 // when --no-foo we make sure default is true, unless a --foo option is already defined377 if (option.negate) {378 var opts = self.opts();379 defaultValue = Object.prototype.hasOwnProperty.call(opts, name) ? opts[name] : true;
15 unmodified lines
395 if (val !== null && fn) {396 val = fn(val, self[name] === undefined ? defaultValue : self[name]);397 }398399 // unassigned or bool399 // unassigned or boolean value400 if (typeof self[name] === 'boolean' || typeof self[name] === 'undefined') {401 // if no value, negate false, and we have a default, then use it!402 if (val == null) {403 self[name] = option.negate
929 unmodified lines
modified:Readme.md+2 added-1 removed
98 unmodified lines
99### Other option types, negatable boolean and flag|value100101You can specify a boolean option long name with a leading `no-` to set the option value to false when used.102Defined alone this also makes the option true by default.103If you define `foo` first, adding `--no-foo` does not change the default value.103104If you define `--foo` first, adding `--no-foo` does not change the default value from what it would105otherwise be. You can specify a default boolean value for a boolean flag and it can be overridden on command line.106107```js108const program = require('commander');109
455 unmodified lines