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