-
Notifications
You must be signed in to change notification settings - Fork 994
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
Comments
@steveharman You can tell yargs that 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 |
This is old issue but I found this from search for how to use multiple values. Here is what I try and it just work. 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:
Something like this will not work:
Because you will got |
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:
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
The text was updated successfully, but these errors were encountered: