From ee631fe30b5d5c57e8fd8472c0c583fd6d942279 Mon Sep 17 00:00:00 2001 From: John Gee Date: Sat, 8 Feb 2020 13:53:38 +1300 Subject: [PATCH 1/2] Suggest help when unknown command --- index.js | 7 ++++++- tests/command.exitOverride.test.js | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9dc4aa8a1..bd8a834ad 100644 --- a/index.js +++ b/index.js @@ -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} --help'.`; console.error(message); this._exit(1, 'commander.unknownCommand', message); }; diff --git a/tests/command.exitOverride.test.js b/tests/command.exitOverride.test.js index fb76504bb..52917ca38 100644 --- a/tests/command.exitOverride.test.js +++ b/tests/command.exitOverride.test.js @@ -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'); @@ -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. From 225ab71089fe5f72ba44a840801ac11d8117be19 Mon Sep 17 00:00:00 2001 From: John Gee Date: Sat, 8 Feb 2020 14:05:09 +1300 Subject: [PATCH 2/2] Use custom help flag in message --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index bd8a834ad..b869cb640 100644 --- a/index.js +++ b/index.js @@ -1129,7 +1129,7 @@ class Command extends EventEmitter { partCommands.unshift(parentCmd.name()); } const fullCommand = partCommands.join(' '); - const message = `error: unknown command '${this.args[0]}'. See '${fullCommand} --help'.`; + const message = `error: unknown command '${this.args[0]}'. See '${fullCommand} ${this._helpLongFlag}'.`; console.error(message); this._exit(1, 'commander.unknownCommand', message); };