0

「test.js」というファイルがあるとします。

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

次に実行します:

>node test command

このエラーが発生しました:

オプションの 2 番目の引数はオブジェクトでなければなりません

.command の 3 番目のパラメーターを削除すると:

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

すべて順調。

私はばかげた間違いをしなければなりません。しかし、私はそれを理解することはできません。

ありがとう

4

1 に答える 1

0

3 番目の引数は必須ではないため、削除すると機能します。3番目の引数は関数呼び出しでなければならないと確信しています。

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>node app.js コマンド -c run

あなたのコードに含めることができますconsole.log(args.c);//prints out run

于 2016-10-17T23:03:02.273 に答える