diff --git a/lib/command.js b/lib/command.js index ca4a45ecb..97519d57b 100644 --- a/lib/command.js +++ b/lib/command.js @@ -79,7 +79,7 @@ class Command extends EventEmitter { this._helpConfiguration = {}; this._version = undefined; - this._versionOptionName = undefined; + this._versionOption = undefined; } /** @@ -1562,10 +1562,11 @@ Expecting one of '${allowedValues.join("', '")}'`); // Preserve original behaviour so backwards compatible when still using properties const result = {}; const len = this.options.length; + const versionOptionName = this._versionOption?.attributeName(); for (let i = 0; i < len; i++) { const key = this.options[i].attributeName(); - result[key] = key === this._versionOptionName ? this._version : this[key]; + result[key] = key === versionOptionName ? this._version : this[key]; } return result; } @@ -1821,10 +1822,9 @@ Expecting one of '${allowedValues.join("', '")}'`); this._version = str; flags = flags || '-V, --version'; description = description || 'output the version number'; - const versionOption = this.createOption(flags, description); - this._versionOptionName = versionOption.attributeName(); - this.options.push(versionOption); - this.on('option:' + versionOption.name(), () => { + this._versionOption = this.createOption(flags, description); + this.options.push(this._versionOption); + this.on('option:' + this._versionOption.name(), () => { this._outputConfiguration.writeOut(`${str}\n`); this._exit(0, 'commander.version', str); });