Skip to content

Commit

Permalink
Have help command call help directly for subcommands, when possible (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn authored Apr 21, 2023
1 parent fac9d8c commit 63abdac
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,26 @@ Expecting one of '${allowedValues.join("', '")}'`);
return hookResult;
}

/**
* Invoke help directly if possible, or dispatch if necessary.
* e.g. help foo
*
* @api private
*/

_dispatchHelpCommand(subcommandName) {
if (!subcommandName) {
this.help();
}
const subCommand = this._findCommand(subcommandName);
if (subCommand && !subCommand._executableHandler) {
subCommand.help();
}

// Fallback to parsing the help flag to invoke the help.
return this._dispatchSubcommand(subcommandName, [], [this._helpLongFlag]);
}

/**
* Check this.args against expected this._args.
*
Expand Down Expand Up @@ -1248,10 +1268,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
return this._dispatchSubcommand(operands[0], operands.slice(1), unknown);
}
if (this._hasImplicitHelpCommand() && operands[0] === this._helpCommandName) {
if (operands.length === 1) {
this.help();
}
return this._dispatchSubcommand(operands[1], [], [this._helpLongFlag]);
return this._dispatchHelpCommand(operands[1]);
}
if (this._defaultCommandName) {
outputHelpIfRequested(this, unknown); // Run the help for default command from parent rather than passing to default command
Expand Down
37 changes: 37 additions & 0 deletions tests/command.helpCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,41 @@ describe('help command processed on correct command', () => {
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');
});
});

0 comments on commit 63abdac

Please sign in to comment.