Pass previous option value or default value to coercion function so it can be used to collect values.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Simon Oulevay · committed by Simon Oulevay · parent 56f4796 · Browse files · 4 files·+43 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+43 added−4 removed
- tests:+15 added−2 removed
- other:+28 added−2 removed
Provenance
- Commit
edafae1- Tree
fff28dd- Author
Simon Oulevay <simon.oulevay@gmail.com>- Committer
Simon Oulevay <simon.oulevay@gmail.com>- Parent
56f4796
4 files changed+43 added−4 removed
modified:examples/coercion+14 added-1 removed
12 unmodified lines13 function list(val) {14 return val.split(',');15 }16 17+function collect(val, memo) {18+ memo.push(val);19+ return memo;20+}21+22+function increaseVerbosity(v, total) {23+ return total + 1;24+}25+26 program27 .version('0.0.1')28 .usage('test')29 .option('-i, --integer <n>', 'An integer argument', parseInt)30 .option('-f, --float <n>', 'A float argument', parseFloat)31 .option('-r, --range <a>..<b>', 'A range', range)32 .option('-l, --list <items>', 'A list', list)33 .option('-o, --optional [value]', 'An optional value')34+ .option('-c, --collect [value]', 'A repeatable value', collect, [])35+ .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)36 .parse(process.argv);37 38 console.log(' int: %j', program.integer);39 console.log(' float: %j', program.float);40 console.log(' optional: %j', program.optional);41 program.range = program.range || [];42 console.log(' range: %j..%j', program.range[0], program.range[1]);43 console.log(' list: %j', program.list);44+console.log(' collect: %j', program.collect);45+console.log(' verbosity: %j', program.verbose);33-console.log(' args: %j', program.args);46+console.log(' args: %j', program.args);modified:test/test.options.coercion.js+15 added-2 removed
7 unmodified lines8910111213141516171819202122232425172627282919303132333435367 unmodified linesfunction 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 <n>', 'pass an int', parseInt) .option('-n, --num <n>', 'pass a number', Number) .option('-f, --float <n>', 'pass a float', parseFloat) .option('-r, --range <a..b>', 'pass a range', parseRange); .option('-r, --range <a..b>', 'pass a range', parseRange) .option('-v, --verbose', 'increase verbosity', increaseVerbosity, 0) .option('-c, --collect <str>', '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);modified:index.js+1 added-1 removed
322 unmodified lines323 // when it's passed assign the value324 // and conditionally invoke the callback325 this.on(oname, function(val){326 // coercion327- if (null != val && fn) val = fn(val);327+ if (null !== val && fn) val = fn(val, undefined !== self[name] ? self[name] : defaultValue);328 329 // unassigned or bool330 if ('boolean' == typeof self[name] || 'undefined' == typeof self[name]) {331 // if no value, bool true, and we have a default, then use it!521 unmodified linesmodified:Readme.md+13 added
67 unmodified lines68 function list(val) {69 return val.split(',');70 }71 72+function collect(val, memo) {73+ memo.push(val);74+ return memo;75+}76+77+function increaseVerbosity(v, total) {78+ return total + 1;79+}80+81 program82 .version('0.0.1')83 .usage('[options] <file ...>')84 .option('-i, --integer <n>', 'An integer argument', parseInt)85 .option('-f, --float <n>', 'A float argument', parseFloat)86 .option('-r, --range <a>..<b>', 'A range', range)87 .option('-l, --list <items>', 'A list', list)88 .option('-o, --optional [value]', 'An optional value')89+ .option('-c, --collect [value]', 'A repeatable value', [])90+ .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)91 .parse(process.argv);92 93 console.log(' int: %j', program.integer);94 console.log(' float: %j', program.float);95 console.log(' optional: %j', program.optional);96 program.range = program.range || [];97 console.log(' range: %j..%j', program.range[0], program.range[1]);98 console.log(' list: %j', program.list);99+console.log(' collect: %j', program.collect);100+console.log(' verbosity: %j', program.verbose);101 console.log(' args: %j', program.args);102 ```103 104 ## Custom help104 unmodified lines