使用 yargs/optimist 的具有不同选项的多个命令

Multiple commands with different options using yargs/optimist

我正在尝试使用 yargs 为节点模块编写 CLI,但我很难设置两个可以使用的不同命令,一个只需要一个参数,而另一个需要两个。这是我目前所拥有的

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: [=11=] -r [string] -d [array]')
   .argv;

我会重置数据

node main.js -r mysite

但是要复制我想做的仪表板

node main.js -d theSiteToCopyFrom theSiteToCopyTo

甚至

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

我已经看过给出的例子,比如

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

但我看不到如何指定不同命令需要多个参数。

我刚刚发布了 yargs 的 nargs 功能,它为您提供了以下功能:

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