Skip to content

Passing command option leaves content in args #1032

@oliversturm

Description

@oliversturm

As discussed here, opening as a new issue.

I'm trying to find out whether a command has been executed. The simple idea is that if a command is given, it should be executed (and nothing else). If no command is given, the default logic at the bottom should execute (but not otherwise). In all cases, the global option --load should be supported. Here's the test code:

var program = require('commander');

program.version('0.0.1').option('-l, --load <name>', 'load something');

program
  .command('doit [id]')
  .description('do something')
  .option('--better', 'do it better')
  .action((id, cmd) => {
    console.log('executing doit action');
    console.log('id is', id);
    console.log('global load option is ', cmd.parent.load);
    console.log('cmd.better is', cmd.better);
  });

program.parse(process.argv);

if (program.args.length === 0)
  console.log('started without command, executing main logic');

This works correctly when I execute like this:

➜ node test.js -l loadname doit myid        
executing doit action
id is myid
global load option is  loadname
cmd.better is undefined

However, when I pass the --better option to the command, the whole thing breaks and the "main logic" is executed in addition to the command:

➜ node test.js -l loadname doit --better myid
executing doit action
id is myid
global load option is  loadname
cmd.better is true
started without command, executing main logic

Unexpectedly, args contains information after the action has been executed, if the command option --better is passed.

Metadata

Metadata

Assignees

Labels

bugCommander is not working as intended

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions