Skip to content

Feature Request: useDefault, if implemented #142

@Lucretiel

Description

@Lucretiel

In the same vein as convenience features like #134 and #18, add support for the automatic use of Default, if implemented. This would be a breaking change if done poorly, and in any case I'm not sure if the procedural macro system is powerful enough to "detect" trait implementations. I'd therefore propose the following syntax, inspired by #18:

#[derive(Debug)]
struct Value(String);

impl Default for Value {
    fn default() -> Value {
        Value("default".to_string())
    }
}

impl FromStr for Value { ... }

#[derive(Debug, StructOpt)]
struct Opt {
    // using the proposed automatic name syntax
   #[structopt(long, short, default)]
    value: Value
}

Basically, if the default key is present, then use the Default trait if the flag isn't given. I'd also be okay with default_value being the key, but my suggestion is that it be slightly different because under the hood, something slightly different is happening. This isn't using clap's default_value builder method, since it requires a str. Rather, structopt itself will handle the logic for filling in the default value if necessary.

This could have implications for #123, as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementWe would love to have this feature! Feel free to supply a PRneed-designThe concrete desing is uncertain, please get involved in the discussion before sending a PR

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions