In Clap v3.1.6 when I define struct
I can skip explicit declaration of attribute long name and it will be taken from the variable name:
use clap::Parser;
#[derive(Parser, Debug, PartialEq)]
#[clap(rename_all = "kebab_case")]
struct Opt {
#[clap(long, // <-- here long name is implicitly set to `boo-boo` due to `rename_all`
)]
boo_boo: Boo,
}
Now the same parsing doesn't work with enums, so if I define enum like:
#[derive(Parser, Debug, PartialEq)]
#[clap(rename_all = "kebab_case")]
enum Boo {
Aa,
Bb,
}
It will return error that FromStr
is not implemented.
It looks like I have to do some cumbersome constructions with manual parsing and specifying possible values:
#[derive(Parser, Debug, PartialEq)]
#[clap(rename_all = "kebab_case")]
struct Opt {
#[clap(long,
parse(try_from_str = boo_parser),
possible_values = &["aa", "bb"]
)]
boo_boo: Boo,
}
fn boo_parser(s: &str) -> Result<Boo, &'static str> {
match s {
"aa" => Ok(Boo::Aa),
"bb" => Ok(Boo::Bb),
_ => Err("unsupported value"),
}
}
#[derive(Parser, Debug, PartialEq)]
#[clap(rename_all = "kebab_case")]
enum Boo {
Aa,
Bb,
}
This way I have to implement my own parser and specify "aa"
in two places without an option to rely on rename_all
style.
The question is if there is something I've missed and if it is possible to simplify enums values parsing.