clap icon indicating copy to clipboard operation
clap copied to clipboard

clap_derive: Merge/inherit `ValueEnum` (sharing enum variants in `ValueEnum`)

Open KSXGitHub opened this issue 2 years ago • 1 comments

Please complete the following tasks

Clap Version

3.1.18

Describe your use case

I have multiple flags that use various value_enum. These value_enums 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

KSXGitHub avatar Jul 12 '22 14:07 KSXGitHub

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.

epage avatar Jul 12 '22 14:07 epage