Philip O'Brien
Philip O'Brien

Reputation: 4266

Multiple commands with different options using yargs/optimist

I'm trying to write a CLI for a node module using yargs but I'm having difficulty setting two different commands that could be used, one requiring only one parameter, but the other requiring two. Here is what I have so far

var argv = require('yargs')
   .option('r', {
       alias : 'reset',
       describe: 'clear data for site(s)',
       type: 'string'
   }).
   option('d', {
       alias : 'dashboard',
       describe: 'copy dashboard for the specified site across all others',
       type: 'array'
   })
   .usage('Usage: $0 -r [string] -d [array]')
   .argv;

To reset data I would do

node main.js -r mysite

But to copy a dashboard I want to be to do

node main.js -d theSiteToCopyFrom theSiteToCopyTo

or even

node main.js -d theSiteToCopyFrom [theArrayOfSitesToCopyTo, , ,] 

I have looked through the examples given, such as

var argv = require('yargs')
    .option('f', {
        alias : 'file',
        demand: true,
        default: '/etc/passwd',
        describe: 'x marks the spot',
        type: 'string'
    })
    .argv
;

but I can't see how to specify that multiple parameters are required for different commands.

Upvotes: 5

Views: 9894

Answers (1)

Ben Coe
Ben Coe

Reputation: 46

I just released the nargs feature for yargs, which gives you this capability:

https://github.com/bcoe/yargs#nargskey-count

Upvotes: 3

Related Questions