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
Provenance
- Commit
a9503bb- Tree
f5b756f- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
55e88dc
7 files changed+99 added−67 removed
modified:test/test.options.bool.js+19 added-12 removed
1231235645891011678910111213131415141516171819202122/** * Module dependencies. */const commander = require('../');require('should');
var program = require('../') , should = require('should');// Test simple flag and negatable flag
program .version('0.0.1') .option('-p, --pepper', 'add pepper') .option('-c, --no-cheese', 'remove cheese');function simpleFlagProgram() { const program = new commander.Command(); program .option('-p, --pepper', 'add pepper') .option('-C, --no-cheese', 'remove cheese'); return program;}
program.parse(['node', 'test', '--pepper']);program.pepper.should.be.true();program.cheese.should.be.true();const simpleFlagNoOptions = simpleFlagProgram();simpleFlagNoOptions.parse(['node', 'test']);simpleFlagNoOptions.should.not.have.property('pepper');simpleFlagNoOptions.cheese.should.be.true();
const simpleFlagLong = simpleFlagProgram();simpleFlagLong.parse(['node', 'test', '--pepper', '--no-cheese']);simpleFlagLong.pepper.should.be.true();simpleFlagLong.cheese.should.be.false();modified:test/test.options.bool.no.js+57 added-25 removed
1231235645689101112137891011121314151617181516202122231718192025262721222324293031252627282930313233343536373839404142434445464748495051525354555657585960616263/** * Module dependencies. */const commander = require('../');require('should');
var program = require('../') , should = require('should');// Test combination of flag and --no-flag// (negatable flag on its own is tested in test.options.bool.js)
program .version('0.0.1') .option('-e, --everything', 'add all of the toppings') .option('-p, --pepper', 'add pepper') .option('-P, --no-pepper', 'remove pepper') .option('-c|--no-cheese', 'remove cheese');function flagProgram(defaultValue) { const program = new commander.Command(); program .option('-p, --pepper', 'add pepper', defaultValue) .option('-P, --no-pepper', 'remove pepper'); return program;}
program.parse(['node', 'test']);program.should.not.have.property('everything');program.should.not.have.property('pepper');program.cheese.should.be.true();// Flag with no default, normal usage.
program.parse(['node', 'test', '--everything']);program.everything.should.be.true();program.should.not.have.property('pepper');program.cheese.should.be.true();const programNoDefaultNoOptions = flagProgram();programNoDefaultNoOptions.parse(['node', 'test']);programNoDefaultNoOptions.should.not.have.property('pepper');
program.parse(['node', 'test', '--pepper']);program.pepper.should.be.true();program.cheese.should.be.true();const programNoDefaultWithFlag = flagProgram();programNoDefaultWithFlag.parse(['node', 'test', '--pepper']);programNoDefaultWithFlag.pepper.should.be.true();
program.parse(['node', 'test', '--everything', '--no-pepper', '--no-cheese']);program.pepper.should.be.false();program.cheese.should.be.false();const programNoDefaultWithNegFlag = flagProgram();programNoDefaultWithNegFlag.parse(['node', 'test', '--no-pepper']);programNoDefaultWithNegFlag.pepper.should.be.false();
// Flag with default, say from an environment variable.
const programTrueDefaultNoOptions = flagProgram(true);programTrueDefaultNoOptions.parse(['node', 'test']);programTrueDefaultNoOptions.pepper.should.be.true();
const programTrueDefaultWithFlag = flagProgram(true);programTrueDefaultWithFlag.parse(['node', 'test', '-p']);programTrueDefaultWithFlag.pepper.should.be.true();
const programTrueDefaultWithNegFlag = flagProgram(true);programTrueDefaultWithNegFlag.parse(['node', 'test', '-P']);programTrueDefaultWithNegFlag.pepper.should.be.false();
const programFalseDefaultNoOptions = flagProgram(false);programFalseDefaultNoOptions.parse(['node', 'test']);programFalseDefaultNoOptions.pepper.should.be.false();
const programFalseDefaultWithFlag = flagProgram(false);programFalseDefaultWithFlag.parse(['node', 'test', '-p']);programFalseDefaultWithFlag.pepper.should.be.true();
const programFalseDefaultWithNegFlag = flagProgram(false);programFalseDefaultWithNegFlag.parse(['node', 'test', '-P']);programFalseDefaultWithNegFlag.pepper.should.be.false();
// Flag specified both ways, last one wins.
const programNoYes = flagProgram();programNoYes.parse(['node', 'test', '--no-pepper', '--pepper']);programNoYes.pepper.should.be.true();
const programYesNo = flagProgram();programYesNo.parse(['node', 'test', '--pepper', '--no-pepper']);programYesNo.pepper.should.be.false();modified:test/test.options.bool.small.js+2 added-6 removed
12312356745674 unmodified lines/** * Module dependencies. */var program = require('../');require('should');
var program = require('../') , should = require('should');
program .version('0.0.1') .option('-p, --pepper', 'add pepper') .option('-c, --no-cheese', 'remove cheese');4 unmodified linesmodified:test/test.options.defaults.given.js+8 added-9 removed
1231235674956789101112131417151619171819202122/** * Module dependencies. */const program = require('../');require('should');
var program = require('../') , should = require('should');
program .version('0.0.1') .option('-a, --anchovies', 'Add anchovies?') .option('-o, --onions', 'Add onions?', true) .option('-O, --no-onions', 'No onions') .option('-t, --tomatoes', 'Add tomatoes?', false) .option('-T, --no-tomatoes', 'No tomatoes') .option('-v, --olives', 'Add olives? Sorry we only have black.', 'black') .option('-s, --no-sauce', 'Uh… okay') .option('-r, --crust <type>', 'What kind of crust would you like?', 'hand-tossed') .option('-c, --cheese [type]', 'optionally specify the type of cheese', 'mozzarella');
program.parse(['node', 'test', '--anchovies', '--onions', '--olives', '--no-sauce', '--crust', 'thin', '--cheese', 'wensleydale']);program.parse(['node', 'test', '--anchovies', '--no-onions', '--tomatoes', '--olives', '--no-sauce', '--crust', 'thin', '--cheese', 'wensleydale']);program.should.have.property('anchovies', true);program.should.have.property('onions', true);program.should.have.property('onions', false);program.should.have.property('tomatoes', true);program.should.have.property('olives', 'black');program.should.have.property('sauce', false);program.should.have.property('crust', 'thin');program.should.have.property('cheese', 'wensleydale');modified:test/test.options.defaults.js+8 added-11 removed
1231235674956789101112131417181520161722181920212223/** * Module dependencies. */const program = require('../');require('should');
var program = require('../') , should = require('should');
program .version('0.0.1') .option('-a, --anchovies', 'Add anchovies?') .option('-o, --onions', 'Add onions?', true) .option('-O, --no-onions', 'No onions') .option('-t, --tomatoes', 'Add tomatoes?', false) .option('-T, --no-tomatoes', 'No tomatoes') .option('-v, --olives', 'Add olives? Sorry we only have black.', 'black') .option('-s, --no-sauce', 'Uh… okay') .option('-r, --crust <type>', 'What kind of crust would you like?', 'hand-tossed') .option('-c, --cheese [type]', 'optionally specify the type of cheese', 'mozzarella');
program.should.have.property('_name', '');
program.parse(['node', 'test']);program.should.have.property('_name', 'test');
program.should.not.have.property('anchovies');program.should.not.have.property('onions');program.should.have.property('onions', true);program.should.have.property('tomatoes', false);program.should.not.have.property('olives');program.should.have.property('sauce', true);program.should.have.property('crust', 'hand-tossed');program.should.have.property('cheese', 'mozzarella');modified:index.js+3 added-3 removed
369 unmodified lines370 fn = null;371 }372 }373 374- // 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 lines395 if (val !== null && fn) {396 val = fn(val, self[name] === undefined ? defaultValue : self[name]);397 }398 399- // 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.negate929 unmodified linesmodified:Readme.md+2 added-1 removed
98 unmodified lines99 ### Other option types, negatable boolean and flag|value100 101 You can specify a boolean option long name with a leading `no-` to set the option value to false when used.102 Defined alone this also makes the option true by default.103-If you define `foo` first, adding `--no-foo` does not change the default value.103 104+If you define `--foo` first, adding `--no-foo` does not change the default value from what it would105+otherwise be. You can specify a default boolean value for a boolean flag and it can be overridden on command line.106 107 ```js108 const program = require('commander');109 455 unmodified lines