Skip to content

Commit

Permalink
Store help option instance instead of just flags
Browse files Browse the repository at this point in the history
Analogous to 3c94dfd in tj#1933 for version option.

Storing the Option instance of the help option instead of just its
flags (_helpShortFlag, _helpLongFlag) is meaningful for cases when other
properties of the instance need to be accessed (not currently the case,
but could be in the future).
  • Loading branch information
aweebit committed Aug 3, 2023
1 parent 1bb30d7 commit 9857b1e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
10 changes: 4 additions & 6 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ 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._helpOption.long]);
}

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

this.emit(this._helpLongFlag); // deprecated
this.emit(this._helpOption.long); // deprecated
this.emit('afterHelp', context);
getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context));
}
Expand All @@ -2056,9 +2056,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
this._helpFlags = flags = flags || this._helpFlags;
this._helpDescription = description = description || this._helpDescription;

const helpOption = this.createOption(flags, description);
this._helpShortFlag = helpOption.short;
this._helpLongFlag = helpOption.long;
this._helpOption = this.createOption(flags, description);

return this;
}
Expand Down Expand Up @@ -2123,7 +2121,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/

function outputHelpIfRequested(cmd, args) {
const helpOption = cmd._hasHelpOption && args.find(arg => arg === cmd._helpLongFlag || arg === cmd._helpShortFlag);
const helpOption = cmd._hasHelpOption && args.find(arg => arg === cmd._helpOption.long || arg === cmd._helpOption.short);
if (helpOption) {
cmd.outputHelp();
// (Do not have all displayed text available so only passing placeholder.)
Expand Down
8 changes: 4 additions & 4 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ class Help {
visibleOptions(cmd) {
const visibleOptions = cmd.options.filter((option) => !option.hidden);
// Implicit help
const showShortHelpFlag = cmd._hasHelpOption && cmd._helpShortFlag && !cmd._findOption(cmd._helpShortFlag);
const showLongHelpFlag = cmd._hasHelpOption && !cmd._findOption(cmd._helpLongFlag);
const showShortHelpFlag = cmd._hasHelpOption && cmd._helpOption.short && !cmd._findOption(cmd._helpOption.short);
const showLongHelpFlag = cmd._hasHelpOption && !cmd._findOption(cmd._helpOption.long);
if (showShortHelpFlag || showLongHelpFlag) {
let helpOption;
if (!showShortHelpFlag) {
helpOption = cmd.createOption(cmd._helpLongFlag, cmd._helpDescription);
helpOption = cmd.createOption(cmd._helpOption.long, cmd._helpDescription);
} else if (!showLongHelpFlag) {
helpOption = cmd.createOption(cmd._helpShortFlag, cmd._helpDescription);
helpOption = cmd.createOption(cmd._helpOption.short, cmd._helpDescription);
} else {
helpOption = cmd.createOption(cmd._helpFlags, cmd._helpDescription);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/command.copySettings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ describe('copyInheritedSettings property tests', () => {
cmd.copyInheritedSettings(source);
expect(cmd._helpFlags).toBe('-Z, --zz');
expect(cmd._helpDescription).toBe('ddd');
expect(cmd._helpShortFlag).toBe('-Z');
expect(cmd._helpLongFlag).toBe('--zz');
expect(cmd._helpOption.short).toBe('-Z');
expect(cmd._helpOption.long).toBe('--zz');
});

test('when copyInheritedSettings then copies addHelpCommand(name, description)', () => {
Expand Down

0 comments on commit 9857b1e

Please sign in to comment.