Have help command call help directly for subcommands, when possible (#1864)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by GitHub · parent fac9d8c · Browse files · 2 files·+58 added−4 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+58 added−4 removed
- tests:+37 added−0 removed
- other:+21 added−4 removed
Provenance
- Commit
63abdac- Tree
ff21f1a- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
fac9d8c
2 files changed+58 added−4 removed
modified:lib/command.js+21 added-4 removed
1083 unmodified lines1084 return hookResult;1085 }1086 1087 /**1088+ * Invoke help directly if possible, or dispatch if necessary.1089+ * e.g. help foo1090+ *1091+ * @api private1092+ */1093+1094+ _dispatchHelpCommand(subcommandName) {1095+ if (!subcommandName) {1096+ this.help();1097+ }1098+ const subCommand = this._findCommand(subcommandName);1099+ if (subCommand && !subCommand._executableHandler) {1100+ subCommand.help();1101+ }1102+1103+ // Fallback to parsing the help flag to invoke the help.1104+ return this._dispatchSubcommand(subcommandName, [], [this._helpLongFlag]);1105+ }1106+1107+ /**1108 * Check this.args against expected this._args.1109 *1110 * @api private1111 */155 unmodified lines1267 if (operands && this._findCommand(operands[0])) {1268 return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);1269 }1270 if (this._hasImplicitHelpCommand() && operands[0] === this._helpCommandName) {1251- if (operands.length === 1) {1252- this.help();1253- }1254- return this._dispatchSubcommand(operands[1], [], [this._helpLongFlag]);1271+ return this._dispatchHelpCommand(operands[1]);1272 }1273 if (this._defaultCommandName) {1274 outputHelpIfRequested(this, unknown); // Run the help for default command from parent rather than passing to default command1275 return this._dispatchSubcommand(this._defaultCommandName, operands, unknown);921 unmodified linesmodified:tests/command.helpCommand.test.js+37 added
121 unmodified linesExpand all122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163121 unmodified linesExpand all expect(() => { program.parse('node test.js help'.split(' ')); }).toThrow('program'); });
test('when no long help flag then "help sub" works', () => { const program = new commander.Command(); program.exitOverride(); program.helpOption('-H'); const sub = program.command('sub'); // Patch help for easy way to check called. sub.help = () => { throw new Error('sub help'); }; expect(() => { program.parse(['help', 'sub'], { from: 'user' }); }).toThrow('sub help'); });
test('when no help options in sub then "help sub" works', () => { const program = new commander.Command(); program.exitOverride(); const sub = program.command('sub') .helpOption(false); // Patch help for easy way to check called. sub.help = () => { throw new Error('sub help'); }; expect(() => { program.parse(['help', 'sub'], { from: 'user' }); }).toThrow('sub help'); });
test('when different help options in sub then "help sub" works', () => { const program = new commander.Command(); program.exitOverride(); const sub = program.command('sub'); program.helpOption('-h, --help'); sub.helpOption('-a, --assist'); // Patch help for easy way to check called. sub.help = () => { throw new Error('sub help'); }; expect(() => { program.parse(['help', 'sub'], { from: 'user' }); }).toThrow('sub help'); });});