macro_rules! value_t { ($m:ident, $v:expr, $t:ty) => { ... }; ($m:ident.value_of($v:expr), $t:ty) => { ... }; }
Expand description
Convenience macro getting a typed value T
where T
implements std::str::FromStr
from an
argument value. This macro returns a Result<T,String>
which allows you as the developer to
decide what you’d like to do on a failed parse. There are two types of errors, parse failures
and those where the argument wasn’t present (such as a non-required argument). You can use
it to get a single value, or a iterator as with the ArgMatches::values_of
Examples
let matches = App::new("myapp")
.arg_from_usage("[length] 'Set the length to use as a pos whole num, i.e. 20'")
.get_matches();
let len = value_t!(matches.value_of("length"), u32).unwrap_or_else(|e| e.exit());
let also_len = value_t!(matches, "length", u32).unwrap_or_else(|e| e.exit());
println!("{} + 2: {}", len, len + 2);