Skip to content

Feature Request: useDefault, if implemented #142

Closed

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.

Activity

  1. added
    enhancement
    on Oct 12, 2018
  2. TeXitoi commented on Oct 12, 2018

    @TeXitoi
    Owner

    Seems interesting and cleanly designed.

  3. TeXitoi commented on Oct 12, 2018

    @TeXitoi
    Owner

    This is similar to #105

  4. Lucretiel commented on Oct 12, 2018

    @Lucretiel
    Author

    Can you assign this to me, so that it shows up in my dashboard?

  5. TeXitoi commented on Oct 12, 2018

    @TeXitoi
    Owner

    Done

  6. TeXitoi commented on Oct 15, 2018

    @TeXitoi
    Owner

    So what we do for the fact that clap want to print the default value?

  7. added
    need-design
    on Nov 1, 2018
  8. Lucretiel commented on Nov 2, 2018

    @Lucretiel
    Author

    I'm not super familiar with this part of clap's design, but I figured we'd just instantiate a default value and print that. If there's a way to detect when clap is printing usage text, we can do it manually by appending the default value message to the usage for that field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementneed-design

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions