-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
How to detect an unsupported command? #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
My $0.02 - by default commander.js should sanitize commands and automatically show usage and exit if an unsupported command is used (it doesn't currently). |
Basically I agree with you, but sometimes it could be useful to handle arbitrary input that is not a valid command. |
@chrishiestand That's not too hard to implement yourself, and like @goloroden, people might want other options. Unfortunately, at this time |
@SomeKittens: Why has this been closed? Just because what @chrishiestand is not too hard to implement, my original question has not yet been answered. Is
really the last word on this? |
So, I finally ended up with this: program
.version(...)
.command(...)
.on('*', function (command) {
this.commands.some(function (command) {
return command._name === argv[0];
}) || this.help();
})
parse(...); This does the job perfectly for me :-) |
For me: program.version(pkg.version)
.command("clean", "...")
// ...
.parse(process.argv)
if (cli.args.length < 1) {
program.help()
} else {
if (!program._execs[program.args[0]]) {
// unknown option
}
} |
@goloroden There's nothing wrong with your suggestion - on the contrary, this is something lots of folks hit. I don't think we'll be implementing a default at this time (but I want to revisit this when if/when the plugin system ever happens). |
I hit this same problem but I couldn't use @goloroden suggestion because I wasn't using git like commands. Instead I created a var executed = false;
program
.command('foo')
.action(foo);
program.parse(process.argv);
if (!executed) {
program.help();
}
function foo() {
executed = true;
// ...
} |
For those coming to this issue - the @goloroden solution does not work now. But there is official way mentioned in README: // error on unknown commands
program.on('command:*', function () {
console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));
process.exit(1);
}); |
In my use case, I wanted a single command that the cli program defaults to when executed.
|
But this seems to work for the root tree:
|
@vitalets This method wouldn't work on git-style subcommands. For example in main.js
In this case, even if I do |
I just ran into this as well, and this seems to work quite well, @frankcchen:
This is more or less a combination of the solution @goloroden offered at one point, and the adaption to a newer version of |
The proper way to print help info only when there is no command. see tj/commander.js#338 (comment)
Unsupported commands are detected by default from Commander v5, available now as a published pre-release (#1163) |
@vitalets That solution does not work with default commands:
|
@hacknaked has shown the current correct solution but I would add one tip. I would suggest adding program.command('404', { isDefault: true })
.argument('[args...]', 'Catch all arguments/flags provided.')
.allowUnknownOption()
.action((args) => {
// maybe show help as fallback.
program.help();
}); The below example is shown in the event NOTE: This below must be after you've defined all your // calls to program.command() above ↑
program.command('404')
.argument('[args...]', 'Catch all arguments and flags.')
.allowUnknownOption()
.action((args) => {
// do something w/ args or maybe show help!
program.help();
});
const argv = [...process.argv]; // clone argv.
const cleanArgv = argv.slice(2); // remove node path etc.
const firstArg = cleanArgv // first command ignore lfags.
.filter(v => !/^--?/.test(v))
.shift();
// empty argv fallback to help.
if (!cleanArgv.length)
argv.push('help');
// if unknown command insert "404" catch all.
else if (!program.commands.includes(firstArg))
argv.splice(2, 0, 'fallback');
program.parse(argv); |
I have a CLI application that uses commander.js and defines two commands,
foo
andbar
. Since I am using git-style commands, basically my main file looks like this: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
orbar
as commands.What does not work is if I specify a non-existing command. In this case, simply nothing happens at all:
How can I catch this case? I read that you may use
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
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?
The text was updated successfully, but these errors were encountered: