-
Notifications
You must be signed in to change notification settings - Fork 123
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
Coerce function returns option of type "number" as "string" #182
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my concern with this patch is folks who are trying to return the string "55"
from coerce, and instead end up with the value 55
because we automatically parse numbers.
What if, instead, we apply the type casting to a variable before passing it to coerce, and always use the value returned by coerce -- I think this would be less shocking.
It depends on the option type wether we parse or not. The current behavior (without coerce functions) is:
So if users define their option as The key question finally is whether the option type has precedence over the return type of the coerce function. |
Can you explain the bug to me that this solves for you in |
const parse = require('yargs-parser');
const args = parse('--retries 2 --retries 3', {
number: ['retries'],
coerce: {
'retries': v => Array.isArray(v) ? v.pop() : v
// 'retries': v => Number(Array.isArray(v) ? v.pop() : v)
},
configuration: {
'parse-numbers': true, // default
'duplicate-arguments-array': true // default
}
}
);
console.log(args); // { _: [], retries: '3' } In short: a This is not a critical bug for Mocha, so don't wait with cutting your new release. |
Description
closes #176
Description of Change
applyCoercions()
: the return value of the coerce function is now checked by callingmaybeCoerceNumber()
, wether it should be converted to type number.maybeCoerceNumber()
: currently return values of coerce functions are not converted to type number.I deleted this restriction <== THIS HAS TO BE CONFIRMED