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

serde-like default value behavior #3116

Open
epage opened this issue Dec 9, 2021 · 3 comments
Open

serde-like default value behavior #3116

epage opened this issue Dec 9, 2021 · 3 comments
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing

Comments

@epage
Copy link
Member

epage commented Dec 9, 2021

Maintainer's notes

  • We can use the field's Default::default with #[clap(default_value_t)]
  • Design work is needed for using the container's Default::default

Issue by epage
Thursday May 03, 2018 at 13:17 GMT
Originally opened as TeXitoi/structopt#105


It'd be nice to have annotations like serde for default values

  • Use the struct's Default impl
  • Use the field's Default impl
@epage
Copy link
Member Author

epage commented Dec 9, 2021

Comment by TeXitoi
Thursday May 03, 2018 at 13:38 GMT


The problem is that, most of the time, clap will not be able to show the default value (because just a few types implement Display).

@epage
Copy link
Member Author

epage commented Dec 9, 2021

default_value_t allows you to do the type's Default impl or a custom value of the type, so long as the type implements Display.

@epage epage closed this as completed Dec 9, 2021
@epage epage added the A-derive Area: #[derive]` macro API label Dec 9, 2021
@epage
Copy link
Member Author

epage commented Dec 14, 2021

I missed the case of using the container's Default::default. The question is how to specify this. Serde does this by having a container-level default attribute but for us to do that, we would make all fields optional. This would require a weird handling of structs to use flatten to leverage it or a new way to specify it at the field level

@epage epage reopened this Dec 14, 2021
@epage epage added the S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing label Dec 14, 2021
@epage epage added the C-enhancement Category: Raise on the bar on expectations label May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing
Projects
None yet
Development

No branches or pull requests

1 participant