Skip to content
Myr

Suggest help when unknown command (#1179)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Suggest help when unknown command * Use custom help flag in message

authored by John Gee · committed by GitHub · parent 1704022 · Browse files · 2 files+8 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+8 added−2 removed

  • tests:+2 added−1 removed
  • other:+6 added−1 removed

2 files+8 added−2 removed

modified:tests/command.exitOverride.test.js+2 added-1 removed
52 unmodified lines
5354 test('when specify unknown command then throw CommanderError', () => {55 const program = new commander.Command();56 program57 .name('prog')58 .exitOverride()59 .command('sub');6061 let caughtErr;
3 unmodified lines
65 caughtErr = err;66 }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'.");70 });7172 // Same error as above, but with custom handler.73 test('when supply custom handler then throw custom error', () => {
136 unmodified lines
modified:index.js+6 added-1 removed
1122 unmodified lines
1123 * @api private1124 */11251126 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}'.`;1133 console.error(message);1134 this._exit(1, 'commander.unknownCommand', message);1135 };1136
548 unmodified lines