@@ -118,7 +118,7 @@ function _removeLeadingDashes(key: string): string {
118
118
119
119
function _assignOption (
120
120
arg : string ,
121
- args : string [ ] ,
121
+ nextArg : string | undefined ,
122
122
{ options, parsedOptions, leftovers, ignored, errors, deprecations } : {
123
123
options : Option [ ] ,
124
124
parsedOptions : Arguments ,
@@ -130,9 +130,10 @@ function _assignOption(
130
130
} ,
131
131
) {
132
132
const from = arg . startsWith ( '--' ) ? 2 : 1 ;
133
+ let consumedNextArg = false ;
133
134
let key = arg . substr ( from ) ;
134
135
let option : Option | null = null ;
135
- let value = '' ;
136
+ let value : string | undefined = '' ;
136
137
const i = arg . indexOf ( '=' ) ;
137
138
138
139
// If flag is --no-abc AND there's no equal sign.
@@ -151,7 +152,7 @@ function _assignOption(
151
152
// Set it to true if it's a boolean and the next argument doesn't match true/false.
152
153
const maybeOption = _getOptionFromName ( key , options ) ;
153
154
if ( maybeOption ) {
154
- value = args [ 0 ] ;
155
+ value = nextArg ;
155
156
let shouldShift = true ;
156
157
157
158
if ( value && value . startsWith ( '-' ) ) {
@@ -163,7 +164,7 @@ function _assignOption(
163
164
164
165
// Only absorb it if it leads to a better value.
165
166
if ( shouldShift && _coerce ( value , maybeOption ) !== undefined ) {
166
- args . shift ( ) ;
167
+ consumedNextArg = true ;
167
168
} else {
168
169
value = '' ;
169
170
}
@@ -179,9 +180,9 @@ function _assignOption(
179
180
}
180
181
181
182
if ( option === null ) {
182
- if ( args [ 0 ] && ! args [ 0 ] . startsWith ( '-' ) ) {
183
- leftovers . push ( arg , args [ 0 ] ) ;
184
- args . shift ( ) ;
183
+ if ( nextArg && ! nextArg . startsWith ( '-' ) ) {
184
+ leftovers . push ( arg , nextArg ) ;
185
+ consumedNextArg = true ;
185
186
} else {
186
187
leftovers . push ( arg ) ;
187
188
}
@@ -206,6 +207,8 @@ function _assignOption(
206
207
ignored . push ( arg ) ;
207
208
}
208
209
}
210
+
211
+ return consumedNextArg ;
209
212
}
210
213
211
214
@@ -289,30 +292,33 @@ export function parseArguments(
289
292
290
293
const state = { options, parsedOptions, positionals, leftovers, ignored, errors, deprecations } ;
291
294
292
- for ( let arg = args . shift ( ) ; arg !== undefined ; arg = args . shift ( ) ) {
295
+ for ( let argIndex = 0 ; argIndex < args . length ; argIndex ++ ) {
296
+ const arg = args [ argIndex ] ;
297
+ let consumedNextArg = false ;
298
+
293
299
if ( arg == '--' ) {
294
300
// If we find a --, we're done.
295
- leftovers . push ( ...args ) ;
301
+ leftovers . push ( ...args . slice ( argIndex + 1 ) ) ;
296
302
break ;
297
303
}
298
304
299
305
if ( arg . startsWith ( '--' ) ) {
300
- _assignOption ( arg , args , state ) ;
306
+ consumedNextArg = _assignOption ( arg , args [ argIndex + 1 ] , state ) ;
301
307
} else if ( arg . startsWith ( '-' ) ) {
302
308
// Argument is of form -abcdef. Starts at 1 because we skip the `-`.
303
309
for ( let i = 1 ; i < arg . length ; i ++ ) {
304
310
const flag = arg [ i ] ;
305
311
// If the next character is an '=', treat it as a long flag.
306
312
if ( arg [ i + 1 ] == '=' ) {
307
313
const f = '-' + flag + arg . slice ( i + 1 ) ;
308
- _assignOption ( f , args , state ) ;
314
+ consumedNextArg = _assignOption ( f , args [ argIndex + 1 ] , state ) ;
309
315
break ;
310
316
}
311
317
// Treat the last flag as `--a` (as if full flag but just one letter). We do this in
312
318
// the loop because it saves us a check to see if the arg is just `-`.
313
319
if ( i == arg . length - 1 ) {
314
320
const arg = '-' + flag ;
315
- _assignOption ( arg , args , state ) ;
321
+ consumedNextArg = _assignOption ( arg , args [ argIndex + 1 ] , state ) ;
316
322
} else {
317
323
const maybeOption = _getOptionFromName ( flag , options ) ;
318
324
if ( maybeOption ) {
@@ -326,6 +332,10 @@ export function parseArguments(
326
332
} else {
327
333
positionals . push ( arg ) ;
328
334
}
335
+
336
+ if ( consumedNextArg ) {
337
+ argIndex ++ ;
338
+ }
329
339
}
330
340
331
341
// Deal with positionals.
0 commit comments