Skip to content

Commit 768eccf

Browse files
authored
Merge pull request #70 from prataprc/opt_get
implement opt_get() method call on Matches.
2 parents 4976a82 + 26ddb4c commit 768eccf

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

src/lib.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ use std::ffi::OsStr;
118118
use std::fmt;
119119
use std::iter::{repeat, IntoIterator};
120120
use std::result;
121+
use std::str::FromStr;
121122

122123
use unicode_width::UnicodeWidthStr;
123124

@@ -819,6 +820,33 @@ impl Matches {
819820
}
820821
}
821822

823+
/// Returns some matching value or `None`.
824+
///
825+
/// Similar to opt_str, also converts matching argument using FromStr.
826+
pub fn opt_get<T>(&self, nm: &str) -> result::Result<Option<T>, T::Err>
827+
where T: FromStr
828+
{
829+
match self.opt_val(nm) {
830+
Some(Val(s)) => Ok(Some(s.parse()?)),
831+
Some(Given) => Ok(None),
832+
None => Ok(None),
833+
}
834+
}
835+
836+
/// Returns a matching value or default.
837+
///
838+
/// Similar to opt_default, except the two differences.
839+
/// Instead of returning None when argument was not present, return `def`.
840+
/// Instead of returning &str return type T, parsed using str::parse().
841+
pub fn opt_get_default<T>(&self, nm: &str, def: T)
842+
-> result::Result<T, T::Err> where T: FromStr
843+
{
844+
match self.opt_val(nm) {
845+
Some(Val(s)) => s.parse(),
846+
Some(Given) => Ok(def),
847+
None => Ok(def),
848+
}
849+
}
822850
}
823851

824852
fn is_arg(arg: &str) -> bool {
@@ -2002,4 +2030,70 @@ Options:
20022030
Err(e) => panic!("{}", e)
20032031
}
20042032
}
2033+
2034+
#[test]
2035+
fn test_opt_default() {
2036+
let mut opts = Options::new();
2037+
opts.optflag("h", "help", "Description");
2038+
opts.optflag("i", "ignore", "Description");
2039+
opts.optflag("r", "run", "Description");
2040+
opts.long_only(false);
2041+
2042+
let args: Vec<String> = ["-i", "-r", "10"]
2043+
.iter().map(|x| x.to_string()).collect();
2044+
let matches = &match opts.parse(&args) {
2045+
Ok(m) => m,
2046+
Err(e) => panic!("{}", e)
2047+
};
2048+
assert_eq!(matches.opt_default("help", ""), None);
2049+
assert_eq!(matches.opt_default("i", "def"), Some("def".to_string()));
2050+
}
2051+
2052+
#[test]
2053+
fn test_opt_get() {
2054+
let mut opts = Options::new();
2055+
opts.optflag("h", "help", "Description");
2056+
opts.optflagopt("i", "ignore", "Description", "true | false");
2057+
opts.optflagopt("r", "run", "Description", "0 .. 10");
2058+
opts.optflagopt("p", "percent", "Description", "0.0 .. 10.0");
2059+
opts.long_only(false);
2060+
2061+
let args: Vec<String> = [
2062+
"-i", "true", "-p", "1.1"
2063+
].iter().map(|x| x.to_string()).collect();
2064+
let matches = &match opts.parse(&args) {
2065+
Ok(m) => m,
2066+
Err(e) => panic!("{}", e)
2067+
};
2068+
let h_arg = matches.opt_get::<i32>("help");
2069+
assert_eq!(h_arg, Ok(None));
2070+
let i_arg = matches.opt_get("i");
2071+
assert_eq!(i_arg, Ok(Some(true)));
2072+
let p_arg = matches.opt_get("p");
2073+
assert_eq!(p_arg, Ok(Some(1.1)));
2074+
}
2075+
2076+
#[test]
2077+
fn test_opt_get_default() {
2078+
let mut opts = Options::new();
2079+
opts.optflag("h", "help", "Description");
2080+
opts.optflagopt("i", "ignore", "Description", "true | false");
2081+
opts.optflagopt("r", "run", "Description", "0 .. 10");
2082+
opts.optflagopt("p", "percent", "Description", "0.0 .. 10.0");
2083+
opts.long_only(false);
2084+
2085+
let args: Vec<String> = [
2086+
"-i", "true", "-p", "1.1"
2087+
].iter().map(|x| x.to_string()).collect();
2088+
let matches = &match opts.parse(&args) {
2089+
Ok(m) => m,
2090+
Err(e) => panic!("{}", e)
2091+
};
2092+
let h_arg =matches.opt_get_default("help", 10);
2093+
assert_eq!(h_arg, Ok(10));
2094+
let i_arg = matches.opt_get_default("i", false);
2095+
assert_eq!(i_arg, Ok(true));
2096+
let p_arg = matches.opt_get_default("p", 10.2);
2097+
assert_eq!(p_arg, Ok(1.1));
2098+
}
20052099
}

0 commit comments

Comments
 (0)