Assuming I wanted to create an 'apples' object containing all apple colours passed to it from the command line. Could be two colours, could be five colours... I seem to be able to do that with
node ./myscript.js --apples=red --apples=green --apples=yellow
Which successfully constructs:
{ _: [],
apples: [ 'red', 'green', 'yellow' ],
'$0': 'myscript.js' }
But is there a more succinct way to specify that the 'apples' key should construct its object using multiple arguments from the command line _without_ repeating the --apples key for each colour?
I wondered if --apples=Red,Green,Yellow might do the trick but perhaps understandably that takes 'Red,Green,Yellow' as a single string. Tried it with various ideas around spaces too, but that didn't seem to help either.
Thanks,
Steve
@steveharman You can tell yargs that apples should be an array, like this:
var argv = require('yargs')
.option('apples', {
type: 'array',
desc: 'One or more apple types/colors'
})
.argv
console.log(argv)
$ node issue686.js --apples red green yellow
{ _: [],
apples: [ 'red', 'green', 'yellow' ],
'$0': 'issue686.js' }
Is this similar to what you're looking for?
That, Andrew is not _similar_ to what I need, it's precisely what I need. Fantastic - thanks for the tip.
Steve
@steveharman Excellent! Let us know if you encounter any problems. Thanks for using yargs!
@steveharman I should note that you can also use coercion to do your own custom value parsing too, e.g. if you wanted to support a syntax like --apples red,green,yellow.
Most helpful comment
@steveharman You can tell yargs that
applesshould be an array, like this:Is this similar to what you're looking for?