Skip to content

Commit ed0f014

Browse files
committed
Fix opts_str.
opt_val doesn't not fail! for missing options. Closes #6492
1 parent 0ed8713 commit ed0f014

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/libextra/getopts.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,14 @@ fn opt_vals(mm: &Matches, nm: &str) -> ~[Optval] {
369369
};
370370
}
371371

372-
fn opt_val(mm: &Matches, nm: &str) -> Optval { opt_vals(mm, nm)[0].clone() }
372+
fn opt_val(mm: &Matches, nm: &str) -> Option<Optval> {
373+
let vals = opt_vals(mm, nm);
374+
if (vals.is_empty()) {
375+
None
376+
} else {
377+
Some(opt_vals(mm, nm)[0].clone())
378+
}
379+
}
373380

374381
/// Returns true if an option was matched
375382
pub fn opt_present(mm: &Matches, nm: &str) -> bool {
@@ -400,7 +407,10 @@ pub fn opts_present(mm: &Matches, names: &[~str]) -> bool {
400407
* argument
401408
*/
402409
pub fn opt_str(mm: &Matches, nm: &str) -> ~str {
403-
return match opt_val(mm, nm) { Val(s) => s, _ => fail!() };
410+
return match opt_val(mm, nm) {
411+
Some(Val(s)) => s,
412+
_ => fail!()
413+
};
404414
}
405415

406416
/**
@@ -412,7 +422,7 @@ pub fn opt_str(mm: &Matches, nm: &str) -> ~str {
412422
pub fn opts_str(mm: &Matches, names: &[~str]) -> ~str {
413423
for names.iter().advance |nm| {
414424
match opt_val(mm, *nm) {
415-
Val(ref s) => return (*s).clone(),
425+
Some(Val(ref s)) => return (*s).clone(),
416426
_ => ()
417427
}
418428
}

0 commit comments

Comments
 (0)