use structopt::StructOpt;
fn true_or_false(s: &str) -> Result<bool, &'static str> {
match s {
"true" => Ok(true),
"false" => Ok(false),
_ => Err("expected `true` or `false`"),
}
}
#[derive(StructOpt, Debug, PartialEq)]
struct Opt {
#[structopt(long, parse(try_from_str))]
foo: bool,
#[structopt(long, parse(try_from_str = true_or_false))]
bar: bool,
#[structopt(long, parse(try_from_str))]
boom: bool,
}
fn main() {
assert_eq!(
Opt::from_iter(&["test", "--foo=true", "--bar=false", "true"]),
Opt {
foo: true,
bar: false,
boom: true
}
);
assert!(Opt::from_iter_safe(&["test", "--foo=beauty"]).is_err());
}