Skip to content

Commit 3c94dfd

Browse files
committed
Store version option instance instead of just name
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).
1 parent b488e6e commit 3c94dfd

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lib/command.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Command extends EventEmitter {
7979
this._helpConfiguration = {};
8080

8181
this._version = undefined;
82-
this._versionOptionName = undefined;
82+
this._versionOption = undefined;
8383
}
8484

8585
/**
@@ -1562,10 +1562,11 @@ Expecting one of '${allowedValues.join("', '")}'`);
15621562
// Preserve original behaviour so backwards compatible when still using properties
15631563
const result = {};
15641564
const len = this.options.length;
1565+
const versionOptionName = this._versionOption?.attributeName();
15651566

15661567
for (let i = 0; i < len; i++) {
15671568
const key = this.options[i].attributeName();
1568-
result[key] = key === this._versionOptionName ? this._version : this[key];
1569+
result[key] = key === versionOptionName ? this._version : this[key];
15691570
}
15701571
return result;
15711572
}
@@ -1821,10 +1822,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
18211822
this._version = str;
18221823
flags = flags || '-V, --version';
18231824
description = description || 'output the version number';
1824-
const versionOption = this.createOption(flags, description);
1825-
this._versionOptionName = versionOption.attributeName();
1826-
this.options.push(versionOption);
1827-
this.on('option:' + versionOption.name(), () => {
1825+
this._versionOption = this.createOption(flags, description);
1826+
this.options.push(this._versionOption);
1827+
this.on('option:' + this._versionOption.name(), () => {
18281828
this._outputConfiguration.writeOut(`${str}\n`);
18291829
this._exit(0, 'commander.version', str);
18301830
});

0 commit comments

Comments
 (0)