diff --git a/Readme.md b/Readme.md index d1644012c..54eb6266e 100644 --- a/Readme.md +++ b/Readme.md @@ -69,6 +69,15 @@ function list(val) { return val.split(','); } +function collect(val, memo) { + memo.push(val); + return memo; +} + +function increaseVerbosity(v, total) { + return total + 1; +} + program .version('0.0.1') .usage('[options] ') @@ -77,6 +86,8 @@ program .option('-r, --range ..', 'A range', range) .option('-l, --list ', 'A list', list) .option('-o, --optional [value]', 'An optional value') + .option('-c, --collect [value]', 'A repeatable value', []) + .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0) .parse(process.argv); console.log(' int: %j', program.integer); @@ -85,6 +96,8 @@ console.log(' optional: %j', program.optional); program.range = program.range || []; console.log(' range: %j..%j', program.range[0], program.range[1]); console.log(' list: %j', program.list); +console.log(' collect: %j', program.collect); +console.log(' verbosity: %j', program.verbose); console.log(' args: %j', program.args); ``` diff --git a/examples/coercion b/examples/coercion index cd0bff831..46a46a286 100755 --- a/examples/coercion +++ b/examples/coercion @@ -14,6 +14,15 @@ function list(val) { return val.split(','); } +function collect(val, memo) { + memo.push(val); + return memo; +} + +function increaseVerbosity(v, total) { + return total + 1; +} + program .version('0.0.1') .usage('test') @@ -22,6 +31,8 @@ program .option('-r, --range ..', 'A range', range) .option('-l, --list ', 'A list', list) .option('-o, --optional [value]', 'An optional value') + .option('-c, --collect [value]', 'A repeatable value', collect, []) + .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0) .parse(process.argv); console.log(' int: %j', program.integer); @@ -30,4 +41,6 @@ console.log(' optional: %j', program.optional); program.range = program.range || []; console.log(' range: %j..%j', program.range[0], program.range[1]); console.log(' list: %j', program.list); -console.log(' args: %j', program.args); \ No newline at end of file +console.log(' collect: %j', program.collect); +console.log(' verbosity: %j', program.verbose); +console.log(' args: %j', program.args); diff --git a/index.js b/index.js index 107901d37..2c2f07e75 100644 --- a/index.js +++ b/index.js @@ -324,7 +324,7 @@ Command.prototype.option = function(flags, description, fn, defaultValue){ // and conditionally invoke the callback this.on(oname, function(val){ // coercion - if (null != val && fn) val = fn(val); + if (null !== val && fn) val = fn(val, undefined !== self[name] ? self[name] : defaultValue); // unassigned or bool if ('boolean' == typeof self[name] || 'undefined' == typeof self[name]) { diff --git a/test/test.options.coercion.js b/test/test.options.coercion.js index 296d9671a..df80a3602 100644 --- a/test/test.options.coercion.js +++ b/test/test.options.coercion.js @@ -9,15 +9,28 @@ function parseRange(str) { return str.split('..').map(Number); } +function increaseVerbosity(v, total) { + return total + 1; +} + +function collectValues(str, memo) { + memo.push(str); + return memo; +} + program .version('0.0.1') .option('-i, --int ', 'pass an int', parseInt) .option('-n, --num ', 'pass a number', Number) .option('-f, --float ', 'pass a float', parseFloat) - .option('-r, --range ', 'pass a range', parseRange); + .option('-r, --range ', 'pass a range', parseRange) + .option('-v, --verbose', 'increase verbosity', increaseVerbosity, 0) + .option('-c, --collect ', 'add a string (can be used multiple times)', collectValues, []); -program.parse('node test -i 5.5 -f 5.5 -n 15.99 -r 1..5'.split(' ')); +program.parse('node test -i 5.5 -f 5.5 -n 15.99 -r 1..5 -c foo -c bar -c baz -vvvv --verbose'.split(' ')); program.int.should.equal(5); program.num.should.equal(15.99); program.float.should.equal(5.5); program.range.should.eql([1,5]); +program.collect.should.eql([ 'foo', 'bar', 'baz' ]); +program.verbose.should.equal(5); diff --git a/test/test.options.commands.js b/test/test.options.commands.js index 8cb9e487e..85df74f74 100644 --- a/test/test.options.commands.js +++ b/test/test.options.commands.js @@ -48,8 +48,8 @@ program.parse(['node', 'test', '--config', 'conf']); program.config.should.equal("conf"); program.commands[0].should.not.have.property.setup_mode; program.commands[1].should.not.have.property.exec_mode; -envValue.should.be.null; -cmdValue.should.be.null; +envValue.should.equal(""); +cmdValue.should.equal(""); program.parse(['node', 'test', '--config', 'conf1', 'setup', '--setup_mode', 'mode3', 'env1']); program.config.should.equal("conf1");