Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commander can't handle multiple command arguments

Tags:

node.js

I have the following commander command with multiple arguments:

var program = require('commander');

program
  .command('rename <id> [name]')
  .action(function() {
    console.log(arguments);
  });

program.parse(process.argv);

Using the app yields the following result:

$ node app.js 1 "Hello"
{ '0': '1',
  '1':
   { commands: [],
     options: [],
     _execs: [],
     _args: [ [Object] ],
     _name: 'rename',
     parent:
      { commands: [Object],
        options: [],
        _execs: [],
        _args: [],
        _name: 'app',
        Command: [Function: Command],
        Option: [Function: Option],
        _events: [Object],
        rawArgs: [Object],
        args: [Object] } } }

As you can see, the action receives the first argument (<id>) and program, but doesn't receives the second argument: [name].

I've tried:

  • Making [name] a required argument.
  • Passing the name unquoted to the tool from the command line.
  • Simplifying my real app into the tiny reproducible program above.
  • Using a variadic argument for name (rename <id> [name...]), but this results on both 1 and Hello to being assigned into the same array as the first parameter to action, defeating the purpose of having id.

What am I missing? Does commander only accepts one argument per command (doesn't looks so in the documentation)?

like image 535
jviotti Avatar asked Nov 21 '14 16:11

jviotti


1 Answers

I think this was a bug in an old version of commander. This works now with [email protected].

like image 58
bolav Avatar answered Nov 08 '22 18:11

bolav