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

Can't use default together with coercion (example from front-page broken) #400

Closed
insidewhy opened this issue Apr 28, 2015 · 10 comments
Closed

Comments

@insidewhy
Copy link

Taken example from the front-page:

var program = require('commander');

function increaseVerbosity(v, total) { return total + 1; }

program.option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0);

program.parse(process.argv)
console.log(program.verbose)

If you run this without specifying -v it will log undefined rather than the expected 0.

@insidewhy
Copy link
Author

It works if you use -v, --verbose <val> as the string (but then you have to use -v1 instead of -v).

@insidewhy
Copy link
Author

Another example with a similar principle, this time broken in a different way:

var program = require('commander');

program.option('-n, --some-number <n>', 'a number', parseInt, 100)
program.parse(process.argv)

console.log(program.someNumber)

Running it:

% node test.js      
100
% node test.js -n 14
NaN

If I remove the default argment (, 100) the latter works:

% node test.js -n 14
14

@xgbuils
Copy link

xgbuils commented Jun 24, 2015

Hi @ohjames!

I'm interested to combine coercion and default values in a personal project and am researching about this issue. The last example works if you set this code:

var program = require('commander');

program.option('-n, --some-number <n>', 'a number', function (val) {
    parseInt(val)
}, 100)
program.parse(process.argv)

console.log(program.someNumber)

Because parseInt() has two arguments and the second is 10 by default. But, in source the second parameter of coercion callback is used.

I'll keep researching your first example.

Regards!

@insidewhy
Copy link
Author

@xgbuils I think you'll have to fix this yourself or use an alternative project such as yargs if you want to do this.

@xgbuils
Copy link

xgbuils commented Jun 24, 2015

yargs has coercion feature?

@xgbuils
Copy link

xgbuils commented Jun 24, 2015

If you run this without specifying -v it will log undefined rather than the expected 0.

Now it log 1

$ node index.js -v
1

@beaugunderson
Copy link

The cleanest way I've found to do this is:

const _ = require('lodash');
const parse10 = _.ary(_.partialRight(parseInt, 10), 1);

program.option('-n, --some-number <n>', 'a number', parse10, 100)

_.ary(_.partialRight(parseInt, 10), 1); creates a function that takes one argument and passes that one argument through to parseInt(x, 10).

@gersongoulart
Copy link

gersongoulart commented Jul 26, 2018

I have a different use case (bellow is a simplified version):

var url = require('url');
var program = require('commander');

program.option('-n, --some-url <url>', 'A URL', url.parse, 'localhost:5001');
program.parse(process.argv);

console.log(program.someUrl);
// Expected: Url { protocol: 'localhost:', slashes: null, auth: null, host: '5001', port: null, hostname: '5001', hash: null, search: null, query: {}, pathname: null, path: null, href: 'localhost:5001' }
// Got: 'localhost:5001'

To me, this is unexpected behavior. I don't understand why the default value would not go through the same coercion function as the values informed by the user?

To get the correct default value that I need I have to add as default: url.parse('localhost:5001') but as a result, when running --help I'll get a message that says (default: [object Object]) instead of (default: 'localhost:5001').

@shadowspawn
Copy link
Collaborator

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

@shadowspawn
Copy link
Collaborator

I have updated the options coverage in the README. I'm not claiming to have addressed all the varied concerns here, but hopefully reduced some confusions!

This issue has not had any activity in over six months. It isn't likely to get acted on due to this report.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

Thank you for your contributions.

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

5 participants