1 use clap::Parser;
2
3 #[derive(Parser, Debug, PartialEq)]
4 #[clap(author, version, about, long_about = None)]
5 struct Opt {
6 // Default parser for `Set` is FromStr::from_str.
7 // `impl FromStr for bool` parses `true` or `false` so this
8 // works as expected.
9 #[clap(long, action = clap::ArgAction::Set)]
10 foo: bool,
11
12 // Of course, this could be done with an explicit parser function.
13 #[clap(long, action = clap::ArgAction::Set, value_parser = true_or_false, default_value_t)]
14 bar: bool,
15
16 // `bool` can be positional only with explicit `action` annotation
17 #[clap(action = clap::ArgAction::Set)]
18 boom: bool,
19 }
20
true_or_false(s: &str) -> Result<bool, &'static str>21 fn true_or_false(s: &str) -> Result<bool, &'static str> {
22 match s {
23 "true" => Ok(true),
24 "false" => Ok(false),
25 _ => Err("expected `true` or `false`"),
26 }
27 }
28
main()29 fn main() {
30 let opt = Opt::parse();
31 dbg!(opt);
32 }
33