Skip to content
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

Suggest help when unknown command #1179

Merged
merged 2 commits into from Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Expand Up @@ -1124,7 +1124,12 @@ class Command extends EventEmitter {
*/

unknownCommand() {
const message = `error: unknown command '${this.args[0]}'`;
const partCommands = [this.name()];
for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
partCommands.unshift(parentCmd.name());
}
const fullCommand = partCommands.join(' ');
const message = `error: unknown command '${this.args[0]}'. See '${fullCommand} ${this._helpLongFlag}'.`;
console.error(message);
this._exit(1, 'commander.unknownCommand', message);
};
Expand Down
3 changes: 2 additions & 1 deletion tests/command.exitOverride.test.js
Expand Up @@ -54,6 +54,7 @@ describe('.exitOverride and error details', () => {
test('when specify unknown command then throw CommanderError', () => {
const program = new commander.Command();
program
.name('prog')
.exitOverride()
.command('sub');

Expand All @@ -65,7 +66,7 @@ describe('.exitOverride and error details', () => {
}

expect(consoleErrorSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.unknownCommand', "error: unknown command 'oops'");
expectCommanderError(caughtErr, 1, 'commander.unknownCommand', "error: unknown command 'oops'. See 'prog --help'.");
});

// Same error as above, but with custom handler.
Expand Down