diff --git a/lib/command.js b/lib/command.js index 590a271dd..d0868fd7e 100644 --- a/lib/command.js +++ b/lib/command.js @@ -109,6 +109,19 @@ class Command extends EventEmitter { return this; } + /** + * @returns {Command[]} + * @api private + */ + + _getCommandAndAncestors() { + const result = []; + for (let command = this; command; command = command.parent) { + result.push(command); + } + return result; + } + /** * Define a command. * @@ -825,7 +838,7 @@ Expecting one of '${allowedValues.join("', '")}'`); getOptionValueSourceWithGlobals(key) { // global overwrites local, like optsWithGlobals let source; - getCommandAndParents(this).forEach((cmd) => { + this._getCommandAndAncestors().forEach((cmd) => { if (cmd.getOptionValueSource(key) !== undefined) { source = cmd.getOptionValueSource(key); } @@ -1208,7 +1221,7 @@ Expecting one of '${allowedValues.join("', '")}'`); _chainOrCallHooks(promise, event) { let result = promise; const hooks = []; - getCommandAndParents(this) + this._getCommandAndAncestors() .reverse() .filter(cmd => cmd._lifeCycleHooks[event] !== undefined) .forEach(hookedCommand => { @@ -1365,13 +1378,13 @@ Expecting one of '${allowedValues.join("', '")}'`); _checkForMissingMandatoryOptions() { // Walk up hierarchy so can call in subcommand after checking for displaying help. - for (let cmd = this; cmd; cmd = cmd.parent) { + this._getCommandAndAncestors().forEach((cmd) => { cmd.options.forEach((anOption) => { if (anOption.mandatory && (cmd.getOptionValue(anOption.attributeName()) === undefined)) { cmd.missingMandatoryOptionValue(anOption); } }); - } + }); } /** @@ -1412,9 +1425,9 @@ Expecting one of '${allowedValues.join("', '")}'`); */ _checkForConflictingOptions() { // Walk up hierarchy so can call in subcommand after checking for displaying help. - for (let cmd = this; cmd; cmd = cmd.parent) { + this._getCommandAndAncestors().forEach((cmd) => { cmd._checkForConflictingLocalOptions(); - } + }); } /** @@ -1577,7 +1590,7 @@ Expecting one of '${allowedValues.join("', '")}'`); */ optsWithGlobals() { // globals overwrite locals - return getCommandAndParents(this).reduce( + return this._getCommandAndAncestors().reduce( (combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()), {} ); @@ -2022,7 +2035,7 @@ Expecting one of '${allowedValues.join("', '")}'`); } const context = this._getHelpContext(contextOptions); - getCommandAndParents(this).reverse().forEach(command => command.emit('beforeAllHelp', context)); + this._getCommandAndAncestors().reverse().forEach(command => command.emit('beforeAllHelp', context)); this.emit('beforeHelp', context); let helpInformation = this.helpInformation(context); @@ -2036,7 +2049,7 @@ Expecting one of '${allowedValues.join("', '")}'`); this.emit(this._helpLongFlag); // deprecated this.emit('afterHelp', context); - getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context)); + this._getCommandAndAncestors().forEach(command => command.emit('afterAllHelp', context)); } /** @@ -2179,18 +2192,4 @@ function incrementNodeInspectorPort(args) { }); } -/** - * @param {Command} startCommand - * @returns {Command[]} - * @api private - */ - -function getCommandAndParents(startCommand) { - const result = []; - for (let command = startCommand; command; command = command.parent) { - result.push(command); - } - return result; -} - exports.Command = Command; diff --git a/lib/help.js b/lib/help.js index 14e0fb9f3..560801bd3 100644 --- a/lib/help.js +++ b/lib/help.js @@ -101,8 +101,8 @@ class Help { if (!this.showGlobalOptions) return []; const globalOptions = []; - for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) { - const visibleOptions = parentCmd.options.filter((option) => !option.hidden); + for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) { + const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden); globalOptions.push(...visibleOptions); } if (this.sortOptions) { @@ -240,11 +240,11 @@ class Help { if (cmd._aliases[0]) { cmdName = cmdName + '|' + cmd._aliases[0]; } - let parentCmdNames = ''; - for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) { - parentCmdNames = parentCmd.name() + ' ' + parentCmdNames; + let ancestorCmdNames = ''; + for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) { + ancestorCmdNames = ancestorCmd.name() + ' ' + ancestorCmdNames; } - return parentCmdNames + cmdName + ' ' + cmd.usage(); + return ancestorCmdNames + cmdName + ' ' + cmd.usage(); } /**