Skip to content

Commit

Permalink
Store version option instance instead of just name
Browse files Browse the repository at this point in the history
Storing the Option instance of the version option instead of just its
.attributeName() (_versionOptionName) is meaningful for cases when other
properties of the instance need to be accessed (not currently the case,
but could be in the future).
  • Loading branch information
aweebit committed Aug 3, 2023
1 parent b488e6e commit 3c94dfd
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Command extends EventEmitter {
this._helpConfiguration = {};

this._version = undefined;
this._versionOptionName = undefined;
this._versionOption = undefined;
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 3c94dfd

Please sign in to comment.