Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default in derive API from Default::default() #3720

Closed
2 tasks done
Shatur opened this issue May 11, 2022 · 1 comment
Closed
2 tasks done

Default in derive API from Default::default() #3720

Shatur opened this issue May 11, 2022 · 1 comment
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@Shatur
Copy link

Shatur commented May 11, 2022

Please complete the following tasks

Clap Version

3.1.8

Describe your use case

I have a struct with non-optional values but with defaults. Currently I define it like this:

#[derive(Args, Clone)]
#[cfg_attr(test, derive(PartialEq, Debug))]
pub(crate) struct ServerSettings {
    /// Server name that will be visible to other players.
    #[clap(short, long, default_value_t = ServerSettings::default().server_name)]
    pub(crate) server_name: String,

    /// IP address to bind.
    #[clap(short, long, default_value_t = ServerSettings::default().ip)]
    pub(crate) ip: String,

    /// Port to use.
    #[clap(short, long, default_value_t = ServerSettings::default().port)]
    pub(crate) port: u16,

    /// Game mode.
    #[clap(short, long, default_value_t = ServerSettings::default().game_mode)]
    pub(crate) game_mode: GameMode,

    /// Game map.
    #[clap(short, long, default_value_t = ServerSettings::default().map)]
    pub(crate) map: Map,

    /// Choose heroes randomly.
    #[clap(short, long)]
    pub(crate) random_heroes: bool,
}

Because default_value_t uses Default::default for the underlying type, not from the struct. It's very unergonomic and error-prone.

Describe the solution you'd like

It would be nice to have an attribute to use default value from the struct's Default trait.

Alternatives, if applicable

No response

Additional Context

No response

@Shatur Shatur added the C-enhancement Category: Raise on the bar on expectations label May 11, 2022
@epage
Copy link
Member

epage commented May 11, 2022

Looks like we have #3116 for this, closing in favor if that issue.

@epage epage closed this as completed May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants