Skip to content

Commit

Permalink
Merge pull request #70 from prataprc/opt_get
Browse files Browse the repository at this point in the history
implement opt_get() method call on Matches.
  • Loading branch information
KodrAus committed Jun 28, 2018
2 parents 4976a82 + 26ddb4c commit 768eccf
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/lib.rs
Expand Up @@ -118,6 +118,7 @@ use std::ffi::OsStr;
use std::fmt;
use std::iter::{repeat, IntoIterator};
use std::result;
use std::str::FromStr;

use unicode_width::UnicodeWidthStr;

Expand Down Expand Up @@ -819,6 +820,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 @@ -2002,4 +2030,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));
}
}

0 comments on commit 768eccf

Please sign in to comment.