clap icon indicating copy to clipboard operation
clap copied to clipboard

`serde`-like default value behavior

Open epage opened this issue 2 years ago • 3 comments

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 https://github.com/TeXitoi/structopt/issues/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 avatar Dec 09 '21 16:12 epage

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 avatar Dec 09 '21 16:12 epage

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 avatar Dec 09 '21 16:12 epage

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 avatar Dec 14 '21 14:12 epage