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
{{ message }}
Repository navigation
Disallow multiple arguments (array) for individual key #1318
"duplicate-arguments-array" exists as a "yargs-parser" option to set this globally.
But I don't see how to leave it enabled globally and disable it on key-by-key basis (or vice versa).
Need both behaviors.
Desired Outcome
Example has two arguments:
collection: Value is an array of all provided values
#229 (Disallow multiple arguments (array) for a key) [original issue] #530 (Disable "duplicates" behaviour) #586 (Add coerce API)
Reacted by Grigory Zaripov, Michael Ledin, Rohit Vighne, Florian Guitton, Søren Louv-Jansen, Yuya Tanaka, gnoownow10, mizdra, Michael Hladky, Jan and 2 more
I found this quite confusing as well. I'm seeing that by default, if I specify a type like number or string, or even set a choice of multiple string values, my options can end up as arrays, even if I enable strict parsing mode.
I would have expected any option key that specifies a type or choice, but does not specify array: true, to throw an error if the option is passed multiple times. If I give a primitive type, then I mean I want a single value for that option!
Due to compatibility, maybe it would be more feasible to only enable this behavior in strict parsing mode, or perhaps also require setting array: false for individual options? @bcoe let me know if you think one of these options is worth pursuing and I will try to write a PR.
In the meantime people need to be aware of this gotcha, or arguments will not have their expected types under all circumstances, even in strict mode. Depending on what the program does with the argument, this could be pretty bad. Here's what I am currently doing for additional validation:
constprogram=require('yargs').usage( ... ).option('string-arg1',{type: 'string'})....strict();constargv=program.argv;functionusageAndExit(errorMessage){if(errorMessage){console.error('Error: %s',errorMessage);console.error();}program.showHelp();process.exit(1);}['stringArg1','stringArg2','numberArg','choiceArg'].forEach(arg=>{if(argv[arg]&&Array.isArray(argv[arg])){usageAndExit(util.format('Invalid value for --%s: %o',arg,argv[arg]));}});
But yeah, an option that should be included in arguments only once is such a trivial case, it makes no sense at all that there's no simple way to specify it. Especially considering that you set the type of option as a simple value (number/string) and then you get it as an array, forcing you to perform unnecessary checks.
Reacted by David Murdoch, Jonathan Buhacoff and Ondrej Medek
However, I vote for some option like "singleValue: true" for simplicity, since this is very common usage: that we want just one value for the option and we want to stop with error if multiple options are on the command line.
Activity
juergba commentedon Mar 21, 2019
Within "yargs-parser" you can define
collectionas an array:array: ['collection'].duplicate-arguments-arrayhas no effect on yargs typearray.juergba commentedon Mar 21, 2019
duplicate-arguments-arrayis not working consistently. The output is different fornumbertype than forstringtype:nylen commentedon Apr 5, 2019
I found this quite confusing as well. I'm seeing that by default, if I specify a type like
numberorstring, or even set achoiceof multiple string values, my options can end up as arrays, even if I enable strict parsing mode.I would have expected any option key that specifies a
typeorchoice, but does not specifyarray: true, to throw an error if the option is passed multiple times. If I give a primitive type, then I mean I want a single value for that option!Due to compatibility, maybe it would be more feasible to only enable this behavior in strict parsing mode, or perhaps also require setting
array: falsefor individual options? @bcoe let me know if you think one of these options is worth pursuing and I will try to write a PR.In the meantime people need to be aware of this gotcha, or arguments will not have their expected types under all circumstances, even in strict mode. Depending on what the program does with the argument, this could be pretty bad. Here's what I am currently doing for additional validation:
danielvy commentedon May 22, 2019
@nylen
You can use
.check()to validate count of arguments. It will make code more clear and concise.But yeah, an option that should be included in arguments only once is such a trivial case, it makes no sense at all that there's no simple way to specify it. Especially considering that you set the type of option as a simple value (number/string) and then you get it as an array, forcing you to perform unnecessary checks.
patarapolw commentedon Dec 22, 2019
Too check all, I resorted to
mleguen commentedon Jan 8, 2020
Some tests with the the current master branch of yargs.
With the default configuration:
$ node index.js --collection 1 --collection 2 3 --scalar 5 --scalar 6 7 { _: [ 7 ], collection: [ 1, 2, 3 ], scalar: [ 5, 6 ], '$0': 'index.js' }If disabling
duplicate-arguments-array:$ node index.js --collection 1 --collection 2 3 --scalar 5 --scalar 6 7 { _: [ 7 ], collection: [ 2, 3 ], scalar: 6, '$0': 'index.js' }And even if specifying
collectionasstring:$ node index.js --collection 1 --collection 2 3 --scalar 5 --scalar 6 7 { _: [ 7 ], collection: [ '2', '3' ], scalar: 6, '$0': 'index.js' }To summarize:
array: false)array: truearray: true)array: trueSo:
.array()seems to be doing only half of what it is promising in the doc (the "multiple values for a single option occurrence" part)IMHO:
array()is called (or set) for a key, we should only keep the last value of the array provided byyargs-parserarray()that it will only partially work if "duplicate-arguments-array" is disabled@bcoe @juergba What is your opinion on this?
markus456 commentedon Jan 5, 2021
This seems to also affect variadic positional arguments:
Removing
"duplicate-arguments-array": falsefrom the parser configuration results in expected output:Explicitly specifying the type of the variadic argument as
arraywith.positionaldoesn't seem to affect it. This was with yargs 16.2.0.the-spyke commentedon Feb 6, 2021
Yargs
16.2.0havingand calling
mycommand -s asd -s qweI get{ "_": [], "s": "qwe", "source": [ "asd", "qwe" ], "$0": "mycommand" }Notice that
sis okay, butsourceis still an array.xmedeko commentedon Aug 1, 2025
It's possible to use
coerceto check multiple values:However, I vote for some option like "singleValue: true" for simplicity, since this is very common usage: that we want just one value for the option and we want to stop with error if multiple options are on the command line.
Validate that non-array args can't be passed twice
Validate that non-array args can't be passed twice