clap::value_t_or_exit! [] [src]

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 value\n\n{}\n\nPlease re-run with {} for \
                            more information",
                            ::clap::Format::Error("error:"),
                            ::clap::Format::Warning(v.to_string()),
                            $m.usage(),
                            ::clap::Format::Good("--help"));
                        ::std::process::exit(1);
                    }
                }
            },
            None => {
                println!("{} The argument '{}' was not found or is not valid\n\n{}\n\nPlease re-run with \
                    {} for more information",
                    ::clap::Format::Error("error:"),
                    ::clap::Format::Warning($v.to_string()),
                    $m.usage(),
                    ::clap::Format::Good("--help"));
                ::std::process::exit(1);
            }
        }
    };
    ($m:ident.values_of($v:expr), $t:ty) => {
        match $m.values_of($v) {
            Some(ref v) => {
                let mut tmp = Vec::with_capacity(v.len());
                for pv in v {
                    match pv.parse::<$t>() {
                        Ok(rv) => tmp.push(rv),
                        Err(_)  => {
                            println!("{} '{}' isn't a valid value\n\n{}\n\nPlease re-run with {} for more \
                                information",
                                ::clap::Format::Error("error:"),
                                ::clap::Format::Warning(pv),
                                $m.usage(),
                                ::clap::Format::Good("--help"));
                            ::std::process::exit(1);
                        }
                    }
                }
                tmp
            },
            None => {
                println!("{} The argument '{}' not found or is not valid\n\n{}\n\nPlease re-run with \
                    {} for more information",
                    ::clap::Format::Error("error:"),
                    ::clap::Format::Warning($v.to_string()),
                    $m.usage(),
                    ::clap::Format::Good("--help"));
                ::std::process::exit(1);
            }
        }
    };
}

Convenience macro getting a typed value T where T implements std::str::FromStr This macro returns a T or Vec<T> or exits with a usage string upon failure. This removes some of the boiler plate to handle failures from value_t! above.

You can use it to get a single value T, or a Vec<T> with the values_of()

NOTE: This should only be used on required arguments, as it can be confusing to the user why they are getting error messages when it appears they're entering all required argumetns.

NOTE: Be cautious, as since this a macro invocation it's not exactly like standard syntax.

Example single value

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_or_exit!(matches.value_of("length"), u32);

println!("{} + 2: {}", len, len + 2);

Example multiple values

let matches = App::new("myapp")
                  .arg_from_usage("[seq]... 'A sequence of pos whole nums, i.e. 20 45'")
                  .get_matches();
for v in value_t_or_exit!(matches.values_of("seq"), u32) {
    println!("{} + 2: {}", v, v + 2);
}