From 8752700fbb30e89ee68adbce24489ae9a24d33a9 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Tue, 14 Apr 2015 12:06:15 -0400 Subject: [PATCH] feat(macros): add convenience macro to get a typed value --- src/macros.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/macros.rs b/src/macros.rs index f549c6f3fae..7c987eb969f 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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")) + } + }; } \ No newline at end of file