Commander.js: Help option does not display help when using requiredOption

Created on 4 Nov 2019  路  3Comments  路  Source: tj/commander.js

Hi,

I wanted to have one option required. Once I added one, the help message will no longer show unless you include the required option. I would have expected it to show the help.

I created some sample code, from one of your samples.

Test Code:

const commander = require("commander");
const program = new commander.Command();

program
  .option("-d, --debug", "output extra debugging")
  .requiredOption("-s, --small", "small pizza size")
  .option("-p, --pizza-type <type>", "flavour of pizza");

program.parse(process.argv);

if (program.debug) console.log(program.opts());
console.log("pizza details:");
if (program.small) console.log("- small pizza size");
if (program.pizzaType) console.log(`- ${program.pizzaType}`);

Output:

$ node test.js -h
error: required option '-s, --small' not specified

If I remove the requiredOption I get the expected output:

$ node test.js -h
Usage: test [options]

Options:
  -d, --debug              output extra debugging
  -s, --small              small pizza size
  -p, --pizza-type <type>  flavour of pizza
  -h, --help               output usage information
bug

Most helpful comment

Should be fixed in 4.0.1 which has been released. Thanks @aminch for report and clear description.

All 3 comments

Thanks. Reproduced. That is a bug and not the intended behaviour.

I consulted an old PR for the implementation pattern,and I thought it was a bit easy at the time! I have been untangling the command processing today and think I see a solid approach. Trying code now.

Should be fixed in 4.0.1 which has been released. Thanks @aminch for report and clear description.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RoXioTD picture RoXioTD  路  4Comments

mathiasbynens picture mathiasbynens  路  3Comments

DeoLeung picture DeoLeung  路  4Comments

snitin315 picture snitin315  路  4Comments

0505gonzalez picture 0505gonzalez  路  3Comments