@@ -566,45 +566,30 @@ impl<'a, 'b> Parser<'a, 'b>
566
566
// Is this a new argument, or values from a previous option?
567
567
debug ! ( "Starts new arg..." ) ;
568
568
let starts_new_arg = if arg_os. starts_with ( b"-" ) {
569
- sdebugln ! ( "Yes" ) ;
569
+ sdebugln ! ( "Maybe" ) ;
570
+ // a singe '-' by itself is a value and typically means "stdin" on unix systems
570
571
!( arg_os. len_ ( ) == 1 )
571
572
} else {
572
573
sdebugln ! ( "No" ) ;
573
574
false
574
575
} ;
575
576
576
- // Has the user already passed '--'?
577
+ // Has the user already passed '--'? Meaning only positional args follow
577
578
if !self . trailing_vals {
578
579
// Does the arg match a subcommand name, or any of it's aliases (if defined)
579
- let pos_sc = self . subcommands
580
- . iter ( )
581
- . any ( |s| {
582
- & s. p . meta . name [ ..] == & * arg_os ||
583
- ( s. p . meta . aliases . is_some ( ) &&
584
- s. p
585
- . meta
586
- . aliases
587
- . as_ref ( )
588
- . unwrap ( )
589
- . iter ( )
590
- . any ( |& ( a, _) | a == & * arg_os) )
591
- } ) ;
592
- if ( !starts_new_arg || self . is_set ( AppSettings :: AllowLeadingHyphen ) ) && !pos_sc {
580
+ let pos_sc = self . possible_subcommand ( & arg_os) ;
581
+
582
+ // If the arg doesn't start with a `-` (except numbers, or AllowLeadingHyphen) and
583
+ // isn't a subcommand
584
+ if ( !starts_new_arg ||
585
+ ( self . is_set ( AppSettings :: AllowLeadingHyphen ) ||
586
+ self . is_set ( AppSettings :: AllowNegativeNumbers ) ) ) &&
587
+ !pos_sc {
593
588
// Check to see if parsing a value from an option
594
- if let Some ( nvo ) = needs_val_of {
589
+ if let Some ( arg ) = needs_val_of {
595
590
// get the OptBuilder so we can check the settings
596
- if let Some ( opt) = self . opts
597
- . iter ( )
598
- . find ( |o| {
599
- & o. name == & nvo ||
600
- ( o. aliases . is_some ( ) &&
601
- o. aliases
602
- . as_ref ( )
603
- . unwrap ( )
604
- . iter ( )
605
- . any ( |& ( a, _) | a == & * nvo) )
606
- } ) {
607
- needs_val_of = try!( self . add_val_to_arg ( opt, & arg_os, matcher) ) ;
591
+ if let Some ( ref opt) = self . get_opt ( & arg) {
592
+ needs_val_of = try!( self . add_val_to_arg ( * opt, & arg_os, matcher) ) ;
608
593
// get the next value from the iterator
609
594
continue ;
610
595
}
@@ -752,7 +737,8 @@ impl<'a, 'b> Parser<'a, 'b>
752
737
name : sc_name,
753
738
matches : sc_m. into ( ) ,
754
739
} ) ;
755
- } else if !self . settings . is_set ( AppSettings :: AllowLeadingHyphen ) {
740
+ } else if !( self . is_set ( AppSettings :: AllowLeadingHyphen ) ||
741
+ self . is_set ( AppSettings :: AllowNegativeNumbers ) ) {
756
742
return Err ( Error :: unknown_argument ( & * arg_os. to_string_lossy ( ) ,
757
743
"" ,
758
744
& * self . create_current_usage ( matcher) ,
@@ -1262,6 +1248,10 @@ impl<'a, 'b> Parser<'a, 'b>
1262
1248
return Ok ( None ) ;
1263
1249
} else if self . is_set ( AppSettings :: AllowLeadingHyphen ) {
1264
1250
return Ok ( None ) ;
1251
+ } else if self . is_set ( AppSettings :: AllowNegativeNumbers ) &&
1252
+ ( arg. to_string_lossy ( ) . parse :: < i64 > ( ) . is_ok ( ) ||
1253
+ arg. to_string_lossy ( ) . parse :: < f64 > ( ) . is_ok ( ) ) {
1254
+ return Ok ( None ) ;
1265
1255
}
1266
1256
1267
1257
debugln ! ( "Didn't match anything" ) ;
@@ -1319,7 +1309,8 @@ impl<'a, 'b> Parser<'a, 'b>
1319
1309
// Handle conflicts, requirements, overrides, etc.
1320
1310
// Must be called here due to mutablilty
1321
1311
arg_post_processing ! ( self , flag, matcher) ;
1322
- } else if !self . is_set ( AppSettings :: AllowLeadingHyphen ) {
1312
+ } else if !( self . is_set ( AppSettings :: AllowLeadingHyphen ) ||
1313
+ self . is_set ( AppSettings :: AllowNegativeNumbers ) ) {
1323
1314
let mut arg = String :: new ( ) ;
1324
1315
arg. push ( '-' ) ;
1325
1316
arg. push ( c) ;
0 commit comments