Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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