Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

greedy arrays seems not work #393

Open
banli17 opened this issue Jun 15, 2021 · 3 comments
Open

greedy arrays seems not work #393

banli17 opened this issue Jun 15, 2021 · 3 comments
Labels

Comments

@banli17
Copy link

banli17 commented Jun 15, 2021

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'],

@bcoe bcoe added the question label Jun 20, 2021
@sdta25196
Copy link

same question。Did you solve it?

@ghiscoding
Copy link

ghiscoding commented Mar 1, 2022

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
Copy link
Contributor

laggingreflex commented May 1, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants