Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.2.0 isn't compatible any more with parseInt used with default arguments #201

Closed
kornelski opened this issue Apr 1, 2014 · 7 comments
Closed

Comments

@kornelski
Copy link

 require('commander').
 option('--port <num>', 'Port number to listen on', parseInt, 9000).
 parse([ 'node', 'main.js', '--port=80' ]).
 port

This works as expected in 2.1.0, but in 2.2.0 parseInt is given 9000 as a second argument, which makes it output nonsense.

@rymai
Copy link

rymai commented Apr 22, 2014

I ran across this issue too. I've worked around (temporarily I hope) it like this:

function myParseInt(string, defaultValue) {
  var int = parseInt(string, 10);

  if (typeof int == 'number') {
    return int;
  } else {
    return defaultValue;
  }
}

and then:

 require('commander').
 option('--port <num>', 'Port number to listen on', myParseInt, 9000).
 parse([ 'node', 'main.js', '--port=80' ]).
 port

@oliverzy
Copy link

Me too. I think maybe it should be mentioned in doc at least. It took me half hour to figure out what happened.

@hildjj
Copy link

hildjj commented Nov 6, 2014

At a minimum, the README must be fixed. The examples in it do not run anymore, because:

parseInt("6", 2)

yields NaN. The second parameter to parseInt is all about the base.

@zhiyelee
Copy link
Collaborator

I have edited the History.md in 863e73a

srisonti added a commit to clearhead/node-proxy-injector that referenced this issue Feb 10, 2015
parseInt wasn't working correctly, so I was unable to use a custom port. Found this github issue: tj/commander.js#201. Made a custom function to handle the port option argument and it started working correctly.
@shadowspawn
Copy link
Collaborator

shadowspawn commented Apr 17, 2019

Oops, used this as the earliest report and want it open as I am linking to it from multiple places!

@shadowspawn shadowspawn reopened this Apr 17, 2019
@shadowspawn
Copy link
Collaborator

I have proposed a large update to options coverages in README in #953

@shadowspawn
Copy link
Collaborator

The options coverage in the README has been updated and includes new description of custom option processing, including a working parseInt example.

https://github.com/tj/commander.js#custom-option-processing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants