Skip to content

Commit

Permalink
Use _getCommandAndAncestors() less aggressively
Browse files Browse the repository at this point in the history
Only call when all elements are to be iterated.
Resort to manual iteration via .parent in other cases.
  • Loading branch information
aweebit authored and abetomo committed Aug 19, 2023
1 parent 25ebead commit f76a734
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -1761,13 +1761,14 @@ Expecting one of '${allowedValues.join("', '")}'`);
if (flag.startsWith('--') && this._showSuggestionAfterError) {
// Looping to pick up the global options too
let candidateFlags = [];
for (const command of this._getCommandAndAncestors()) {
let command = this;
do {
const moreFlags = command.createHelp().visibleOptions(command)
.filter(option => option.long)
.map(option => option.long);
candidateFlags = candidateFlags.concat(moreFlags);
if (command._enablePositionalOptions) break;
}
command = command.parent;
} while (command && !command._enablePositionalOptions);
suggestion = suggestSimilar(flag, candidateFlags);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ class Help {
if (!this.showGlobalOptions) return [];

const globalOptions = [];
cmd._getCommandAndAncestors().slice(1).forEach((ancestorCmd) => {
for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
globalOptions.push(...visibleOptions);
});
}
if (this.sortOptions) {
globalOptions.sort(this.compareOptions);
}
Expand Down Expand Up @@ -241,9 +241,9 @@ class Help {
cmdName = cmdName + '|' + cmd._aliases[0];
}
let ancestorCmdNames = '';
cmd._getCommandAndAncestors().slice(1).forEach((ancestorCmd) => {
for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
ancestorCmdNames = ancestorCmd.name() + ' ' + ancestorCmdNames;
});
}
return ancestorCmdNames + cmdName + ' ' + cmd.usage();
}

Expand Down

0 comments on commit f76a734

Please sign in to comment.