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
Comments
It works if you use |
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:
If I remove the default argment (
|
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! |
@xgbuils I think you'll have to fix this yourself or use an alternative project such as |
|
Now it log $ node index.js -v
1 |
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)
|
tj/commander.js#400 - unfixed for 3 years...
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: |
I have proposed a large update to options coverages in README in #953 |
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. |
Taken example from the front-page:
If you run this without specifying
-v
it will logundefined
rather than the expected0
.The text was updated successfully, but these errors were encountered: