@@ -29,10 +29,10 @@ function _coerceType(str: string | undefined, type: OptionType, v?: Value): Valu
29
29
}
30
30
31
31
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 ) ;
36
36
37
37
case OptionType . String :
38
38
return str || '' ;
@@ -93,21 +93,28 @@ function _coerce(str: string | undefined, o: Option | null, v?: Value): Value |
93
93
94
94
95
95
function _getOptionFromName ( name : string , options : Option [ ] ) : Option | undefined {
96
- const cName = strings . camelize ( name ) ;
96
+ const camelName = / ( - | _ ) / . test ( name )
97
+ ? strings . camelize ( name )
98
+ : name ;
97
99
98
100
for ( const option of options ) {
99
- if ( option . name == name || option . name == cName ) {
101
+ if ( option . name === name || option . name === camelName ) {
100
102
return option ;
101
103
}
102
104
103
- if ( option . aliases . some ( x => x == name || x == cName ) ) {
105
+ if ( option . aliases . some ( x => x === name || x === camelName ) ) {
104
106
return option ;
105
107
}
106
108
}
107
109
108
110
return undefined ;
109
111
}
110
112
113
+ function _removeLeadingDashes ( key : string ) : string {
114
+ const from = key . startsWith ( '--' ) ? 2 : key . startsWith ( '-' ) ? 1 : 0 ;
115
+
116
+ return key . substr ( from ) ;
117
+ }
111
118
112
119
function _assignOption (
113
120
arg : string ,
@@ -127,16 +134,10 @@ function _assignOption(
127
134
128
135
// If flag is --no-abc AND there's no equal sign.
129
136
if ( i == - 1 ) {
130
- if ( key . startsWith ( 'no-' ) ) {
131
- // Only use this key if the option matching the rest is a boolean.
132
- const maybeOption = _getOptionFromName ( key . substr ( 3 ) , options ) ;
133
- if ( maybeOption && maybeOption . type == 'boolean' ) {
134
- value = 'false' ;
135
- option = maybeOption ;
136
- }
137
- } else if ( key . startsWith ( 'no' ) ) {
137
+ if ( key . startsWith ( 'no' ) ) {
138
138
// Only use this key if the option matching the rest is a boolean.
139
- const maybeOption = _getOptionFromName ( key . substr ( 2 ) , options ) ;
139
+ const from = key . startsWith ( 'no-' ) ? 3 : 2 ;
140
+ const maybeOption = _getOptionFromName ( strings . camelize ( key . substr ( from ) ) , options ) ;
140
141
if ( maybeOption && maybeOption . type == 'boolean' ) {
141
142
value = 'false' ;
142
143
option = maybeOption ;
@@ -168,7 +169,7 @@ function _assignOption(
168
169
}
169
170
} else {
170
171
key = arg . substring ( 0 , i ) ;
171
- option = _getOptionFromName ( key , options ) || null ;
172
+ option = _getOptionFromName ( _removeLeadingDashes ( key ) , options ) || null ;
172
173
if ( option ) {
173
174
value = arg . substring ( i + 1 ) ;
174
175
}
0 commit comments