clap
clap copied to clipboard
clap_derive: Merge/inherit `ValueEnum` (sharing enum variants in `ValueEnum`)
Please complete the following tasks
- [X] I have searched the discussions
- [X] I have searched the open and rejected issues
Clap Version
3.1.18
Describe your use case
I have multiple flags that use various value_enum
. These value_enum
s share some of their variants.
Describe the solution you'd like
Allow including one value_enum
in the other. Like so:
#[derive(Clone, ValueEnum)]
enum Serde {
Json,
Yaml,
Toml,
}
#[derive(Clone, ValueEnum)]
enum SerdePlain {
Plain,
#[clap(flatten)]
Serde(Serde),
}
Alternatives, if applicable
No response
Additional Context
No response
The main limitation is the current trait design
pub trait ValueEnum: Sized + Clone {
fn value_variants<'a>() -> &'a [Self];
fn from_str(input: &str, ignore_case: bool) -> Result<Self, String> {
...
}
fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>>;
}
value_variants
requires a fixed size at the moment.
#2799 discussed a different part of the API but had a similar aspect of "should we allocate as part of value_variants
?".
If we could support impl Trait
in return position in traits, then we could have value_variants
return that and chain the flatten calls together.