Skip to content

Commit 01251dc

Browse files
committed
Use _getCommandAndAncestors() consistently
(cherry picked from commit 777a452)
1 parent ed2b8c5 commit 01251dc

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

lib/command.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,13 +1378,13 @@ Expecting one of '${allowedValues.join("', '")}'`);
13781378

13791379
_checkForMissingMandatoryOptions() {
13801380
// Walk up hierarchy so can call in subcommand after checking for displaying help.
1381-
for (let cmd = this; cmd; cmd = cmd.parent) {
1381+
this._getCommandAndAncestors().forEach((cmd) => {
13821382
cmd.options.forEach((anOption) => {
13831383
if (anOption.mandatory && (cmd.getOptionValue(anOption.attributeName()) === undefined)) {
13841384
cmd.missingMandatoryOptionValue(anOption);
13851385
}
13861386
});
1387-
}
1387+
});
13881388
}
13891389

13901390
/**
@@ -1425,9 +1425,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
14251425
*/
14261426
_checkForConflictingOptions() {
14271427
// Walk up hierarchy so can call in subcommand after checking for displaying help.
1428-
for (let cmd = this; cmd; cmd = cmd.parent) {
1428+
this._getCommandAndAncestors().forEach((cmd) => {
14291429
cmd._checkForConflictingLocalOptions();
1430-
}
1430+
});
14311431
}
14321432

14331433
/**
@@ -1756,14 +1756,13 @@ Expecting one of '${allowedValues.join("', '")}'`);
17561756
if (flag.startsWith('--') && this._showSuggestionAfterError) {
17571757
// Looping to pick up the global options too
17581758
let candidateFlags = [];
1759-
let command = this;
1760-
do {
1759+
for (const command of this._getCommandAndAncestors()) {
17611760
const moreFlags = command.createHelp().visibleOptions(command)
17621761
.filter(option => option.long)
17631762
.map(option => option.long);
17641763
candidateFlags = candidateFlags.concat(moreFlags);
1765-
command = command.parent;
1766-
} while (command && !command._enablePositionalOptions);
1764+
if (command._enablePositionalOptions) break;
1765+
}
17671766
suggestion = suggestSimilar(flag, candidateFlags);
17681767
}
17691768

lib/help.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ class Help {
101101
if (!this.showGlobalOptions) return [];
102102

103103
const globalOptions = [];
104-
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {
105-
const visibleOptions = parentCmd.options.filter((option) => !option.hidden);
104+
cmd._getCommandAndAncestors().slice(1).forEach((ancestorCmd) => {
105+
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
106106
globalOptions.push(...visibleOptions);
107-
}
107+
});
108108
if (this.sortOptions) {
109109
globalOptions.sort(this.compareOptions);
110110
}
@@ -240,11 +240,11 @@ class Help {
240240
if (cmd._aliases[0]) {
241241
cmdName = cmdName + '|' + cmd._aliases[0];
242242
}
243-
let parentCmdNames = '';
244-
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {
245-
parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;
246-
}
247-
return parentCmdNames + cmdName + ' ' + cmd.usage();
243+
let ancestorCmdNames = '';
244+
cmd._getCommandAndAncestors().slice(1).forEach((ancestorCmd) => {
245+
ancestorCmdNames = ancestorCmd.name() + ' ' + ancestorCmdNames;
246+
});
247+
return ancestorCmdNames + cmdName + ' ' + cmd.usage();
248248
}
249249

250250
/**

0 commit comments

Comments
 (0)