Skip to content

Commit a3f0e28

Browse files
committed
Improve version option property handling
- Only define it when a version has been set - Show as data property in console output
1 parent a5afe93 commit a3f0e28

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lib/command.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ class Command extends EventEmitter {
7878
this._helpCommandDescription = 'display help for command';
7979
this._helpConfiguration = {};
8080

81-
this._optionValuesProxy = new Proxy(this._optionValues, {
81+
// Double proxy to show the version option property value instead of [Getter/Setter] when printing the return value of opts() to a console.
82+
// Required because Node internally unwraps one proxy and therefore would not use the getOwnPropertyDescriptor() trap otherwise.
83+
this._optionValuesProxy = new Proxy(new Proxy(this._optionValues, {
84+
get: (_, key) => {
85+
return this.getOptionValue(key);
86+
},
8287
set: (_, key, value) => {
8388
this.setOptionValue(key, value);
8489
return true;
@@ -93,8 +98,19 @@ Option value deletion is not supported`);
9398
- or Object.defineProperties(),
9499
- or Reflect.defineProperty().
95100
Options value configuration is not supported`);
101+
},
102+
getOwnPropertyDescriptor: (target, key) => {
103+
if (this._storeOptionsAsProperties && key === this._versionOptionName) {
104+
return {
105+
value: target[key],
106+
writable: true,
107+
configurable: true,
108+
enumerable: true
109+
};
110+
}
111+
return Reflect.getOwnPropertyDescriptor(target, key);
96112
}
97-
});
113+
}), {});
98114

99115
// Because of how the returned proxy works, ideally, no prooerties should be defined outside the cinstructor.
100116
// They can still be defined outside the constructor in subclasses, but only when _storeOptionsAsProperties is set to false.
@@ -835,10 +851,12 @@ Expecting one of '${allowedValues.join("', '")}'`);
835851
if (Object.keys(this._optionValues).length) {
836852
throw new Error('call .storeOptionsAsProperties() before setting option values');
837853
}
838-
if (!this._storeOptionsAsProperties && storeAsProperties) {
839-
this._defineVersionOptionAsProperty();
840-
} else if (this._storeOptionsAsProperties && !storeAsProperties) {
841-
this._deleteVersionOptionProperty();
854+
if (this._versionOptionName !== undefined) {
855+
if (!this._storeOptionsAsProperties && storeAsProperties) {
856+
this._defineVersionOptionProperty();
857+
} else if (this._storeOptionsAsProperties && !storeAsProperties) {
858+
this._deleteVersionOptionProperty();
859+
}
842860
}
843861
this._storeOptionsAsProperties = !!storeAsProperties;
844862
return this;
@@ -1889,7 +1907,7 @@ Add support for option by calling .option() or .addOption() first`);
18891907
*
18901908
* You can optionally supply the flags and description to override the defaults.
18911909
*
1892-
* @param {string} str
1910+
* @param {string} [str]
18931911
* @param {string} [flags]
18941912
* @param {string} [description]
18951913
* @return {this | string} `this` command for chaining, or version string if no arguments
@@ -1903,7 +1921,7 @@ Add support for option by calling .option() or .addOption() first`);
19031921
const versionOption = this.createOption(flags, description);
19041922
if (this._storeOptionsAsProperties) this._deleteVersionOptionProperty();
19051923
this._versionOptionName = versionOption.attributeName();
1906-
if (this._storeOptionsAsProperties) this._defineVersionOptionAsProperty();
1924+
if (this._storeOptionsAsProperties) this._defineVersionOptionProperty();
19071925
this.options.push(versionOption);
19081926
this.on('option:' + versionOption.name(), () => {
19091927
this._outputConfiguration.writeOut(`${str}\n`);
@@ -1915,7 +1933,7 @@ Add support for option by calling .option() or .addOption() first`);
19151933
/**
19161934
* @api private
19171935
*/
1918-
_defineVersionOptionAsProperty() {
1936+
_defineVersionOptionProperty() {
19191937
return Reflect.defineProperty(this._optionValues, this._versionOptionName, {
19201938
get: () => this._version,
19211939
set: (value) => {

typings/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export class Command {
293293
*
294294
* You can optionally supply the flags and description to override the defaults.
295295
*/
296-
version(str: string, flags?: string, description?: string): this;
296+
version(str?: string, flags?: string, description?: string): this;
297297

298298
/**
299299
* Define a command, implemented using an action handler.

0 commit comments

Comments
 (0)