@@ -29,10 +29,10 @@ function _coerceType(str: string | undefined, type: OptionType, v?: Value): Valu
2929 }
3030
3131 return _coerceType ( str , OptionType . Boolean , v ) !== undefined
32- ? _coerceType ( str , OptionType . Boolean , v )
33- : _coerceType ( str , OptionType . Number , v ) !== undefined
34- ? _coerceType ( str , OptionType . Number , v )
35- : _coerceType ( str , OptionType . String , v ) ;
32+ ? _coerceType ( str , OptionType . Boolean , v )
33+ : _coerceType ( str , OptionType . Number , v ) !== undefined
34+ ? _coerceType ( str , OptionType . Number , v )
35+ : _coerceType ( str , OptionType . String , v ) ;
3636
3737 case OptionType . String :
3838 return str || '' ;
@@ -93,21 +93,28 @@ function _coerce(str: string | undefined, o: Option | null, v?: Value): Value |
9393
9494
9595function _getOptionFromName ( name : string , options : Option [ ] ) : Option | undefined {
96- const cName = strings . camelize ( name ) ;
96+ const camelName = / ( - | _ ) / . test ( name )
97+ ? strings . camelize ( name )
98+ : name ;
9799
98100 for ( const option of options ) {
99- if ( option . name == name || option . name == cName ) {
101+ if ( option . name === name || option . name === camelName ) {
100102 return option ;
101103 }
102104
103- if ( option . aliases . some ( x => x == name || x == cName ) ) {
105+ if ( option . aliases . some ( x => x === name || x === camelName ) ) {
104106 return option ;
105107 }
106108 }
107109
108110 return undefined ;
109111}
110112
113+ function _removeLeadingDashes ( key : string ) : string {
114+ const from = key . startsWith ( '--' ) ? 2 : key . startsWith ( '-' ) ? 1 : 0 ;
115+
116+ return key . substr ( from ) ;
117+ }
111118
112119function _assignOption (
113120 arg : string ,
@@ -130,16 +137,10 @@ function _assignOption(
130137
131138 // If flag is --no-abc AND there's no equal sign.
132139 if ( i == - 1 ) {
133- if ( key . startsWith ( 'no-' ) ) {
134- // Only use this key if the option matching the rest is a boolean.
135- const maybeOption = _getOptionFromName ( key . substr ( 3 ) , options ) ;
136- if ( maybeOption && maybeOption . type == 'boolean' ) {
137- value = 'false' ;
138- option = maybeOption ;
139- }
140- } else if ( key . startsWith ( 'no' ) ) {
140+ if ( key . startsWith ( 'no' ) ) {
141141 // Only use this key if the option matching the rest is a boolean.
142- const maybeOption = _getOptionFromName ( key . substr ( 2 ) , options ) ;
142+ const from = key . startsWith ( 'no-' ) ? 3 : 2 ;
143+ const maybeOption = _getOptionFromName ( strings . camelize ( key . substr ( from ) ) , options ) ;
143144 if ( maybeOption && maybeOption . type == 'boolean' ) {
144145 value = 'false' ;
145146 option = maybeOption ;
@@ -171,7 +172,7 @@ function _assignOption(
171172 }
172173 } else {
173174 key = arg . substring ( 0 , i ) ;
174- option = _getOptionFromName ( key , options ) || null ;
175+ option = _getOptionFromName ( _removeLeadingDashes ( key ) , options ) || null ;
175176 if ( option ) {
176177 value = arg . substring ( i + 1 ) ;
177178 }
0 commit comments