Commander unsafely allows commands with duplicate name
s to be registered #1903
Closed
Description
We ran into an issue recently in a project that depends on commander
, which dynamically defines commands based on plugins. Commander's internal array data structure for commands, and the Array.find
based lookup on name
, meant that repeat calls to command()
produced unexpected behaviour (the most recent call does not redeclare the command).
At minimum, I believe this should error.
Example
const { Command } = require('commander');
const program = new Command();
program.command('split').action((str, options) => console.log('1st'));
program.command('split').action((str, options) => console.log('2nd'));
program.parse();
Output will always be the first.
$ node example
Usage: example [options] [command]
Options:
-h, --help display help for command
Commands:
split
split
help [command] display help for command
$ node example split
1st
Expectation
Shouldn't allow duplicates and should throw an exception if a name
already exists.