Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow boolean default for flag option #987

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -402,8 +402,8 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {
}
}

// preassign default value only for --no-*, [optional], or <required>
if (option.negate || option.optional || option.required) {
// preassign default value for --no-*, [optional], <required>, or plain flag if boolean value
if (option.negate || option.optional || option.required || typeof defaultValue === 'boolean') {
// when --no-foo we make sure default is true, unless a --foo option is already defined
if (option.negate) {
var opts = self.opts();
Expand All @@ -427,7 +427,7 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {
val = fn(val, self[name] === undefined ? defaultValue : self[name]);
}

// unassigned or bool
// unassigned or boolean value
if (typeof self[name] === 'boolean' || typeof self[name] === 'undefined') {
// if no value, negate false, and we have a default, then use it!
if (val == null) {
Expand Down
8 changes: 6 additions & 2 deletions test/test.options.defaults.given.js
Expand Up @@ -8,15 +8,19 @@ var program = require('../')
program
.version('0.0.1')
.option('-a, --anchovies', 'Add anchovies?')
.option('-o, --onions', 'Add onions?', true)
.option('-o, --onions', 'Add onions?')
.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', '--onions', '--tomatoes', '--olives', '--no-sauce', '--crust', 'thin', '--cheese', 'wensleydale']);
shadowspawn marked this conversation as resolved.
Show resolved Hide resolved
program.should.have.property('anchovies', true);
program.should.have.property('onions', true);
program.should.have.property('tomatoes', true);
program.should.have.property('olives', 'black');
program.should.have.property('sauce', false);
program.should.have.property('crust', 'thin');
Expand Down
6 changes: 5 additions & 1 deletion test/test.options.defaults.js
Expand Up @@ -8,7 +8,10 @@ var program = require('../')
program
.version('0.0.1')
.option('-a, --anchovies', 'Add anchovies?')
.option('-o, --onions', 'Add onions?', true)
.option('-o, --onions', 'Add onions?')
.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')
Expand All @@ -20,6 +23,7 @@ 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('tomatoes', false);
program.should.not.have.property('olives');
program.should.have.property('sauce', true);
program.should.have.property('crust', 'hand-tossed');
Expand Down