Commander Cannot Handle Multiple Command Arguments

I have the following commander command with several arguments:

var program = require('commander'); program .command('rename <id> [name]') .action(function() { console.log(arguments); }); program.parse(process.argv); 

Using the application gives 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 does not receive the second argument: [name] .

I tried:

  • Create [name] required argument.
  • Passing a name not entered into the tool from the command line.
  • Simplifying my real application in the tiny reproducible program above.
  • Using a variational argument for name ( rename <id> [name...] ), but this leads to both 1 and Hello being assigned to the same array as the first parameter for the action, defeating the goal of having id .

What am I missing? Does the commander accept only one argument per command (doesn't look like this in the documentation)?

+5
source share
1 answer

I think it was a mistake in the old version of commander . Now it works with commander@2.9.0 .

+1
source

Source: https://habr.com/ru/post/1207422/


All Articles