I'd like to use ArgAction::Count
to count the number of occurrences of my --verbose
flag, and then send the result through a closure to convert it to a Verbosity
enum.
At the moment I'm trying this:
use clap::{Parser, ArgAction, builder::TypedValueParser};
#[derive(Debug, Parser)]
struct Cli {
#[arg(short, long, action = ArgAction::Count, value_parser(
clap::value_parser!(u8)
.map(|v| match v {
0 => Verbosity::Low,
1 => Verbosity::Medium,
_ => Verbosity::High,
})
))]
verbose: Verbosity,
}
#[derive(Debug, Clone)]
enum Verbosity {
Low,
Medium,
High,
}
fn main() {
dbg!(Cli::parse());
}
But this panics at runtime:
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `u8`,
right: `clap_question::Verbosity`: Argument `verbose`'s selected action Count contradicts `value_parser` (ValueParser::other(clap_question::Verbosity))
Is there any way to make this work?