commander.js icon indicating copy to clipboard operation
commander.js copied to clipboard

9.1.0 - option.conflicts not working

Open rpearce63 opened this issue 4 years ago • 5 comments

I'm trying to use the option conflicts configuration, but it doesn't appear to catch the conflicts.

const { program, Option } = require("commander");

const handleAction = (command) => {
  const options = program.opts();
  //options.exact && (options.wild || options.all) && console.log("conflict");
  console.log(
    `${command.name()} with options ${JSON.stringify(program.opts())}`
  );
};
//Global options
program
  .addOption(
    new Option("-a, --all", "all wallets").conflicts(["exact", "wild"])
  )
  .addOption(
    new Option("-x, --exact <name>", "exact wallet name").conflicts([
      "all",
      "wild",
    ])
  )
  .addOption(
    new Option("-w, --wild <partial>", "wild card").conflicts(["all", "exact"])
  )
  .option("-p, --password <password>");

//Sub commands
program.command("claim").action((options, command) => {
  handleAction(command);
});

program.parse();

When I run with: node commands.js claim -a -x OG

The result is: claim with options {"all":true,"exact":"OG"}

Have I configured it incorrectly? Also tried specifying just the short names. Same result.

Thanks.

rpearce63 avatar Mar 24 '22 15:03 rpearce63

Thanks for the example.

This is a bug. When a subcommand is used, the conflicts are only being checked on the subcommand and not the program.

(In the code it looks right with _checkForConflictingOptions in same place as _checkForMissingMandatoryOptions, but that second call walks up the stack.)

shadowspawn avatar Mar 24 '22 19:03 shadowspawn

Any idea when this might be released?

rpearce63 avatar Apr 09 '22 14:04 rpearce63

Hi @rpearce63 , likely within a week.

(Not a hard rule, but I prefer to wait a month between releases.)

shadowspawn avatar Apr 09 '22 20:04 shadowspawn

Fixed in Commander v9.2.0

shadowspawn avatar Apr 15 '22 07:04 shadowspawn

The update worked. Thanks.

 % node commands.js claim -a -x OG
error: option '-a, --all' cannot be used with option '-x, --exact <name>'

rpearce63 avatar Apr 15 '22 15:04 rpearce63