@@ -140,7 +140,9 @@ pub fn optflag(name: &str) -> Opt {
140
140
return Opt { name : mkname ( name) , hasarg : No , occur : Optional } ;
141
141
}
142
142
143
- /// Create an option that is optional and does not take an argument
143
+ /** Create an option that is optional, does not take an argument,
144
+ * and may occur multiple times.
145
+ */
144
146
pub fn optflagmulti ( name : & str ) -> Opt {
145
147
return Opt { name : mkname ( name) , hasarg : No , occur : Multi } ;
146
148
}
@@ -369,7 +371,14 @@ fn opt_vals(mm: &Matches, nm: &str) -> ~[Optval] {
369
371
} ;
370
372
}
371
373
372
- fn opt_val( mm: & Matches , nm: & str ) -> Optval { opt_vals ( mm, nm) [ 0 ] . clone ( ) }
374
+ fn opt_val( mm: & Matches , nm: & str ) -> Option <Optval > {
375
+ let vals = opt_vals ( mm, nm) ;
376
+ if ( vals. is_empty ( ) ) {
377
+ None
378
+ } else {
379
+ Some ( opt_vals ( mm, nm) [ 0 ] . clone ( ) )
380
+ }
381
+ }
373
382
374
383
/// Returns true if an option was matched
375
384
pub fn opt_present ( mm : & Matches , nm : & str ) -> bool {
@@ -400,7 +409,10 @@ pub fn opts_present(mm: &Matches, names: &[~str]) -> bool {
400
409
* argument
401
410
*/
402
411
pub fn opt_str ( mm : & Matches , nm : & str ) -> ~str {
403
- return match opt_val ( mm, nm) { Val ( s) => s, _ => fail ! ( ) } ;
412
+ return match opt_val ( mm, nm) {
413
+ Some ( Val ( s) ) => s,
414
+ _ => fail ! ( )
415
+ } ;
404
416
}
405
417
406
418
/**
@@ -412,7 +424,7 @@ pub fn opt_str(mm: &Matches, nm: &str) -> ~str {
412
424
pub fn opts_str( mm : & Matches , names : & [ ~str ] ) -> ~str {
413
425
for names. iter( ) . advance |nm| {
414
426
match opt_val( mm, * nm) {
415
- Val ( ref s) => return ( * s) . clone( ) ,
427
+ Some ( Val ( ref s) ) => return ( * s) . clone( ) ,
416
428
_ => ( )
417
429
}
418
430
}
@@ -1318,24 +1330,41 @@ mod tests {
1318
1330
1319
1331
#[test]
1320
1332
fn test_multi() {
1321
- let args = ~[~" -e", ~" foo", ~" --encrypt", ~" foo"];
1322
1333
let opts = ~[optopt(" e"), optopt(" encrypt"), optopt(" f")];
1323
- let matches = &match getopts(args, opts) {
1334
+
1335
+ let args_single = ~[~" -e", ~" foo"];
1336
+ let matches_single = &match getopts(args_single, opts) {
1337
+ result::Ok(m) => m,
1338
+ result::Err(_) => fail!()
1339
+ };
1340
+ assert!(opts_present(matches_single, [~" e"]));
1341
+ assert!(opts_present(matches_single, [~" encrypt", ~" e"]));
1342
+ assert!(opts_present(matches_single, [~" e", ~" encrypt"]));
1343
+ assert!(!opts_present(matches_single, [~" encrypt"]));
1344
+ assert!(!opts_present(matches_single, [~" thing"]));
1345
+ assert!(!opts_present(matches_single, []));
1346
+
1347
+ assert_eq!(opts_str(matches_single, [~" e"]), ~" foo");
1348
+ assert_eq!(opts_str(matches_single, [~" e", ~" encrypt"]), ~" foo");
1349
+ assert_eq!(opts_str(matches_single, [~" encrypt", ~" e"]), ~" foo");
1350
+
1351
+ let args_both = ~[~" -e", ~" foo", ~" --encrypt", ~" foo"];
1352
+ let matches_both = &match getopts(args_both, opts) {
1324
1353
result::Ok(m) => m,
1325
1354
result::Err(_) => fail!()
1326
1355
};
1327
- assert!(opts_present(matches , [~" e"]));
1328
- assert!(opts_present(matches , [~" encrypt"]));
1329
- assert!(opts_present(matches , [~" encrypt", ~" e"]));
1330
- assert!(opts_present(matches , [~" e", ~" encrypt"]));
1331
- assert!(!opts_present(matches , [~" f"]));
1332
- assert!(!opts_present(matches , [~" thing"]));
1333
- assert!(!opts_present(matches , []));
1334
-
1335
- assert_eq!(opts_str(matches , [~" e"]), ~" foo");
1336
- assert_eq!(opts_str(matches , [~" encrypt"]), ~" foo");
1337
- assert_eq!(opts_str(matches , [~" e", ~" encrypt"]), ~" foo");
1338
- assert_eq!(opts_str(matches , [~" encrypt", ~" e"]), ~" foo");
1356
+ assert!(opts_present(matches_both , [~" e"]));
1357
+ assert!(opts_present(matches_both , [~" encrypt"]));
1358
+ assert!(opts_present(matches_both , [~" encrypt", ~" e"]));
1359
+ assert!(opts_present(matches_both , [~" e", ~" encrypt"]));
1360
+ assert!(!opts_present(matches_both , [~" f"]));
1361
+ assert!(!opts_present(matches_both , [~" thing"]));
1362
+ assert!(!opts_present(matches_both , []));
1363
+
1364
+ assert_eq!(opts_str(matches_both , [~" e"]), ~" foo");
1365
+ assert_eq!(opts_str(matches_both , [~" encrypt"]), ~" foo");
1366
+ assert_eq!(opts_str(matches_both , [~" e", ~" encrypt"]), ~" foo");
1367
+ assert_eq!(opts_str(matches_both , [~" encrypt", ~" e"]), ~" foo");
1339
1368
}
1340
1369
1341
1370
#[test]
0 commit comments