Skip to content
Myr

Use _getCommandAndAncestors() less aggressively

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Only call when all elements are to be iterated. Resort to manual iteration via .parent in other cases.

authored by Wee Bit · committed by Abe Tomoaki · parent 25ebead · Browse files · 2 files+8 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+8 added−7 removed

  • other:+8 added−7 removed

2 files+8 added−7 removed

modified:lib/command.js+4 added-3 removed
1759 unmodified lines
17601761 if (flag.startsWith('--') && this._showSuggestionAfterError) {1762 // Looping to pick up the global options too1763 let candidateFlags = [];1764 for (const command of this._getCommandAndAncestors()) {1764 let command = this;1765 do {1766 const moreFlags = command.createHelp().visibleOptions(command)1767 .filter(option => option.long)1768 .map(option => option.long);1769 candidateFlags = candidateFlags.concat(moreFlags);1769 if (command._enablePositionalOptions) break;1770 }1770 command = command.parent;1771 } while (command && !command._enablePositionalOptions);1772 suggestion = suggestSimilar(flag, candidateFlags);1773 }17741775 const message = `error: unknown option '${flag}'${suggestion}`;
427 unmodified lines
modified:lib/help.js+4 added-4 removed
99 unmodified lines
100 visibleGlobalOptions(cmd) {101 if (!this.showGlobalOptions) return [];102103 const globalOptions = [];104 cmd._getCommandAndAncestors().slice(1).forEach((ancestorCmd) => {104 for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {105 const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);106 globalOptions.push(...visibleOptions);107 });107 }108 if (this.sortOptions) {109 globalOptions.sort(this.compareOptions);110 }111 return globalOptions;
128 unmodified lines
240 if (cmd._aliases[0]) {241 cmdName = cmdName + '|' + cmd._aliases[0];242 }243 let ancestorCmdNames = '';244 cmd._getCommandAndAncestors().slice(1).forEach((ancestorCmd) => {244 for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {245 ancestorCmdNames = ancestorCmd.name() + ' ' + ancestorCmdNames;246 });246 }247 return ancestorCmdNames + cmdName + ' ' + cmd.usage();248 }249250 /**
214 unmodified lines