92
92
93
93
#[ cfg( test) ] #[ phase( plugin, link) ] extern crate log;
94
94
95
- use self :: Name :: * ;
96
- use self :: HasArg :: * ;
97
- use self :: Occur :: * ;
98
- use self :: Fail :: * ;
95
+ pub use self :: Name :: * ;
96
+ pub use self :: HasArg :: * ;
97
+ pub use self :: Occur :: * ;
98
+ pub use self :: Fail_ :: * ;
99
+ pub use self :: FailType :: * ;
99
100
use self :: Optval :: * ;
100
101
101
102
use std:: fmt;
@@ -190,7 +191,7 @@ pub struct Matches {
190
191
/// expected format. Use the `Show` implementation to output detailed
191
192
/// information.
192
193
#[ deriving( Clone , PartialEq , Eq ) ]
193
- pub enum Fail {
194
+ pub enum Fail_ {
194
195
/// The option requires an argument but none was passed.
195
196
ArgumentMissing ( String ) ,
196
197
/// The passed option is not declared among the possible options.
@@ -203,8 +204,19 @@ pub enum Fail {
203
204
UnexpectedArgument ( String ) ,
204
205
}
205
206
207
+ /// The type of failure that occurred.
208
+ #[ deriving( PartialEq , Eq ) ]
209
+ #[ allow( missing_docs) ]
210
+ pub enum FailType {
211
+ ArgumentMissing_ ,
212
+ UnrecognizedOption_ ,
213
+ OptionMissing_ ,
214
+ OptionDuplicated_ ,
215
+ UnexpectedArgument_ ,
216
+ }
217
+
206
218
/// The result of parsing a command line with a set of options.
207
- pub type Result = result:: Result < Matches , Fail > ;
219
+ pub type Result = result:: Result < Matches , Fail_ > ;
208
220
209
221
impl Name {
210
222
fn from_str ( nm : & str ) -> Name {
@@ -252,7 +264,7 @@ impl OptGroup {
252
264
( 1 , _) => Opt {
253
265
name : Long ( ( long_name) ) ,
254
266
hasarg : hasarg,
255
- occur : occur,
267
+ occur : occur,
256
268
aliases : vec ! (
257
269
Opt {
258
270
name: Short ( short_name. as_slice( ) . char_at( 0 ) ) ,
@@ -354,12 +366,11 @@ impl Matches {
354
366
pub fn opt_default ( & self , nm : & str , def : & str ) -> Option < String > {
355
367
let vals = self . opt_vals ( nm) ;
356
368
if vals. is_empty ( ) {
357
- None
358
- } else {
359
- match vals[ 0 ] {
360
- Val ( ref s) => Some ( ( * s) . clone ( ) ) ,
361
- _ => Some ( def. to_string ( ) )
362
- }
369
+ return None ;
370
+ }
371
+ match vals[ 0 ] {
372
+ Val ( ref s) => Some ( ( * s) . clone ( ) ) ,
373
+ _ => Some ( def. to_string ( ) )
363
374
}
364
375
}
365
376
@@ -523,15 +534,15 @@ pub fn opt(short_name: &str,
523
534
}
524
535
}
525
536
526
- impl Fail {
527
- /// Convert a `Fail ` enum into an error string.
537
+ impl Fail_ {
538
+ /// Convert a `Fail_ ` enum into an error string.
528
539
#[ deprecated="use `Show` (`{}` format specifier)" ]
529
540
pub fn to_err_msg ( self ) -> String {
530
541
self . to_string ( )
531
542
}
532
543
}
533
544
534
- impl fmt:: Show for Fail {
545
+ impl fmt:: Show for Fail_ {
535
546
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
536
547
match * self {
537
548
ArgumentMissing ( ref nm) => {
@@ -559,7 +570,7 @@ impl fmt::Show for Fail {
559
570
/// `opt_str`, etc. to interrogate results.
560
571
/// # Panics
561
572
///
562
- /// Returns `Err(Fail )` on failure: use the `Show` implementation of `Fail ` to display
573
+ /// Returns `Err(Fail_ )` on failure: use the `Show` implementation of `Fail_ ` to display
563
574
/// information about it.
564
575
pub fn getopts ( args : & [ String ] , optgrps : & [ OptGroup ] ) -> Result {
565
576
let opts: Vec < Opt > = optgrps. iter ( ) . map ( |x| x. long_to_short ( ) ) . collect ( ) ;
@@ -670,15 +681,21 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
670
681
}
671
682
i += 1 ;
672
683
}
673
- for i in range ( 0 u, n_opts) {
684
+ i = 0 u;
685
+ while i < n_opts {
674
686
let n = vals[ i] . len ( ) ;
675
687
let occ = opts[ i] . occur ;
676
- if occ == Req && n == 0 {
677
- return Err ( OptionMissing ( opts[ i] . name . to_string ( ) ) ) ;
688
+ if occ == Req {
689
+ if n == 0 {
690
+ return Err ( OptionMissing ( opts[ i] . name . to_string ( ) ) ) ;
691
+ }
678
692
}
679
- if occ != Multi && n > 1 {
680
- return Err ( OptionDuplicated ( opts[ i] . name . to_string ( ) ) ) ;
693
+ if occ != Multi {
694
+ if n > 1 {
695
+ return Err ( OptionDuplicated ( opts[ i] . name . to_string ( ) ) ) ;
696
+ }
681
697
}
698
+ i += 1 ;
682
699
}
683
700
Ok ( Matches {
684
701
opts : opts,
@@ -949,11 +966,20 @@ fn test_split_within() {
949
966
#[ cfg( test) ]
950
967
mod tests {
951
968
use super :: * ;
952
- use super :: Fail :: * ;
953
969
954
970
use std:: result:: { Err , Ok } ;
955
971
use std:: result;
956
972
973
+ fn check_fail_type ( f : Fail_ , ft : FailType ) {
974
+ match f {
975
+ ArgumentMissing ( _) => assert ! ( ft == ArgumentMissing_ ) ,
976
+ UnrecognizedOption ( _) => assert ! ( ft == UnrecognizedOption_ ) ,
977
+ OptionMissing ( _) => assert ! ( ft == OptionMissing_ ) ,
978
+ OptionDuplicated ( _) => assert ! ( ft == OptionDuplicated_ ) ,
979
+ UnexpectedArgument ( _) => assert ! ( ft == UnexpectedArgument_ )
980
+ }
981
+ }
982
+
957
983
// Tests for reqopt
958
984
#[ test]
959
985
fn test_reqopt ( ) {
@@ -987,7 +1013,7 @@ mod tests {
987
1013
let opts = vec ! ( reqopt( "t" , "test" , "testing" , "TEST" ) ) ;
988
1014
let rs = getopts ( args. as_slice ( ) , opts. as_slice ( ) ) ;
989
1015
match rs {
990
- Err ( OptionMissing ( _ ) ) => { } ,
1016
+ Err ( f ) => check_fail_type ( f , OptionMissing_ ) ,
991
1017
_ => panic ! ( )
992
1018
}
993
1019
}
@@ -998,12 +1024,12 @@ mod tests {
998
1024
let opts = vec ! ( reqopt( "t" , "test" , "testing" , "TEST" ) ) ;
999
1025
let rs = getopts ( long_args. as_slice ( ) , opts. as_slice ( ) ) ;
1000
1026
match rs {
1001
- Err ( ArgumentMissing ( _ ) ) => { } ,
1027
+ Err ( f ) => check_fail_type ( f , ArgumentMissing_ ) ,
1002
1028
_ => panic ! ( )
1003
1029
}
1004
1030
let short_args = vec ! ( "-t" . to_string( ) ) ;
1005
1031
match getopts ( short_args. as_slice ( ) , opts. as_slice ( ) ) {
1006
- Err ( ArgumentMissing ( _ ) ) => { } ,
1032
+ Err ( f ) => check_fail_type ( f , ArgumentMissing_ ) ,
1007
1033
_ => panic ! ( )
1008
1034
}
1009
1035
}
@@ -1014,7 +1040,7 @@ mod tests {
1014
1040
let opts = vec ! ( reqopt( "t" , "test" , "testing" , "TEST" ) ) ;
1015
1041
let rs = getopts ( args. as_slice ( ) , opts. as_slice ( ) ) ;
1016
1042
match rs {
1017
- Err ( OptionDuplicated ( _ ) ) => { } ,
1043
+ Err ( f ) => check_fail_type ( f , OptionDuplicated_ ) ,
1018
1044
_ => panic ! ( )
1019
1045
}
1020
1046
}
@@ -1066,12 +1092,12 @@ mod tests {
1066
1092
let opts = vec ! ( optopt( "t" , "test" , "testing" , "TEST" ) ) ;
1067
1093
let rs = getopts ( long_args. as_slice ( ) , opts. as_slice ( ) ) ;
1068
1094
match rs {
1069
- Err ( ArgumentMissing ( _ ) ) => { } ,
1095
+ Err ( f ) => check_fail_type ( f , ArgumentMissing_ ) ,
1070
1096
_ => panic ! ( )
1071
1097
}
1072
1098
let short_args = vec ! ( "-t" . to_string( ) ) ;
1073
1099
match getopts ( short_args. as_slice ( ) , opts. as_slice ( ) ) {
1074
- Err ( ArgumentMissing ( _ ) ) => { } ,
1100
+ Err ( f ) => check_fail_type ( f , ArgumentMissing_ ) ,
1075
1101
_ => panic ! ( )
1076
1102
}
1077
1103
}
@@ -1082,7 +1108,7 @@ mod tests {
1082
1108
let opts = vec ! ( optopt( "t" , "test" , "testing" , "TEST" ) ) ;
1083
1109
let rs = getopts ( args. as_slice ( ) , opts. as_slice ( ) ) ;
1084
1110
match rs {
1085
- Err ( OptionDuplicated ( _ ) ) => { } ,
1111
+ Err ( f ) => check_fail_type ( f , OptionDuplicated_ ) ,
1086
1112
_ => panic ! ( )
1087
1113
}
1088
1114
}
@@ -1130,7 +1156,9 @@ mod tests {
1130
1156
let opts = vec ! ( optflag( "t" , "test" , "testing" ) ) ;
1131
1157
let rs = getopts ( args. as_slice ( ) , opts. as_slice ( ) ) ;
1132
1158
match rs {
1133
- Err ( UnexpectedArgument ( _) ) => { } ,
1159
+ Err ( f) => {
1160
+ check_fail_type ( f, UnexpectedArgument_ ) ;
1161
+ }
1134
1162
_ => panic ! ( )
1135
1163
}
1136
1164
}
@@ -1141,7 +1169,7 @@ mod tests {
1141
1169
let opts = vec ! ( optflag( "t" , "test" , "testing" ) ) ;
1142
1170
let rs = getopts ( args. as_slice ( ) , opts. as_slice ( ) ) ;
1143
1171
match rs {
1144
- Err ( OptionDuplicated ( _ ) ) => { } ,
1172
+ Err ( f ) => check_fail_type ( f , OptionDuplicated_ ) ,
1145
1173
_ => panic ! ( )
1146
1174
}
1147
1175
}
@@ -1289,12 +1317,12 @@ mod tests {
1289
1317
let opts = vec ! ( optmulti( "t" , "test" , "testing" , "TEST" ) ) ;
1290
1318
let rs = getopts ( long_args. as_slice ( ) , opts. as_slice ( ) ) ;
1291
1319
match rs {
1292
- Err ( ArgumentMissing ( _ ) ) => { } ,
1320
+ Err ( f ) => check_fail_type ( f , ArgumentMissing_ ) ,
1293
1321
_ => panic ! ( )
1294
1322
}
1295
1323
let short_args = vec ! ( "-t" . to_string( ) ) ;
1296
1324
match getopts ( short_args. as_slice ( ) , opts. as_slice ( ) ) {
1297
- Err ( ArgumentMissing ( _ ) ) => { } ,
1325
+ Err ( f ) => check_fail_type ( f , ArgumentMissing_ ) ,
1298
1326
_ => panic ! ( )
1299
1327
}
1300
1328
}
@@ -1324,12 +1352,12 @@ mod tests {
1324
1352
let opts = vec ! ( optmulti( "t" , "test" , "testing" , "TEST" ) ) ;
1325
1353
let rs = getopts ( long_args. as_slice ( ) , opts. as_slice ( ) ) ;
1326
1354
match rs {
1327
- Err ( UnrecognizedOption ( _ ) ) => { } ,
1355
+ Err ( f ) => check_fail_type ( f , UnrecognizedOption_ ) ,
1328
1356
_ => panic ! ( )
1329
1357
}
1330
1358
let short_args = vec ! ( "-u" . to_string( ) ) ;
1331
1359
match getopts ( short_args. as_slice ( ) , opts. as_slice ( ) ) {
1332
- Err ( UnrecognizedOption ( _ ) ) => { } ,
1360
+ Err ( f ) => check_fail_type ( f , UnrecognizedOption_ ) ,
1333
1361
_ => panic ! ( )
1334
1362
}
1335
1363
}
@@ -1465,14 +1493,14 @@ mod tests {
1465
1493
#[ test]
1466
1494
fn test_long_to_short ( ) {
1467
1495
let mut short = Opt {
1468
- name : Name :: Long ( "banana" . to_string ( ) ) ,
1469
- hasarg : HasArg :: Yes ,
1470
- occur : Occur :: Req ,
1496
+ name : Long ( "banana" . to_string ( ) ) ,
1497
+ hasarg : Yes ,
1498
+ occur : Req ,
1471
1499
aliases : Vec :: new ( ) ,
1472
1500
} ;
1473
- short. aliases = vec ! ( Opt { name: Name :: Short ( 'b' ) ,
1474
- hasarg: HasArg :: Yes ,
1475
- occur: Occur :: Req ,
1501
+ short. aliases = vec ! ( Opt { name: Short ( 'b' ) ,
1502
+ hasarg: Yes ,
1503
+ occur: Req ,
1476
1504
aliases: Vec :: new( ) } ) ;
1477
1505
let verbose = reqopt ( "b" , "banana" , "some bananas" , "VAL" ) ;
1478
1506
0 commit comments