feat(macros): add convenience macro to get a typed value or exit

This commit is contained in:
Kevin K 2015-04-14 12:30:53 -04:00
parent 8752700fbb
commit 4b7cd3ea49

View file

@ -43,4 +43,26 @@ macro_rules! value_t {
None => Err(format!("Argument not found"))
}
};
}
/// Convenience macro getting a typed value or exiting on failure
#[macro_export]
macro_rules! value_t_or_exit {
($m:ident.value_of($v:expr), $t:ty) => {
match $m.value_of($v) {
Some(v) => {
match v.parse::<$t>() {
Ok(val) => val,
Err(_) => {
println!("{} isn't a valid {}\n{}\nPlease re-run with --help for more information",v,stringify!($t), $m.usage());
::std::process::exit(1);
}
}
},
None => {
println!("Argument not found\n{}\nPlease re-run with --help for more information", $m.usage());
::std::process::exit(1);
}
}
};
}