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