Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have help command call help directly for subcommands, when possible #1864

Merged
merged 2 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

@abetomo abetomo Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If subcommandName is undefined, this._findCommand(subcommandName) also returns undefined.

How about the following code?

if (!subcommandName) {
  this.help();
} else {
  const subCommand = this._findCommand(subcommandName);
  ...
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The help method calls exit, so the following code will not be reached if subcommandName is undefined.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense.

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');
});
});