You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have situation where I'm taking in -b and expecting a number value but leaving it empty makes it true. It would simplify my logic if I didn't have to check the value and make yargs show help etc.
Hey @raine it's a bit low level, but yargs has the check method which allows you to perform arbitrary validation on the final parsed arguments prior to returning them to the user:
What if we modified check to take an argument and a function, and shipped with a few common checks:
check('b', yargs.isInteger), just a thought.
Independently of the check feature, I have been kicking around the idea of an number type, which would work similar to boolean, string, array, but would default integer types to 0 -- I'm not convinced this is super useful though, what do you think?
I don't think it should default to anything, because it seems like misuse of the parameter.
With cmd --foo I'd perceive the user's intent to be pass a boolean flag. If foo is a number option, the program should abort and inform the user about the mistake. The user's intent most likely is not to pass default, in that case they are more likely omit the parameter completely.
In my opinion this holds for string arguments as well. It's possible to inadvertently pass an empty string as parameter. How to pass an empty string then, is a good question. Perhaps cmd --foo "".
@plroebuck Yes, I think so. Thanks for the heads up.
If anyone finds a deficiency with the type: 'number' validation, or if you'd like to request other types (date maybe?), please open a new issue. Thanks!
seems like there still is no argument type validation on yargs. Would like yargs to show a message when i enter a word into a number argument. instead nothing happends and my application crashes further on because the number is not a number but a NaN
Activity
bcoe commentedon Mar 1, 2015
Hey @raine it's a bit low level, but yargs has the
checkmethod which allows you to perform arbitrary validation on the final parsed arguments prior to returning them to the user:https://github.com/bcoe/yargs#checkfn
What if we modified
checkto take an argument and a function, and shipped with a few common checks:check('b', yargs.isInteger), just a thought.Independently of the check feature, I have been kicking around the idea of an
numbertype, which would work similar toboolean,string,array, but would default integer types to0-- I'm not convinced this is super useful though, what do you think?raine commentedon Mar 1, 2015
Without prior knowledge, I was kind of expecting that I could use
typeto automatically perform some level of validation on the arguments.Here
cmd -bshould not become a boolean flag, because it's expecting a number.Here's how optionator, another argv parsing lib I quite like does it:
$ node index.js { _: [] } $ node index.js -b 1 { build: 1, _: [] } $ node index.js -b { _: [] } $ node index.js -b "foo" Invalid value for option 'build' - expected type Number, received value: foo.Code: https://gist.github.com/31ad323a2f7640c2d2b9
bcoe commentedon Mar 1, 2015
@raine seems like a reasonable ask, my only question is, when just a flag is given:
node index.js --fooShould this raise an exception, or should we default to
0, array defaults to[], string defaults to''and boolean defaults totrue.raine commentedon Mar 1, 2015
I don't think it should default to anything, because it seems like misuse of the parameter.
With
cmd --fooI'd perceive the user's intent to be pass a boolean flag. Iffoois a number option, the program should abort and inform the user about the mistake. The user's intent most likely is not to pass default, in that case they are more likely omit the parameter completely.In my opinion this holds for string arguments as well. It's possible to inadvertently pass an empty string as parameter. How to pass an empty string then, is a good question. Perhaps
cmd --foo "".a-x- commentedon Jul 29, 2015
My position, that omitted opt must be undefined.
And set explicit value (most commonly 0) would be do by standart way (using
default(key,0)API call)olsonpm commentedon Aug 24, 2015
+1
I got here looking for type validation - though it doesn't look like a sought after feature.
bcoe commentedon Feb 15, 2016
@raine it might not fully address this issue, but there's now a
numbertype in yargs 4.x.Give it a spin:
npm i yargs@nextplroebuck commentedon Apr 23, 2016
Shouldn't this issue be closed as of yargs-4?
nexdrew commentedon Apr 25, 2016
@plroebuck Yes, I think so. Thanks for the heads up.
If anyone finds a deficiency with the
type: 'number'validation, or if you'd like to request other types (datemaybe?), please open a new issue. Thanks!nghhoese commentedon Jul 3, 2023
seems like there still is no argument type validation on yargs. Would like yargs to show a message when i enter a word into a number argument. instead nothing happends and my application crashes further on because the number is not a number but a NaN
shadowspawn commentedon Jul 3, 2023
The related open issue to have Yargs show the error for bad
numberarguments is #1079