@@ -331,18 +331,23 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
331
331
int currentElement = 0 ;
332
332
int currentCharacter;
333
333
// The value of optind after the last time getopt_long was called.
334
- int prevOptind = 1 ;
334
+ int prevOptIndex = 1 ;
335
335
// All the nonoptions we have found so far will be in range [firstNonoption, lastNonoptionPlus1).
336
336
int firstNonoption = 1 ;
337
337
int lastNonoptionPlus1 = 1 ;
338
+
338
339
// Temporary variables used When finding a new set of nonoptions.
339
340
int firstNonoptionNew;
340
341
int lastNonoptionPlus1New;
341
- // Temporary variable used when mergeing existing nonoptions with new ones.
342
+
343
+ // Temporary variables used when moving already-found nonoptions later in the array so that they
344
+ // will be contiguous with the new non-options that were just found.
345
+ // Index the nonoption is being moved to.
342
346
int moveNonoptionToHere;
343
- // Index variables used in permuting argv.
344
- int idx;
345
- int idx2;
347
+ // Initial index of the nonoption being moved.
348
+ int nonoptionToMove;
349
+ // Index of the next pair of elements to swap in argv (indexToSwap and indexToSwap+1).
350
+ int indexToSwap;
346
351
// Temporary variable for swapping elements of argv.
347
352
char * tempSwap;
348
353
// Permutable version of argv. Needed to move the nonoptions to the end.
@@ -449,23 +454,23 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
449
454
// elements of argv during the execution of this loop may not always match what they would be with the POSIX version. It is
450
455
// only guaranteed to match at the end.
451
456
452
- // The elements that just now got processed by getopt_long are [prevOptind , optind] if the new option is the non-final
453
- // character of an option group (e.g. 'a' or 'b' in "-abc"), or [prevOptind , optind) if it's the final character of an
457
+ // The elements that just now got processed by getopt_long are [prevOptIndex , optind] if the new option is the non-final
458
+ // character of an option group (e.g. 'a' or 'b' in "-abc"), or [prevOptIndex , optind) if it's the final character of an
454
459
// option group or not a group at all. Walk through those elements knowing that all elements we encounter before reaching
455
460
// the option must be nonoptions. Store the range of those nonoptions in firstNonoptionNew and lastNonoptionPlus1New. If
456
- // this is the final iteration (getopt_long returned -1) then prevOptind == optind == argc, which means firstNonoptionNew ==
461
+ // this is the final iteration (getopt_long returned -1) then prevOptIndex == optind == argc, which means firstNonoptionNew ==
457
462
// lastNonoptionPlus1New == argc, the following for loop will be skipped, and all the nonoptions we had already found will
458
463
// get moved to the end of argv.
459
- firstNonoptionNew = prevOptind ;
460
- lastNonoptionPlus1New = prevOptind ;
461
- for (idx = prevOptind; idx <= optind ; idx ++)
464
+ firstNonoptionNew = prevOptIndex ;
465
+ lastNonoptionPlus1New = prevOptIndex ;
466
+ for (nonoptionToMove = prevOptIndex; nonoptionToMove <= optind ; nonoptionToMove ++)
462
467
{
463
- // Note that this loop INCLUDES idx =optind since the option that just got processed might be part of a group of short
468
+ // Note that this loop INCLUDES nonoptionToMove =optind since the option that just got processed might be part of a group of short
464
469
// options all sharing a single '-', in which case optind is going to land on that same element several times in a row
465
470
// before it moves past it.
466
- if (argv[idx ] && argv[idx ][0 ] == ' -' )
471
+ if (argv[nonoptionToMove ] && argv[nonoptionToMove ][0 ] == ' -' )
467
472
{
468
- lastNonoptionPlus1New = idx ;
473
+ lastNonoptionPlus1New = nonoptionToMove ;
469
474
break ;
470
475
}
471
476
}
@@ -476,15 +481,15 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
476
481
// Move the old set of nonoptions we had already found so that they abut the new set of nonoptions we just now found,
477
482
// thus giving us a single contiguous block of nonoptions.
478
483
moveNonoptionToHere = firstNonoptionNew - 1 ;
479
- for (idx = lastNonoptionPlus1 - 1 ; idx >= firstNonoption; idx --)
484
+ for (nonoptionToMove = lastNonoptionPlus1 - 1 ; nonoptionToMove >= firstNonoption; nonoptionToMove --)
480
485
{
481
- // Move element idx (the last nonoption we haven't moved yet) into the slot moveNonoptionToHere.
482
- for (idx2 = idx; idx2 < moveNonoptionToHere; idx2 ++)
486
+ // Move element nonoptionToMove (the last nonoption we haven't moved yet) into the slot moveNonoptionToHere.
487
+ for (indexToSwap = nonoptionToMove; indexToSwap < moveNonoptionToHere; indexToSwap ++)
483
488
{
484
- // Swap element idx2 with element idx2 + 1.
485
- tempSwap = permutableArgv[idx2 ];
486
- permutableArgv[idx2 ] = permutableArgv[idx2 + 1 ];
487
- permutableArgv[idx2 + 1 ] = tempSwap;
489
+ // Swap element indexToSwap with element indexToSwap + 1.
490
+ tempSwap = permutableArgv[indexToSwap ];
491
+ permutableArgv[indexToSwap ] = permutableArgv[indexToSwap + 1 ];
492
+ permutableArgv[indexToSwap + 1 ] = tempSwap;
488
493
}
489
494
// Decrement moveNonoptionToHere so the next nonoption we move will go into the slot before it.
490
495
moveNonoptionToHere--;
@@ -495,7 +500,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
495
500
}
496
501
497
502
// Store the value of optind so we'll know which elements getopt_long processes the next time it is called.
498
- prevOptind = optind ;
503
+ prevOptIndex = optind ;
499
504
500
505
#endif // CONFIG_NON_POSIX_GETOPT_LONG
501
506
@@ -588,7 +593,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
588
593
if (nonOptArgHandler != nullptr )
589
594
{
590
595
#ifdef CONFIG_NON_POSIX_GETOPT_LONG
591
- // On a POSIX implementation, on the final interation when getopt_long returns -1 indicating it has nothing left to do,
596
+ // On a POSIX implementation, on the final iteration when getopt_long returns -1 indicating it has nothing left to do,
592
597
// optind would be set to the location of the first nonoption (all of which by now would have been moved to the end of
593
598
// argv). On some non-POSIX implementations this is not true -- it simply sets optind to the location of argv's terminal
594
599
// NULL (i.e. optind == argc). So we have to alter optind here to simulate the POSIX behavior.
0 commit comments