Search code examples
yargs

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


Suppose I have a file "test.js":

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

Then I run:

>node test command

I got this error:

second argument to option must be an object

If I remove the 3rd parameter of .command:

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

Everything is fine.

I must make a dumb mistake. But I just cannot figure it out.

Thanks


Solution

  • Your 3rd argument is not required, that's why it works when you remove it. I'm pretty sure the 3rd argument has to be a function call.

    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;
    

    an example of usage might be:

    C:\WorkingDirectory>node app.js command -c run

    your code could include console.log(args.c);//prints out run