I have the following type:
#[derive(clap::ValueEnum, Clone, Debug)]
pub enum Processor {
DefaultProcessor,
SecondaryProcessor,
}
I have a FromStr impl for this struct as well (not shown because it's very simple).
I am currently using this in a struct like this:
#[derive(Parser)]
pub struct RunLocalTestnet {
/// Processors to run.
#[clap(long)]
processors: Vec<Processor>,
}
So far so good, this works great. What I'm trying to do now is add a default value for this vector, for example:
#[clap(long, default_value_t = vec![Processor::DefaultProcessor])]
processors: Vec<Processor>,
Unfortunately when I do this I get the following error:
error[E0277]: `Vec<processor::processors::Processor>` doesn't implement `std::fmt::Display`
--> crates/aptos/src/node/local_testnet.rs:103:18
|
103 | #[clap(long, default_value_t = vec![Processor::DefaultProcessor])]
| ^^^^^^^^^^^^^^^ `Vec<processor::processors::Processor>` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Vec<processor::processors::Processor>`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required for `Vec<processor::processors::Processor>` to implement `ToString`
Is there a way to make this work with Clap 4 (specifically 4.3.9) out of the box? I feel like it shouldn't be necessary to make Processor impl Display because it works up until I set the default value.
I believe you want
default_values_t(note thes).default_value_trequires the type to implementDisplayorValueEnumwhich of courseVec<T>does not. Butdefault_values_trequires the type to be aVec<T>where onlyThas to implementDisplayorValueEnum, which is exactly what you have.Docs page: https://docs.rs/clap/4.3.9/clap/_derive/index.html
default_value = <str>:Arg::default_valueandArg::required(false)default_value_t [= <expr>]:Arg::default_valueandArg::required(false)std::fmt::Displaythat roundtrips correctly with theArg::value_parseror#[arg(value_enum)]<expr>, relies onDefault::default()default_values_t = <expr>:Arg::default_valuesandArg::required(false)Vec<T>andTto implementstd::fmt::Displayor#[arg(value_enum)]<expr>must implementIntoIterator<T>