Description
I have a CLI application that uses commander.js and defines two commands, foo
and bar
. Since I am using git-style commands, basically my main file looks like this:
program
.command('foo', 'does something')
.command('bar', 'does something else')
.parse(process.argv);
if (process.argv.length === 2) {
program.help();
}
This works if I call my app without any arguments (then the help is shown, as intended), and it works if I call it with foo
or bar
as commands.
What does not work is if I specify a non-existing command. In this case, simply nothing happens at all:
$ myapp baz
How can I catch this case? I read that you may use
program.on('*', function () {
// ...
});
and basically this works, but this executes not only for unknown commands, but for any command. In the same way, specifying a wildcard command does not work either: If I add
program.command('*').action(function () { ... });
this catches unknown commands (which is exactly what I want), but now if you run the app with --help
it lists a *
command (which is obviously not what I want).
So, to cut a long story short: How do I deal correctly with unknown commands?