Skip to content

Commit 4f4f69d

Browse files
committed
Fix std::getopts::opts_present() to check value.
Currently the opts_present() function only checks to see if the option is configured in the match, but doesn't actually check to see if the option value has been set. This means that opt_present('h') may return false while opts_present([~'h']) returns true. Add a test case to catch this condition and fix opts_present() to check the value before returning true. Note, there is another API difference between these two functions that this does not address. Currently if you pass a non-configured option to opt_present() the program will fail!(), but opts_present() simply returns false. If it is acceptable to standardize on the fail!() then opts_present() should probably be implemented in terms of the opt_present() function.
1 parent 267f6c2 commit 4f4f69d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/libstd/getopts.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ pub fn opt_count(mm: &Matches, nm: &str) -> uint {
369369
pub fn opts_present(mm: &Matches, names: &[~str]) -> bool {
370370
for vec::each(names) |nm| {
371371
match find_opt(mm.opts, mkname(*nm)) {
372-
Some(_) => return true,
373-
None => ()
374-
}
372+
Some(id) if !mm.vals[id].is_empty() => return true,
373+
_ => (),
374+
};
375375
}
376376
false
377377
}
@@ -1174,7 +1174,7 @@ mod tests {
11741174
#[test]
11751175
pub fn test_multi() {
11761176
let args = ~[~"-e", ~"foo", ~"--encrypt", ~"foo"];
1177-
let opts = ~[optopt(~"e"), optopt(~"encrypt")];
1177+
let opts = ~[optopt(~"e"), optopt(~"encrypt"), optopt(~"f")];
11781178
let matches = &match getopts(args, opts) {
11791179
result::Ok(m) => m,
11801180
result::Err(_) => fail!()
@@ -1183,6 +1183,7 @@ mod tests {
11831183
fail_unless!(opts_present(matches, ~[~"encrypt"]));
11841184
fail_unless!(opts_present(matches, ~[~"encrypt", ~"e"]));
11851185
fail_unless!(opts_present(matches, ~[~"e", ~"encrypt"]));
1186+
fail_unless!(!opts_present(matches, ~[~"f"]));
11861187
fail_unless!(!opts_present(matches, ~[~"thing"]));
11871188
fail_unless!(!opts_present(matches, ~[]));
11881189

0 commit comments

Comments
 (0)