Repository navigation
Description
Currently for options of type number, if the user provides the option and fails to provide valid input,
the value is returned as either NaN or undefined. No idea who needs that. Can we make it act more like .choices() and just implode? Not doing so makes implementing yargs.check() function argument arduous when much is seemingly unnecessary.
binary = path.basename pkg.bin # From "package.json"
argv = yargs
.usage('...')
.options(Abao.options)
.check((argv) ->
# Ensure single positional argument present
if argv._.length < 1
throw new Error binary + ': must specify path to file'
else if argv._.length > 1
throw new Error binary + ': accepts single positional command-line argument'
## Validate "timeout" option
if _.isUndefined argv.timeout
throw new Error binary + ': option "--timeout" given without a value'
if _.isNaN argv.timeout
# Would be better message if it included unparsable user argument value
throw new Error binary + ': option "--timeout" value could not be parsed as a number'
if !_.isInteger argv.timeout
throw new Error binary + ': option "--timeout" value must be scalar integer'
if argv.timeout < 0
throw new Error binary + ': option "--timeout" value must be nonnegative'
#
# YA four-part stanza like above for EVERY numeric option...
#
return true
)
.help('help', 'Show usage information and exit')
.version().describe('version', 'Show version number and exit')
.argvTechnically, the above requested change only handles the first half of the stanza.
Would need to add two more API methods to handle the rest:
.scalar(key, bool) - if true, only single value accepted (no array); otherwise, false (default).
.range(key, func) - user-provided function to handle range validation
e.g.,
nonnegative = (val) -> val < 0
yargs.range('timeout', nonnegative)For that matter, how about a sibling .integer(key) method that only accepts integers?
Continuation of now closed #110 discussion...
Activity
grnch commentedon Apr 4, 2018
This part has definitely bitten me more than once. If I specify that an argument is a number, and the user enters something that is not a number, yargs should immediately let the user know they entered an invalid value. Continuing with
NaNlike it does now is just a recipe for weird undiagnosable errors down the line.P.S.
I have no opinion on the rest of the post with the
check()/scalar()/range()stuff. They seem like separate issues, to be honest. I only wanted to add support for the part I quoted above.newmanw commentedon Dec 10, 2020
This got me as well. Expecting that when I define type: 'number' that the input is validated to actually be a number.
Baterka commentedon May 19, 2023
Same.
ReshetovItsMe commentedon Jun 19, 2023
Same
plroebuck commentedon Jul 4, 2023
@bcoe, it's been more than 5 years since this request was made... if this request won't ever get any love, just close it as "won't implement". There have been repeated requests to implode rather than return a
NaNas these are causing bizarre errors that must be tracked down by package developers rather than simply being an error message saying that an argument wasn't given properly by the user.I personally wouldn't think
.scalar()or.range()would be difficult either and their inclusion would make this significantly better. But I see no reason that the default return value should still be broken after all this time.shadowspawn commentedon Jul 4, 2023
This is one of the more popular of the 294 open issues. I am sorry this has not been addressed yet. Or your other suggestions. I apologise on behalf of the world of people with the skills to fix it who have not done so. (Yes, I am joking on the last item. I empathise with your frustration, but taking it out on the long serving project owner is counter-productive.)
Some quick thoughts:
scalar: out-of-scope, open issue is Disallow multiple arguments (array) for individual key #1318range()andinteger(): not in first instance, get the imploding sorted before considering thoseplroebuck commentedon Jul 5, 2023
My apologies to @bcoe during a moment of frustration -- I've had to explain not getting issues resolved myself. But since the request was a post against
yargsv13.2.1 and the package is currentlyv17.7.2, please help correct this outstanding software bug.shadowspawn commentedon Oct 22, 2023
I did some experimentation today, and currently leaning towards generating an error instead of returning
NaNfortype: 'number'. Hand-rolling your own validation with informative error messages is a bit of work for author. Silent parsing toNaNcan be achieved easily for the main cases by passingcoerce: parseFloat. Having yargs do the probably more commonly wanted and more difficult job seems appropriate.shadowspawn commentedon Oct 22, 2023
The big problem is changing an 8 year old behaviour! (Yes, could add more configuration, but in first instance would rather not.)
Is anyone following this issue keen to preserve the current behaviour of silently returning
NaNfor parsing failures?plroebuck commentedon Oct 23, 2023
Cannot think of any reason someone would ever desire such an odd and unexpected behavior. If nothing is passed for a
numberoption on the cmdline, it's just plain wrong -- user error. If an alphabetic is passed to anumberoption on the cmdline, it's also just plain wrong -- yet another user error. The current behavior seems more likely to cause an issue than prevent one; I still vote tossing the current behavior.