@@ -167,12 +167,10 @@ function parse (args, opts) {
167
167
168
168
// arrays format = '--f=a b c'
169
169
if ( checkAllAliases ( m [ 1 ] , flags . arrays ) ) {
170
- args . splice ( i + 1 , 0 , m [ 2 ] )
171
- i = eatArray ( i , m [ 1 ] , args )
172
- } else if ( checkAllAliases ( m [ 1 ] , flags . nargs ) ) {
170
+ i = eatArray ( i , m [ 1 ] , args , m [ 2 ] )
171
+ } else if ( checkAllAliases ( m [ 1 ] , flags . nargs ) !== false ) {
173
172
// nargs format = '--f=monkey washing cat'
174
- args . splice ( i + 1 , 0 , m [ 2 ] )
175
- i = eatNargs ( i , m [ 1 ] , args )
173
+ i = eatNargs ( i , m [ 1 ] , args , m [ 2 ] )
176
174
} else {
177
175
setArg ( m [ 1 ] , m [ 2 ] )
178
176
}
@@ -241,12 +239,10 @@ function parse (args, opts) {
241
239
242
240
if ( checkAllAliases ( key , flags . arrays ) ) {
243
241
// array format = '-f=a b c'
244
- args . splice ( i + 1 , 0 , value )
245
- i = eatArray ( i , key , args )
246
- } else if ( checkAllAliases ( key , flags . nargs ) ) {
242
+ i = eatArray ( i , key , args , value )
243
+ } else if ( checkAllAliases ( key , flags . nargs ) !== false ) {
247
244
// nargs format = '-f=monkey washing cat'
248
- args . splice ( i + 1 , 0 , value )
249
- i = eatNargs ( i , key , args )
245
+ i = eatNargs ( i , key , args , value )
250
246
} else {
251
247
setArg ( key , value )
252
248
}
@@ -364,22 +360,27 @@ function parse (args, opts) {
364
360
365
361
// how many arguments should we consume, based
366
362
// on the nargs option?
367
- function eatNargs ( i , key , args ) {
363
+ function eatNargs ( i , key , args , argAfterEqualSign ) {
368
364
let ii
369
365
let toEat = checkAllAliases ( key , flags . nargs )
370
366
// NaN has a special meaning for the array type, indicating that one or
371
367
// more values are expected.
372
368
toEat = isNaN ( toEat ) ? 1 : toEat
373
369
374
370
if ( toEat === 0 ) {
371
+ if ( ! isUndefined ( argAfterEqualSign ) ) {
372
+ error = Error ( __ ( 'Argument unexpected for: %s' , key ) )
373
+ }
375
374
setArg ( key , defaultValue ( key ) )
376
375
return i
377
376
}
378
377
379
- let available = 0
378
+ let available = isUndefined ( argAfterEqualSign ) ? 0 : 1
380
379
if ( configuration [ 'nargs-eats-options' ] ) {
381
380
// classic behavior, yargs eats positional and dash arguments.
382
- if ( args . length - ( i + 1 ) < toEat ) error = Error ( __ ( 'Not enough arguments following: %s' , key ) )
381
+ if ( args . length - ( i + 1 ) + available < toEat ) {
382
+ error = Error ( __ ( 'Not enough arguments following: %s' , key ) )
383
+ }
383
384
available = toEat
384
385
} else {
385
386
// nargs will not consume flag arguments, e.g., -abc, --foo,
@@ -391,7 +392,11 @@ function parse (args, opts) {
391
392
if ( available < toEat ) error = Error ( __ ( 'Not enough arguments following: %s' , key ) )
392
393
}
393
394
394
- const consumed = Math . min ( available , toEat )
395
+ let consumed = Math . min ( available , toEat )
396
+ if ( ! isUndefined ( argAfterEqualSign ) && consumed > 0 ) {
397
+ setArg ( key , argAfterEqualSign )
398
+ consumed --
399
+ }
395
400
for ( ii = i + 1 ; ii < ( consumed + i + 1 ) ; ii ++ ) {
396
401
setArg ( key , args [ ii ] )
397
402
}
@@ -402,29 +407,34 @@ function parse (args, opts) {
402
407
// if an option is an array, eat all non-hyphenated arguments
403
408
// following it... YUM!
404
409
// e.g., --foo apple banana cat becomes ["apple", "banana", "cat"]
405
- function eatArray ( i , key , args ) {
410
+ function eatArray ( i , key , args , argAfterEqualSign ) {
406
411
let argsToSet = [ ]
407
- let next = args [ i + 1 ]
412
+ let next = argAfterEqualSign || args [ i + 1 ]
408
413
// If both array and nargs are configured, enforce the nargs count:
409
414
const nargsCount = checkAllAliases ( key , flags . nargs )
410
415
411
416
if ( checkAllAliases ( key , flags . bools ) && ! ( / ^ ( t r u e | f a l s e ) $ / . test ( next ) ) ) {
412
417
argsToSet . push ( true )
413
- } else if ( isUndefined ( next ) || ( / ^ - / . test ( next ) && ! negative . test ( next ) && ! isUnknownOptionAsArg ( next ) ) ) {
418
+ } else if ( isUndefined ( next ) ||
419
+ ( isUndefined ( argAfterEqualSign ) && / ^ - / . test ( next ) && ! negative . test ( next ) && ! isUnknownOptionAsArg ( next ) ) ) {
414
420
// for keys without value ==> argsToSet remains an empty []
415
421
// set user default value, if available
416
422
if ( defaults [ key ] !== undefined ) {
417
423
const defVal = defaults [ key ]
418
424
argsToSet = Array . isArray ( defVal ) ? defVal : [ defVal ]
419
425
}
420
426
} else {
427
+ // value in --option=value is eaten as is
428
+ if ( ! isUndefined ( argAfterEqualSign ) ) {
429
+ argsToSet . push ( processValue ( key , argAfterEqualSign ) )
430
+ }
421
431
for ( let ii = i + 1 ; ii < args . length ; ii ++ ) {
432
+ if ( ( ! configuration [ 'greedy-arrays' ] && argsToSet . length > 0 ) ||
433
+ ( nargsCount && argsToSet . length >= nargsCount ) ) break
422
434
next = args [ ii ]
423
435
if ( / ^ - / . test ( next ) && ! negative . test ( next ) && ! isUnknownOptionAsArg ( next ) ) break
424
436
i = ii
425
437
argsToSet . push ( processValue ( key , next ) )
426
- if ( ! configuration [ 'greedy-arrays' ] ||
427
- ( nargsCount && argsToSet . length >= nargsCount ) ) break
428
438
}
429
439
}
430
440
0 commit comments