@@ -354,7 +354,7 @@ impl Options {
354
354
355
355
let mut vals = ( 0 ..opts. len ( ) )
356
356
. map ( |_| Vec :: new ( ) )
357
- . collect :: < Vec < Vec < Optval > > > ( ) ;
357
+ . collect :: < Vec < Vec < ( usize , Optval ) > > > ( ) ;
358
358
let mut free: Vec < String > = Vec :: new ( ) ;
359
359
let args = args
360
360
. into_iter ( )
@@ -365,6 +365,7 @@ impl Options {
365
365
. map ( |s| s. to_owned ( ) )
366
366
} ) . collect :: < :: std:: result:: Result < Vec < _ > , _ > > ( ) ?;
367
367
let mut args = args. into_iter ( ) . peekable ( ) ;
368
+ let mut arg_pos = 0 ;
368
369
while let Some ( cur) = args. next ( ) {
369
370
if !is_arg ( & cur) {
370
371
free. push ( cur) ;
@@ -440,7 +441,7 @@ impl Options {
440
441
if name_pos == names. len ( ) && i_arg. is_some ( ) {
441
442
return Err ( UnexpectedArgument ( nm. to_string ( ) ) ) ;
442
443
}
443
- vals[ optid] . push ( Given ) ;
444
+ vals[ optid] . push ( ( arg_pos , Given ) ) ;
444
445
}
445
446
Maybe => {
446
447
// Note that here we do not handle `--arg value`.
@@ -450,28 +451,29 @@ impl Options {
450
451
// option at the end of the arguments when
451
452
// FloatingFrees is in use.
452
453
if let Some ( i_arg) = i_arg. take ( ) {
453
- vals[ optid] . push ( Val ( i_arg) ) ;
454
+ vals[ optid] . push ( ( arg_pos , Val ( i_arg) ) ) ;
454
455
} else if was_long
455
456
|| name_pos < names. len ( )
456
457
|| args. peek ( ) . map_or ( true , |n| is_arg ( & n) )
457
458
{
458
- vals[ optid] . push ( Given ) ;
459
+ vals[ optid] . push ( ( arg_pos , Given ) ) ;
459
460
} else {
460
- vals[ optid] . push ( Val ( args. next ( ) . unwrap ( ) ) ) ;
461
+ vals[ optid] . push ( ( arg_pos , Val ( args. next ( ) . unwrap ( ) ) ) ) ;
461
462
}
462
463
}
463
464
Yes => {
464
465
if let Some ( i_arg) = i_arg. take ( ) {
465
- vals[ optid] . push ( Val ( i_arg) ) ;
466
+ vals[ optid] . push ( ( arg_pos , Val ( i_arg) ) ) ;
466
467
} else if let Some ( n) = args. next ( ) {
467
- vals[ optid] . push ( Val ( n) ) ;
468
+ vals[ optid] . push ( ( arg_pos , Val ( n) ) ) ;
468
469
} else {
469
470
return Err ( ArgumentMissing ( nm. to_string ( ) ) ) ;
470
471
}
471
472
}
472
473
}
473
474
}
474
475
}
476
+ arg_pos += 1 ;
475
477
}
476
478
debug_assert_eq ! ( vals. len( ) , opts. len( ) ) ;
477
479
for ( vals, opt) in vals. iter ( ) . zip ( opts. iter ( ) ) {
@@ -701,8 +703,8 @@ enum Optval {
701
703
pub struct Matches {
702
704
/// Options that matched
703
705
opts : Vec < Opt > ,
704
- /// Values of the Options that matched
705
- vals : Vec < Vec < Optval > > ,
706
+ /// Values of the Options that matched and their positions
707
+ vals : Vec < Vec < ( usize , Optval ) > > ,
706
708
/// Free string fragments
707
709
pub free : Vec < String > ,
708
710
}
@@ -799,15 +801,15 @@ impl OptGroup {
799
801
}
800
802
801
803
impl Matches {
802
- fn opt_vals ( & self , nm : & str ) -> Vec < Optval > {
804
+ fn opt_vals ( & self , nm : & str ) -> Vec < ( usize , Optval ) > {
803
805
match find_opt ( & self . opts , & Name :: from_str ( nm) ) {
804
806
Some ( id) => self . vals [ id] . clone ( ) ,
805
807
None => panic ! ( "No option '{}' defined" , nm) ,
806
808
}
807
809
}
808
810
809
811
fn opt_val ( & self , nm : & str ) -> Option < Optval > {
810
- self . opt_vals ( nm) . into_iter ( ) . next ( )
812
+ self . opt_vals ( nm) . into_iter ( ) . map ( | ( _ , o ) | o ) . next ( )
811
813
}
812
814
/// Returns true if an option was defined
813
815
pub fn opt_defined ( & self , nm : & str ) -> bool {
@@ -824,6 +826,11 @@ impl Matches {
824
826
self . opt_vals ( nm) . len ( )
825
827
}
826
828
829
+ /// Returns a vector of all the positions in which an option was matched.
830
+ pub fn opt_positions ( & self , nm : & str ) -> Vec < usize > {
831
+ self . opt_vals ( nm) . into_iter ( ) . map ( |( pos, _) | pos) . collect ( )
832
+ }
833
+
827
834
/// Returns true if any of several options were matched.
828
835
pub fn opts_present ( & self , names : & [ String ] ) -> bool {
829
836
names
@@ -851,7 +858,7 @@ impl Matches {
851
858
pub fn opt_strs ( & self , nm : & str ) -> Vec < String > {
852
859
self . opt_vals ( nm)
853
860
. into_iter ( )
854
- . filter_map ( |v | match v {
861
+ . filter_map ( |( _ , v ) | match v {
855
862
Val ( s) => Some ( s) ,
856
863
_ => None ,
857
864
} ) . collect ( )
0 commit comments