Skip to content
Myr

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

4 files+43 added−4 removed

modified:examples/coercion+14 added-1 removed
12 unmodified lines
13function list(val) {14 return val.split(',');15}1617function collect(val, memo) {18 memo.push(val);19 return memo;20}2122function increaseVerbosity(v, total) {23 return total + 1;24}2526program27 .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);3738console.log(' int: %j', program.integer);39console.log(' float: %j', program.float);40console.log(' optional: %j', program.optional);41program.range = program.range || [];42console.log(' range: %j..%j', program.range[0], program.range[1]);43console.log(' list: %j', program.list);44console.log(' collect: %j', program.collect);45console.log(' verbosity: %j', program.verbose);33console.log(' args: %j', program.args);46console.log(' args: %j', program.args);
modified:test/test.options.coercion.js+15 added-2 removed
modified:index.js+1 added-1 removed
322 unmodified lines
323 // 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);328329 // 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 lines
modified:Readme.md+13 added
67 unmodified lines
68function list(val) {69 return val.split(',');70}7172function collect(val, memo) {73 memo.push(val);74 return memo;75}7677function increaseVerbosity(v, total) {78 return total + 1;79}8081program82 .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);9293console.log(' int: %j', program.integer);94console.log(' float: %j', program.float);95console.log(' optional: %j', program.optional);96program.range = program.range || [];97console.log(' range: %j..%j', program.range[0], program.range[1]);98console.log(' list: %j', program.list);99console.log(' collect: %j', program.collect);100console.log(' verbosity: %j', program.verbose);101console.log(' args: %j', program.args);102```103104## Custom help
104 unmodified lines