Merge branch 'integration'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parents 3fb7ab0, c0b42f5 · Browse files · 3 files·+64 added−9 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+64 added−9 removed
- tests:+46 added−0 removed
- other:+18 added−9 removed
Provenance
- Commit
85e9893- Tree
68df507- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
3fb7ab0- Parent 2
c0b42f5
3 files changed+64 added−9 removed
modified:lib/commander.js+18 added-9 removed
264 unmodified lines265 266 // default as 3rd arg267 if ('function' != typeof fn) defaultValue = fn, fn = null;268 269+ // preassign default value only for --no-*, [optional], or <required>270+ if (false == option.bool || option.optional || option.required) {269- // when --no-* we default to true270- if (false == option.bool) defaultValue = true;271+ // when --no-* we make sure default is true272+ if (false == option.bool) defaultValue = true;273+ // preassign only if we have a default274+ if (undefined !== defaultValue) self[name] = defaultValue;275+ }276 272- // default273- if (undefined !== defaultValue) self[name] = defaultValue;274-277 // register the option278 this.options.push(option);279 280 // when it's passed assign the value1 unmodified line282 this.on(name, function(val){283 // coercion284 if (null != val && fn) val = fn(val);285 284- // assign value285- self[name] = null == val286- ? option.bool287- : val;286+ // unassigned or bool287+ if (/undefined|boolean/.test(typeof self[name])) {288+ // if no value, bool true, and we have a default, then use it!289+ self[name] = null == val290+ ? (option.bool291+ ? defaultValue || true : false292+ ) : val;293+ } else {294+ // reassign295+ self[name] = val;296+ }297 });298 299 return this;300 };602 unmodified linesadded:test/test.options.defaults.given.js+23 added
1234567891011121314151617181920212223/** * Module dependencies. */
var program = require('../') , should = require('should');
program .version('0.0.1') .option('-a, --anchovies', 'Add anchovies?') .option('-o, --onions', 'Add onions?', true) .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.should.have.property('anchovies', true);program.should.have.property('onions', 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');added:test/test.options.defaults.js+23 added
1234567891011121314151617181920212223/** * Module dependencies. */
var program = require('../') , should = require('should');
program .version('0.0.1') .option('-a, --anchovies', 'Add anchovies?') .option('-o, --onions', 'Add onions?', true) .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']);program.should.not.have.property('anchovies');program.should.not.have.property('onions');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');