Skip to content

Commit

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

0 comments on commit 8752700

Please sign in to comment.