Skip to content

Allow argument-less default_missing_value attribute #6229

@flying-sheep

Description

@flying-sheep

Please complete the following tasks

Clap Version

4.5.54

Describe your use case

I don’t like that I have to repeat myself here:

#[derive(Debug, Parser)]
struct Args {
    #[arg(long, num_args = ..=1, require_equals = true, default_missing_value = "the-default")]
    some_opt: Option<ThingImplementingDefault>,
}

#[derive(Debug, Default, Clone, clap::ValueEnum)]
enum ThingImplementingDefault {
    #[default]
    TheDefault,
    SomethingElse,
}

Describe the solution you'd like

Would be great if Optional<ThingImplementingDefault> could be made to behave like this without repetition:

  • nothing -> None
  • --some-opt -> Some(Default::default())
  • --some-opt=something-else -> Some(value.parse()?)

Maybe just default_missing_value without argument would behave like this?

Alternatives, if applicable

Implement default_missing_value_t, then I could do default_missing_value_t = Some(Default::default())

Additional Context

See also #6109 for more default_missing_value utility

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-deriveArea: #[derive]` macro APIC-enhancementCategory: Raise on the bar on expectationsS-blockedStatus: Blocked on something else such as an RFC or other implementation work.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions