@@ -589,6 +589,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
589
589
const oname = option . name ( ) ;
590
590
const name = option . attributeName ( ) ;
591
591
592
+ // register the option
593
+ this . options . push ( option ) ;
594
+
592
595
// store default value
593
596
if ( option . negate ) {
594
597
// --no-foo is special and defaults foo to true, unless a --foo option is already defined
@@ -600,9 +603,6 @@ Expecting one of '${allowedValues.join("', '")}'`);
600
603
this . setOptionValueWithSource ( name , option . defaultValue , 'default' ) ;
601
604
}
602
605
603
- // register the option
604
- this . options . push ( option ) ;
605
-
606
606
// handler for cli and env supplied values
607
607
const handleOptionValue = ( val , invalidValueMessage , valueSource ) => {
608
608
// val is null for optional option used without an optional-argument.
@@ -829,10 +829,18 @@ Expecting one of '${allowedValues.join("', '")}'`);
829
829
*/
830
830
831
831
storeOptionsAsProperties ( storeAsProperties = true ) {
832
- this . _storeOptionsAsProperties = ! ! storeAsProperties ;
833
832
if ( this . options . length ) {
834
833
throw new Error ( 'call .storeOptionsAsProperties() before adding options' ) ;
835
834
}
835
+ if ( Object . keys ( this . _optionValues ) . length ) {
836
+ throw new Error ( 'call .storeOptionsAsProperties() before setting option values' ) ;
837
+ }
838
+ if ( ! this . _storeOptionsAsProperties && storeAsProperties ) {
839
+ this . _defineVersionOptionAsProperty ( ) ;
840
+ } else if ( this . _storeOptionsAsProperties && ! storeAsProperties ) {
841
+ this . _deleteVersionOptionProperty ( ) ;
842
+ }
843
+ this . _storeOptionsAsProperties = ! ! storeAsProperties ;
836
844
return this ;
837
845
}
838
846
@@ -869,6 +877,20 @@ Expecting one of '${allowedValues.join("', '")}'`);
869
877
*/
870
878
871
879
setOptionValueWithSource ( key , value , source ) {
880
+ if ( this . _storeOptionsAsProperties ) {
881
+ if ( key === this . _versionOptionName ) {
882
+ throw new Error ( `Tried to set value of option ${ key } reserved for version number.
883
+ Set version number by calling .version() instead` ) ;
884
+ }
885
+ const optionSupported = this . options . some (
886
+ option => key === option . attributeName ( )
887
+ ) ;
888
+ if ( ! optionSupported ) {
889
+ throw new Error ( `Tried to set value of not supported option ${ key } .
890
+ This is not allowed when option values are stored as instance properties.
891
+ Add support for option by calling .option() or .addOption() first` ) ;
892
+ }
893
+ }
872
894
this . _optionValues [ key ] = value ;
873
895
this . _optionValueSources [ key ] = source ;
874
896
return this ;
@@ -1627,20 +1649,6 @@ Expecting one of '${allowedValues.join("', '")}'`);
1627
1649
* @return {Object }
1628
1650
*/
1629
1651
opts ( ) {
1630
- if ( this . _storeOptionsAsProperties ) {
1631
- // Preserve original behaviour so backwards compatible when still using properties
1632
- const result = { } ;
1633
- const len = this . options . length ;
1634
-
1635
- for ( let i = 0 ; i < len ; i ++ ) {
1636
- const key = this . options [ i ] . attributeName ( ) ;
1637
- result [ key ] = key === this . _versionOptionName
1638
- ? this . _version
1639
- : this . _optionValues [ key ] ;
1640
- }
1641
- return result ;
1642
- }
1643
-
1644
1652
return this . _optionValuesProxy ;
1645
1653
}
1646
1654
@@ -1893,7 +1901,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
1893
1901
flags = flags || '-V, --version' ;
1894
1902
description = description || 'output the version number' ;
1895
1903
const versionOption = this . createOption ( flags , description ) ;
1904
+ if ( this . _storeOptionsAsProperties ) this . _deleteVersionOptionProperty ( ) ;
1896
1905
this . _versionOptionName = versionOption . attributeName ( ) ;
1906
+ if ( this . _storeOptionsAsProperties ) this . _defineVersionOptionAsProperty ( ) ;
1897
1907
this . options . push ( versionOption ) ;
1898
1908
this . on ( 'option:' + versionOption . name ( ) , ( ) => {
1899
1909
this . _outputConfiguration . writeOut ( `${ str } \n` ) ;
@@ -1902,6 +1912,27 @@ Expecting one of '${allowedValues.join("', '")}'`);
1902
1912
return this ;
1903
1913
}
1904
1914
1915
+ /**
1916
+ * @api private
1917
+ */
1918
+ _defineVersionOptionAsProperty ( ) {
1919
+ return Reflect . defineProperty ( this . _optionValues , this . _versionOptionName , {
1920
+ get : ( ) => this . _version ,
1921
+ set : ( value ) => {
1922
+ this . _version = value ;
1923
+ } ,
1924
+ configurable : true ,
1925
+ enumerable : true
1926
+ } ) ;
1927
+ }
1928
+
1929
+ /**
1930
+ * @api private
1931
+ */
1932
+ _deleteVersionOptionProperty ( ) {
1933
+ return Reflect . deleteProperty ( this . _optionValues , this . _versionOptionName ) ;
1934
+ }
1935
+
1905
1936
/**
1906
1937
* Set the description.
1907
1938
*
0 commit comments