Commander.js: option() default value doesn't work with parseInt

Created on 13 Apr 2016  路  4Comments  路  Source: tj/commander.js

  commander.option('-I --increment [value]', 'The interval increment', parseInt)
  commander.increment // works if '-I 2'
  commander.option('-I --increment [value]', 'The interval increment', parseInt, 1)
  commander.increment // gives NaN if '-I 2'

Seems like a bug or do I get it wrong?

Most helpful comment

BTW this is the behavior across options which allows you to do things like collections:

commander.option('-f, --foo <val>', 'Add values to foo', collect, []);
function collect(val, collection) {
  collection.push(val);
  return collection;
}

Which can be invoked: program --foo=hi --foo=bye and yields ['hi', 'bye']

All 4 comments

  commander.option('-I --increment [value]', 'The interval increment', str => parseInt(str), 1)

this works though

Did you try debugging it? My first guess would be that it's passing two params (input and default), which ends up calling parseInt with an invalid radix (e.g. parseInt(2, 1) // NaN)

BTW this is the behavior across options which allows you to do things like collections:

commander.option('-f, --foo <val>', 'Add values to foo', collect, []);
function collect(val, collection) {
  collection.push(val);
  return collection;
}

Which can be invoked: program --foo=hi --foo=bye and yields ['hi', 'bye']

@bfricka yes you made a right guess 馃憤
would be better to improve the doc :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FerencH picture FerencH  路  4Comments

san-templates picture san-templates  路  5Comments

shadowspawn picture shadowspawn  路  3Comments

hossamelmansy picture hossamelmansy  路  4Comments

akki005 picture akki005  路  5Comments