yargs / yargs-parser

:muscle: the mighty option parser used by yargs
http://yargs.js.org/
ISC License
493 stars 118 forks source link

Arrays and values with hyphens #217

Open bradisbell opened 8 years ago

bradisbell commented 8 years ago

I need to pass in an array of values that have hyphens. I can't seem to figure out the required syntax.

node app.js --arrOption "-val1" "-val2" "-val3"
Result: []
node app.js --arrOption "-val1" --arrOption "-val2" --arrOption "-val3"
Result: [ [], [] ]
node app.js --arrOption="-val1" --arrOption="-val2" --arrOption="-val3"
Result: [ [], '-val3' ]

Desired result:

[ '-val1', '-val2', '-val3' ]

How can I achieve this?

nexdrew commented 8 years ago

@bradisbell Thanks for reporting!

This seems to be a bug with yargs-parser:

var parse = require('yargs-parser')
var argv = parse(process.argv.slice(2), {
  array: 'arrOption'
})
console.log(argv)
$ node issue621.js --arrOption "-val1" "-val2" "-val3"
{ _: [],
  arrOption: [],
  v: [ true, true, true ],
  a: [ true, true, true ],
  l: [ 1, 2, 3 ] }

$ node issue621.js --arrOption "-val1" --arrOption "-val2" --arrOption "-val3"
{ _: [],
  arrOption: [ [], [] ],
  v: [ true, true, true ],
  a: [ true, true, true ],
  l: [ 1, 2, 3 ] }

$ node issue621.js --arrOption="-val1" --arrOption="-val2" --arrOption="-val3"
{ _: [],
  arrOption: [ [], '-val3' ],
  v: [ true, true ],
  a: [ true, true ],
  l: [ 1, 2 ] }

Oddly enough, I can produce the desired result if I don't explicitly define the option as an array type:

// yargs-parser
var parse = require('yargs-parser')
var argv = parse(process.argv.slice(2))
console.log(argv)

// or yargs
argv = require('yargs').argv
console.log(argv)
$ node issue621.js --arrOption="-val1" --arrOption="-val2" --arrOption="-val3"
{ _: [], arrOption: [ '-val1', '-val2', '-val3' ] }
{ _: [],
  arrOption: [ '-val1', '-val2', '-val3' ],
  '$0': 'issue621.js' }

But that's certainly not acceptable. Sounds similar to yargs/yargs-parser#145, we'll need to fix this in yargs-parser.

christianbradley commented 6 years ago

I have this issue as well, any update @nexdrew ?

masaeedu commented 6 years ago

Is there any workaround for this?

rsalunga29 commented 6 years ago

is there a fix on this issue?