Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
KSXGitHub opened this issue Jul 12, 2022 · 1 comment
Open
2 tasks done
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing

Comments

@KSXGitHub
Copy link

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 KSXGitHub added the C-enhancement Category: Raise on the bar on expectations label Jul 12, 2022
@epage epage added A-derive Area: #[derive]` macro API S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing labels Jul 12, 2022
@epage
Copy link
Member

epage commented Jul 12, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing
Projects
None yet
Development

No branches or pull requests

2 participants