Skip to content

Fallback for ValueEnum #5885

Open
Open
@ModProg

Description

Please complete the following tasks

Clap Version

4.5.26

Describe your use case

I'd like to have support for a fallback option when using ValueEnum.

Describe the solution you'd like

When derive(ValueEnum) is used I'd like to have a variant e.g., Other(String), annotated with something like #[clap(other|fallback)] to contain the literal value if none of the enum variants matches.

Alternatives, if applicable

Using value_parser to and skip like so:

#[derive(Debug, Parser)]
struct Args {
    #[clap(value_parser = enum_parser)]
    arg: Enum,
}

#[derive(ValueEnum, Clone, Debug)]
enum Enum {
    A,
    B,
    #[clap(skip)]
    Other(String),
}

fn enum_parser(value: &str) -> Result<Enum, Infallible> {
    Ok(Enum::from_str(value, true).unwrap_or_else(|_| Enum::Other(value.to_owned())))
}

Additional Context

No response

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Raise on the bar on expectations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions