2015-04-28 01:10:37 -04:00
|
|
|
extern crate pirate;
|
|
|
|
|
2015-06-24 16:04:50 -04:00
|
|
|
use pirate::{Matches, Match, matches, usage, vars};
|
2015-04-28 01:10:37 -04:00
|
|
|
|
2015-06-24 16:04:50 -04:00
|
|
|
#[test]
|
2015-04-28 01:10:37 -04:00
|
|
|
fn main() {
|
2015-06-24 17:02:16 -04:00
|
|
|
let env_args: Vec<String> = vec![String::from("test"), String::from("-a"), String::from("2"),
|
|
|
|
String::from("3")];
|
2015-06-28 21:53:27 -04:00
|
|
|
let opts = vec!["a/addend#The right side of the addition equation; default=1:", "#Required Arguments",
|
|
|
|
":augend#The left side of an addition equation"];
|
2015-06-23 12:00:50 +00:00
|
|
|
let mut vars = vars("test", &opts).unwrap();
|
2015-06-16 14:28:14 -04:00
|
|
|
|
2015-06-28 21:53:27 -04:00
|
|
|
let matches: Matches = match matches(&env_args, &mut vars) {
|
2015-06-10 18:14:11 +00:00
|
|
|
Ok(m) => m,
|
2015-06-24 16:04:50 -04:00
|
|
|
Err(why) => {
|
|
|
|
println!("Error: {}", why);
|
|
|
|
usage(&vars);
|
|
|
|
return;
|
|
|
|
}
|
2015-04-28 01:10:37 -04:00
|
|
|
};
|
2015-06-16 14:28:14 -04:00
|
|
|
|
2015-06-23 12:00:50 +00:00
|
|
|
if matches.has_match("help") {
|
|
|
|
usage(&vars);
|
|
|
|
return;
|
2015-06-16 14:28:14 -04:00
|
|
|
}
|
2015-06-24 16:04:50 -04:00
|
|
|
|
|
|
|
let augend: i32 = matches.get("augend").unwrap().parse::<i32>().unwrap();
|
2015-06-23 12:00:50 +00:00
|
|
|
|
2015-06-24 16:04:50 -04:00
|
|
|
let addend: i32 = match matches.get("addend") {
|
|
|
|
Some(a) => a.parse::<i32>().unwrap(),
|
|
|
|
None => 1
|
2015-06-23 12:00:50 +00:00
|
|
|
};
|
|
|
|
|
2015-06-24 16:04:50 -04:00
|
|
|
let sum = augend + addend;
|
|
|
|
|
|
|
|
assert_eq!(augend, 3);
|
|
|
|
assert_eq!(addend, 2);
|
|
|
|
assert_eq!(sum, 5);
|
2015-04-28 01:10:37 -04:00
|
|
|
}
|