mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
feat(macros): add convenience macro to get a typed value or exit
This commit is contained in:
parent
8752700fbb
commit
4b7cd3ea49
1 changed files with 22 additions and 0 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue