yargs .command error: second argument to option must be an object

yargs .command error: second argument to option must be an object

假设我有一个文件"test.js":

var args = require('yargs')
        .command('command', 'command usage here', {alias: "c"} )
        .argv;

那我运行:

>node test command

我收到这个错误:

second argument to option must be an object

如果我删除 .command 的第三个参数:

 var args = require('yargs')
            .command('command', 'command usage here')
            .argv;

一切都很好。

我肯定犯了一个愚蠢的错误。但是我就是想不通。

谢谢

你的第三个参数不是必需的,这就是为什么当你删除它时它会起作用的原因。我很确定第三个参数必须是函数调用。

var args = require('yargs')
       .command('command',
                'command explanation', 
                 function(yargs){
                     //insert yargs.options here
                     yargs.options({
                         c:{
                             demand: true,//if you require the command
                             alias: 'c',// you will enter -c to use it
                             description: 'explain what it does'
                           }    
                 });
                 })
                 .argv;

一个用法示例可能是:

C:\WorkingDirectory>节点app.js 命令-c 运行

您的代码可以包括 console.log(args.c);//prints out run