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

Syntax for passing multiple arguments of the same key? #686

Closed
steveharman opened this issue Oct 21, 2016 · 5 comments
Closed

Syntax for passing multiple arguments of the same key? #686

steveharman opened this issue Oct 21, 2016 · 5 comments

Comments

@steveharman
Copy link

steveharman commented Oct 21, 2016

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

@nexdrew
Copy link
Member

nexdrew commented Oct 21, 2016

@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?

@steveharman
Copy link
Author

That, Andrew is not similar to what I need, it's precisely what I need. Fantastic - thanks for the tip.

Steve

@nexdrew
Copy link
Member

nexdrew commented Oct 21, 2016

@steveharman Excellent! Let us know if you encounter any problems. Thanks for using yargs!

@nexdrew
Copy link
Member

nexdrew commented Oct 21, 2016

@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.

@ve3
Copy link

ve3 commented Jul 4, 2022

This is old issue but I found this from search for how to use multiple values.
So, the OP's question and nexdrew's answer is very useful to me but need a little improvement especially about how to use the command.

Here is what I try and it just work.
The code:

const yargs = require('yargs/yargs');
const {hideBin} = require('yargs/helpers');
const yargv = yargs(hideBin(process.argv));

yargv
.command('options [options]', 'Multiple option values.', (yargs) => {
    return yargs.option('color', {
        describe: 'Enter color name.',
        type: 'array'
    })
}, showMultiOptVals)
.help()
.argv;

function showMultiOptVals(argv) {
    console.log('argv: ', argv);
}

Yes, nothing changed but I added the code about using it in ES modules.

The code for ES modules (only for if you use new JS modules instead):

export const command = 'options [options]';
export const describe = 'Multiple option values.';
export const builder = (yargs) => {
    return yargs.options({
        'color': {
            //choices: ['red', 'blue'], // maybe limited by choices here.
            describe: 'Enter color name.',
            type: 'array',
        },
    });
};
export const handler = async (argv) => {
    console.log('argv: ', argv);
};

The command can be one of these lines:

$ node index.js options --color red blue
$ node index.js options --color=red blue
$ node index.js options --color=red --color=blue
$ node index.js options --color "red" --color "blue"
$ node index.js options --color "red" "blue"

Something like this will not work:

$ node index.js options --color=red,green

Because you will got color: [ 'red,green' ] instead of color: [ 'red', 'green' ].

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

No branches or pull requests

3 participants