Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement opt_get() method call on Matches. #70

Merged
merged 2 commits into from Jun 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/lib.rs
Expand Up @@ -119,6 +119,7 @@ use std::ffi::OsStr;
use std::fmt;
use std::iter::{repeat, IntoIterator};
use std::result;
use std::str::FromStr;

/// A description of the options that a program can handle.
pub struct Options {
Expand Down Expand Up @@ -841,6 +842,33 @@ impl Matches {
}
}

/// Returns some matching value or `None`.
///
/// Similar to opt_str, also converts matching argument using FromStr.
pub fn opt_get<T>(&self, nm: &str) -> result::Result<Option<T>, T::Err>
where T: FromStr
{
match self.opt_val(nm) {
Some(Val(s)) => Ok(Some(s.parse()?)),
Some(Given) => Ok(None),
None => Ok(None),
}
}

/// Returns a matching value or default.
///
/// Similar to opt_default, except the two differences.
/// Instead of returning None when argument was not present, return `def`.
/// Instead of returning &str return type T, parsed using str::parse().
pub fn opt_get_default<T>(&self, nm: &str, def: T)
-> result::Result<T, T::Err> where T: FromStr
{
match self.opt_val(nm) {
Some(Val(s)) => s.parse(),
Some(Given) => Ok(def),
None => Ok(def),
}
}
}

fn is_arg(arg: &str) -> bool {
Expand Down Expand Up @@ -2018,4 +2046,70 @@ Options:
Err(e) => panic!("{}", e)
}
}

#[test]
fn test_opt_default() {
let mut opts = Options::new();
opts.optflag("h", "help", "Description");
opts.optflag("i", "ignore", "Description");
opts.optflag("r", "run", "Description");
opts.long_only(false);

let args: Vec<String> = ["-i", "-r", "10"]
.iter().map(|x| x.to_string()).collect();
let matches = &match opts.parse(&args) {
Ok(m) => m,
Err(e) => panic!("{}", e)
};
assert_eq!(matches.opt_default("help", ""), None);
assert_eq!(matches.opt_default("i", "def"), Some("def".to_string()));
}

#[test]
fn test_opt_get() {
let mut opts = Options::new();
opts.optflag("h", "help", "Description");
opts.optflagopt("i", "ignore", "Description", "true | false");
opts.optflagopt("r", "run", "Description", "0 .. 10");
opts.optflagopt("p", "percent", "Description", "0.0 .. 10.0");
opts.long_only(false);

let args: Vec<String> = [
"-i", "true", "-p", "1.1"
].iter().map(|x| x.to_string()).collect();
let matches = &match opts.parse(&args) {
Ok(m) => m,
Err(e) => panic!("{}", e)
};
let h_arg = matches.opt_get::<i32>("help");
assert_eq!(h_arg, Ok(None));
let i_arg = matches.opt_get("i");
assert_eq!(i_arg, Ok(Some(true)));
let p_arg = matches.opt_get("p");
assert_eq!(p_arg, Ok(Some(1.1)));
}

#[test]
fn test_opt_get_default() {
let mut opts = Options::new();
opts.optflag("h", "help", "Description");
opts.optflagopt("i", "ignore", "Description", "true | false");
opts.optflagopt("r", "run", "Description", "0 .. 10");
opts.optflagopt("p", "percent", "Description", "0.0 .. 10.0");
opts.long_only(false);

let args: Vec<String> = [
"-i", "true", "-p", "1.1"
].iter().map(|x| x.to_string()).collect();
let matches = &match opts.parse(&args) {
Ok(m) => m,
Err(e) => panic!("{}", e)
};
let h_arg =matches.opt_get_default("help", 10);
assert_eq!(h_arg, Ok(10));
let i_arg = matches.opt_get_default("i", false);
assert_eq!(i_arg, Ok(true));
let p_arg = matches.opt_get_default("p", 10.2);
assert_eq!(p_arg, Ok(1.1));
}
}