Skip to content

Commit 777a452

Browse files
committed
Use _getCommandAndAncestors() consistently
1 parent aa280af commit 777a452

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
@@ -1428,13 +1428,13 @@ Call on top-level command instead`);
14281428

14291429
_checkForMissingMandatoryOptions() {
14301430
// Walk up hierarchy so can call in subcommand after checking for displaying help.
1431-
for (let cmd = this; cmd; cmd = cmd.parent) {
1431+
this._getCommandAndAncestors().forEach((cmd) => {
14321432
cmd.options.forEach((anOption) => {
14331433
if (anOption.mandatory && (cmd.getOptionValue(anOption.attributeName()) === undefined)) {
14341434
cmd.missingMandatoryOptionValue(anOption);
14351435
}
14361436
});
1437-
}
1437+
});
14381438
}
14391439

14401440
/**
@@ -1475,9 +1475,9 @@ Call on top-level command instead`);
14751475
*/
14761476
_checkForConflictingOptions() {
14771477
// Walk up hierarchy so can call in subcommand after checking for displaying help.
1478-
for (let cmd = this; cmd; cmd = cmd.parent) {
1478+
this._getCommandAndAncestors().forEach((cmd) => {
14791479
cmd._checkForConflictingLocalOptions();
1480-
}
1480+
});
14811481
}
14821482

14831483
/**
@@ -1806,14 +1806,13 @@ Call on top-level command instead`);
18061806
if (flag.startsWith('--') && this._showSuggestionAfterError) {
18071807
// Looping to pick up the global options too
18081808
let candidateFlags = [];
1809-
let command = this;
1810-
do {
1809+
for (const command of this._getCommandAndAncestors()) {
18111810
const moreFlags = command.createHelp().visibleOptions(command)
18121811
.filter(option => option.long)
18131812
.map(option => option.long);
18141813
candidateFlags = candidateFlags.concat(moreFlags);
1815-
command = command.parent;
1816-
} while (command && !command._enablePositionalOptions);
1814+
if (command._enablePositionalOptions) break;
1815+
}
18171816
suggestion = suggestSimilar(flag, candidateFlags);
18181817
}
18191818

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)