Skip to content

Commit

Permalink
Suggest help when unknown command (#1179)
Browse files Browse the repository at this point in the history
* Suggest help when unknown command

* Use custom help flag in message
  • Loading branch information
shadowspawn committed Feb 10, 2020
1 parent 1704022 commit 28b649d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
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

0 comments on commit 28b649d

Please sign in to comment.