Skip to content

Commit 28b649d

Browse files
authored
Suggest help when unknown command (#1179)
* Suggest help when unknown command * Use custom help flag in message
1 parent 1704022 commit 28b649d

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,12 @@ class Command extends EventEmitter {
11241124
*/
11251125

11261126
unknownCommand() {
1127-
const message = `error: unknown command '${this.args[0]}'`;
1127+
const partCommands = [this.name()];
1128+
for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
1129+
partCommands.unshift(parentCmd.name());
1130+
}
1131+
const fullCommand = partCommands.join(' ');
1132+
const message = `error: unknown command '${this.args[0]}'. See '${fullCommand} ${this._helpLongFlag}'.`;
11281133
console.error(message);
11291134
this._exit(1, 'commander.unknownCommand', message);
11301135
};

tests/command.exitOverride.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe('.exitOverride and error details', () => {
5454
test('when specify unknown command then throw CommanderError', () => {
5555
const program = new commander.Command();
5656
program
57+
.name('prog')
5758
.exitOverride()
5859
.command('sub');
5960

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

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

7172
// Same error as above, but with custom handler.

0 commit comments

Comments
 (0)