@@ -571,7 +571,6 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
571
571
}
572
572
} else {
573
573
let mut j = 1 ;
574
- let mut last_valid_opt_id = None ;
575
574
names = Vec :: new ( ) ;
576
575
while j < curlen {
577
576
let range = cur. as_slice ( ) . char_range_at ( j) ;
@@ -584,27 +583,24 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
584
583
interpreted correctly
585
584
*/
586
585
587
- match find_opt ( opts. as_slice ( ) , opt. clone ( ) ) {
588
- Some ( id) => last_valid_opt_id = Some ( id) ,
589
- None => {
590
- let arg_follows =
591
- last_valid_opt_id. is_some ( ) &&
592
- match opts[ last_valid_opt_id. unwrap ( ) ]
593
- . hasarg {
594
-
595
- Yes | Maybe => true ,
596
- No => false
597
- } ;
598
- if arg_follows && j < curlen {
599
- i_arg = Some ( cur. as_slice ( )
600
- . slice ( j, curlen) . to_string ( ) ) ;
601
- break ;
602
- } else {
603
- last_valid_opt_id = None ;
604
- }
605
- }
606
- }
586
+ let opt_id = match find_opt ( opts. as_slice ( ) , opt. clone ( ) ) {
587
+ Some ( id) => id,
588
+ None => return Err ( UnrecognizedOption ( opt. to_string ( ) ) )
589
+ } ;
590
+
607
591
names. push ( opt) ;
592
+
593
+ let arg_follows = match opts[ opt_id] . hasarg {
594
+ Yes | Maybe => true ,
595
+ No => false
596
+ } ;
597
+
598
+ if arg_follows && range. next < curlen {
599
+ i_arg = Some ( cur. as_slice ( )
600
+ . slice ( range. next , curlen) . to_string ( ) ) ;
601
+ break ;
602
+ }
603
+
608
604
j = range. next ;
609
605
}
610
606
}
@@ -617,7 +613,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
617
613
} ;
618
614
match opts[ optid] . hasarg {
619
615
No => {
620
- if !i_arg. is_none ( ) {
616
+ if name_pos == names . len ( ) && !i_arg. is_none ( ) {
621
617
return Err ( UnexpectedArgument ( nm. to_string ( ) ) ) ;
622
618
}
623
619
vals. get_mut ( optid) . push ( Given ) ;
@@ -1441,6 +1437,21 @@ mod tests {
1441
1437
1442
1438
}
1443
1439
1440
+ #[ test]
1441
+ fn test_nospace_conflict ( ) {
1442
+ let args = vec ! ( "-vvLverbose" . to_string( ) , "-v" . to_string( ) ) ;
1443
+ let opts = vec ! ( optmulti( "L" , "" , "library directory" , "LIB" ) ,
1444
+ optflagmulti( "v" , "verbose" , "Verbose" ) ) ;
1445
+ let matches = & match getopts ( args. as_slice ( ) , opts. as_slice ( ) ) {
1446
+ result:: Ok ( m) => m,
1447
+ result:: Err ( e) => fail ! ( "{}" , e )
1448
+ } ;
1449
+ assert ! ( matches. opts_present( [ "L" . to_string( ) ] ) ) ;
1450
+ assert_eq ! ( matches. opts_str( [ "L" . to_string( ) ] ) . unwrap( ) , "verbose" . to_string( ) ) ;
1451
+ assert ! ( matches. opts_present( [ "v" . to_string( ) ] ) ) ;
1452
+ assert_eq ! ( 3 , matches. opt_count( "v" ) ) ;
1453
+ }
1454
+
1444
1455
#[ test]
1445
1456
fn test_long_to_short ( ) {
1446
1457
let mut short = Opt {
0 commit comments