@@ -78,7 +78,12 @@ class Command extends EventEmitter {
78
78
this . _helpCommandDescription = 'display help for command' ;
79
79
this . _helpConfiguration = { } ;
80
80
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
+ } ,
82
87
set : ( _ , key , value ) => {
83
88
this . setOptionValue ( key , value ) ;
84
89
return true ;
@@ -93,8 +98,19 @@ Option value deletion is not supported`);
93
98
- or Object.defineProperties(),
94
99
- or Reflect.defineProperty().
95
100
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 ) ;
96
112
}
97
- } ) ;
113
+ } ) , { } ) ;
98
114
99
115
// Because of how the returned proxy works, ideally, no prooerties should be defined outside the cinstructor.
100
116
// 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("', '")}'`);
835
851
if ( Object . keys ( this . _optionValues ) . length ) {
836
852
throw new Error ( 'call .storeOptionsAsProperties() before setting option values' ) ;
837
853
}
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
+ }
842
860
}
843
861
this . _storeOptionsAsProperties = ! ! storeAsProperties ;
844
862
return this ;
@@ -1889,7 +1907,7 @@ Add support for option by calling .option() or .addOption() first`);
1889
1907
*
1890
1908
* You can optionally supply the flags and description to override the defaults.
1891
1909
*
1892
- * @param {string } str
1910
+ * @param {string } [ str]
1893
1911
* @param {string } [flags]
1894
1912
* @param {string } [description]
1895
1913
* @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`);
1903
1921
const versionOption = this . createOption ( flags , description ) ;
1904
1922
if ( this . _storeOptionsAsProperties ) this . _deleteVersionOptionProperty ( ) ;
1905
1923
this . _versionOptionName = versionOption . attributeName ( ) ;
1906
- if ( this . _storeOptionsAsProperties ) this . _defineVersionOptionAsProperty ( ) ;
1924
+ if ( this . _storeOptionsAsProperties ) this . _defineVersionOptionProperty ( ) ;
1907
1925
this . options . push ( versionOption ) ;
1908
1926
this . on ( 'option:' + versionOption . name ( ) , ( ) => {
1909
1927
this . _outputConfiguration . writeOut ( `${ str } \n` ) ;
@@ -1915,7 +1933,7 @@ Add support for option by calling .option() or .addOption() first`);
1915
1933
/**
1916
1934
* @api private
1917
1935
*/
1918
- _defineVersionOptionAsProperty ( ) {
1936
+ _defineVersionOptionProperty ( ) {
1919
1937
return Reflect . defineProperty ( this . _optionValues , this . _versionOptionName , {
1920
1938
get : ( ) => this . _version ,
1921
1939
set : ( value ) => {
0 commit comments