argh icon indicating copy to clipboard operation
argh copied to clipboard

Use the Default trait for enums used as options

Open jRimbault opened this issue 5 years ago • 2 comments

It should be possible to use an enum as the possible value of an option, without wrapping it in an Option when and if the enum implements Default.

Current example :

#[derive(argh::FromArgs)]
struct Args {
    /// output format
    #[argh(option)]
    format: Option<OutputFormat>,
}

let args: Args = argh::from_env();
let format = args.format.unwrap_or_default();

Using the Default trait :

#[derive(argh::FromArgs)]
struct Args {
    /// output format
    #[argh(option)]
    format: OutputFormat, // enum should implement Default
}

Case in my own code, just for context.

jRimbault avatar Jul 09 '20 18:07 jRimbault

I would definitely accept a PR for this, I've thought this in the past as well

benbrittain avatar Jul 09 '20 20:07 benbrittain

I've yet to learn how to write derive macros. I have time these days, maybe I'll give it shot.

jRimbault avatar Jul 09 '20 22:07 jRimbault