Skip to content

Commander unsafely allows commands with duplicate names to be registered #1903

Closed
@huntie

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.

Metadata

Assignees

No one assigned

    Labels

    semver: majorReleasing requires a major version bump, not backwards compatible

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions