Skip to content

Commit

Permalink
feat(macros): add convenience macro to get a typed value or exit
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Apr 14, 2015
1 parent 8752700 commit 4b7cd3e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
}

0 comments on commit 4b7cd3e

Please sign in to comment.