Skip to content

Commit

Permalink
Fix opts_str.
Browse files Browse the repository at this point in the history
opt_val doesn't not fail! for missing options.

Closes #6492
  • Loading branch information
dim-an committed Jul 30, 2013
1 parent 0ed8713 commit ed0f014
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/libextra/getopts.rs
Expand Up @@ -369,7 +369,14 @@ fn opt_vals(mm: &Matches, nm: &str) -> ~[Optval] {
};
}

fn opt_val(mm: &Matches, nm: &str) -> Optval { opt_vals(mm, nm)[0].clone() }
fn opt_val(mm: &Matches, nm: &str) -> Option<Optval> {
let vals = opt_vals(mm, nm);
if (vals.is_empty()) {
None
} else {
Some(opt_vals(mm, nm)[0].clone())
}
}

/// Returns true if an option was matched
pub fn opt_present(mm: &Matches, nm: &str) -> bool {
Expand Down Expand Up @@ -400,7 +407,10 @@ pub fn opts_present(mm: &Matches, names: &[~str]) -> bool {
* argument
*/
pub fn opt_str(mm: &Matches, nm: &str) -> ~str {
return match opt_val(mm, nm) { Val(s) => s, _ => fail!() };
return match opt_val(mm, nm) {
Some(Val(s)) => s,
_ => fail!()
};
}

/**
Expand All @@ -412,7 +422,7 @@ pub fn opt_str(mm: &Matches, nm: &str) -> ~str {
pub fn opts_str(mm: &Matches, names: &[~str]) -> ~str {
for names.iter().advance |nm| {
match opt_val(mm, *nm) {
Val(ref s) => return (*s).clone(),
Some(Val(ref s)) => return (*s).clone(),
_ => ()
}
}
Expand Down

0 comments on commit ed0f014

Please sign in to comment.