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

This commit is contained in:
Kevin K 2015-04-14 12:06:15 -04:00
parent b67f2e537e
commit 8752700fbb

View file

@ -27,4 +27,20 @@ macro_rules! for_match {
} }
} }
}; };
}
/// Convenience macro getting a typed value
#[macro_export]
macro_rules! value_t {
($m:ident.value_of($v:expr), $t:ty) => {
match $m.value_of($v) {
Some(v) => {
match v.parse::<$t>() {
Ok(val) => Ok(val),
Err(_) => Err(format!("{} isn't a valid {}",v,stringify!($t))),
}
},
None => Err(format!("Argument not found"))
}
};
} }