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

[PR with tiny fixes] Fix help command for subcommands with an executable handler and only a short help flag. Do not use undefined long help option flag in legacy code. Change initial variable values in test for better error messages. #1930

Merged
merged 5 commits into from
Aug 19, 2023
Merged
8 changes: 6 additions & 2 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
}

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

/**
Expand Down Expand Up @@ -2034,7 +2036,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
}
context.write(helpInformation);

this.emit(this._helpLongFlag); // deprecated
if (this._helpLongFlag) {
shadowspawn marked this conversation as resolved.
Show resolved Hide resolved
this.emit(this._helpLongFlag); // deprecated
}
this.emit('afterHelp', context);
getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context));
}
Expand Down
10 changes: 5 additions & 5 deletions tests/command.addHelpText.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('context checks with full parse', () => {
});

test('when help requested then context.error is false', () => {
let context = {};
let context;
const program = new commander.Command();
program
.exitOverride()
Expand All @@ -193,7 +193,7 @@ describe('context checks with full parse', () => {
});

test('when help for error then context.error is true', () => {
let context = {};
let context;
const program = new commander.Command();
program
.exitOverride()
Expand All @@ -206,7 +206,7 @@ describe('context checks with full parse', () => {
});

test('when help on program then context.command is program', () => {
let context = {};
let context;
const program = new commander.Command();
program
.exitOverride()
Expand All @@ -218,7 +218,7 @@ describe('context checks with full parse', () => {
});

test('when help on subcommand and "before" subcommand then context.command is subcommand', () => {
let context = {};
let context;
const program = new commander.Command();
program
.exitOverride();
Expand All @@ -231,7 +231,7 @@ describe('context checks with full parse', () => {
});

test('when help on subcommand and "beforeAll" on program then context.command is subcommand', () => {
let context = {};
let context;
const program = new commander.Command();
program
.exitOverride()
Expand Down