yargs / yargs-parser

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

greedy arrays seems not work #393

Open banli17 opened 3 years ago

banli17 commented 3 years ago
const parser = require('yargs-parser')

const args = parser(process.argv.slice(2), {
    //array: ['foo'],
    configuration: {
        "greedy-arrays": true
    }
})
console.log(args);
]$ v-cli --foo apple banana cat
{ _: [ 'banana', 'cat' ], foo: 'apple' }

how to get { _: [ ], foo: ['apple', 'banana', 'cat' ] } Instead of specifying array: ['foo'],

sdta25196 commented 3 years ago

same question。Did you solve it?

ghiscoding commented 2 years ago

I also had this problem then I looked at all the options and if I understood correctly we need to tell the parser that we want it to be converted to array by using the array option as follow

const args = parser(process.argv.slice(2), {
    array: ['--foo', 'apple', 'banana', 'cat'],
    configuration: {
        "greedy-arrays": true
    }
})

so I basically pass the arguments array to both the first argument and also to the array property and now it works, not exactly trivial

laggingreflex commented 2 years ago

Isn't the correct way just:

parser(process.argv.slice(2), {
  array: ['foo'],
})
cli --foo apple banana cat

I.e. you still need to tell the parser whether to treat an option as an array, before greedy parsing even comes into play. And greedy-arrays are already true by default (as of v21)

@banli17 I see you asked the same question in #372. I hope the above answers it.