0

I was looking at all the customization provided by commander npm package, its really good to have, but I would like to know if there is any way to find if user has provided un-supported option.

For un-supported command we can write: (but not sure about un-supported options)

const commander = require('commander');

this.applicationCommander = new commander.Command();

this.applicationCommander.command('*').action(this.applicationCommander.help);
Sudhir Dhumal
  • 902
  • 11
  • 22
  • The universal behaviour for CLI tools, however, is to trigger this on both `--help` and when no arguments are passed, and to simply ignore unknown args. You don't show the help printout on unsupported flags: they're unsupported, your CLI tool already does nothing with them, so there are no errors as far as execution is concerned, and the CLI tool should just run using the args it does know. – Mike 'Pomax' Kamermans Mar 08 '20 at 16:35
  • Agreed, but I would like to show help menu in case of invalid options. It's just me who wants to show output of ```--help``` :) its always good to have all sorts of customization (atleast that what I believe). – Sudhir Dhumal Mar 08 '20 at 16:44
  • the problem with doing that is that people expect your CLI util to generate true `stdout` text. Showing the help on unsupported flags, rather than doing what it's supposed to, means it can't be chained with other CLI utilities. So... really, you shouldn't want to, you should followed the example that everyone else has set. You man page/`--help` output is already useful enough, people will be reading it to see what they can do. – Mike 'Pomax' Kamermans Mar 08 '20 at 16:57
  • 1
    Agreed, and makes sense (will make change), but will keep the question open for possible ways to do so. – Sudhir Dhumal Mar 08 '20 at 19:23
  • Just an update - Was looking at the commander package's source and looks like there is no way to do so. On invalid/unknown option commander exits the program. – Sudhir Dhumal Mar 08 '20 at 19:40
  • 2
    You can always ask them. It's an open source package, nothing stopping you from filing an issue for it. – Mike 'Pomax' Kamermans Mar 08 '20 at 22:17
  • I expanded my answer to include displaying the help in case of invalid options. – shadowspawn Mar 11 '20 at 22:46

1 Answers1

1

Commander by default will display an error for unknown options on the main command:

const program = require('commander');
program.parse(process.argv);
$ node index.js --silly
error: unknown option '--silly'

The relevant information from the Commander v4 README which covers subcommands is:

A command's options on the command line are validated when the command is used. Any unknown options will be reported as an error. However, if an action-based command does not define an action, then the options are not validated.

If you want to do extra handling yourself rather than exit when an unknown option is encountered, take a look at .exitOverride(). For example:

const commander = require('commander');
const program = new commander.Command();
program.exitOverride(); // throw instead of exit

try {
  program.parse(process.argv);
} catch (err) {
  if (err.code === 'commander.unknownOption') {
    console.log();
    program.outputHelp();
  }
}
$ node index.js --silly
error: unknown option '--silly'

Usage: index [options]

Options:
  -h, --help  output usage information
shadowspawn
  • 3,039
  • 22
  • 26