Commit graph

9 commits

Author SHA1 Message Date
Ed Page
9a180c1511 feat(derive): Specify defaults by native expressions
Right now
- `default_value="something"` is a raw method
- `default_value` uses native types

This commit splits the meanings
- `default_value="something"` is a raw method
- `default_value_t` uses `T::default()`
- `default_value_t=expr` uses an expression that evaluates to `T`

This is meant to mirror the `value_of` / `value_of_t` API.

At the moment, this is limited to `T: Display` to work with clap's
default system.  Something we can look at in the future is a way to
loosen that restriction.  One quick win is to specialize when `arg_enum`
is set.  The main downside is complicating the processing of attributes
because it then means we need some processed before others.

Since this builds on `clap`s existing default system, this also means
users do not get any performance gains out of using `default_value_t`,
since we still need to parse it but we also need to convert it to a
string.

Fixes #1694
2021-08-13 12:20:54 -05:00
Ed Page
d643fb3c32 feat(derive): Make it possible to use ArgEnum with default_value
This doesn't solve the problem end-to-end but at least makes it possible
to solve by the user and improve in the future.
2021-07-26 10:33:14 -05:00
Ed Page
c1e272ea6c feat(derive): Allow skipping ArgEnum variants
Depends on #2590
Fixes #2592
2021-07-16 17:04:20 -05:00
Ed Page
6cc76e7237 fix(derive): Clap should not derive ArgEnum
While having convinience derives can be helpful, deriving traits that
are not used in similar situations (`Clap` and `ArgEnum`) can make
things harder
- From a user, derives are opaque and create uncertainty on how to use
  the API if not kept crystal clear (deriving a name gives you the trait
  by that name)
- This makes documentation harder to write and read
- You can use types in unintended places, which is made worse for crate
  APIs because changing this breaks compatibility.

Fixes #2584
2021-07-14 10:50:26 -05:00
Eric Langlois
c20acaf2d4 Derived ArgEnum::from_str returns Err instead of panicking
ArgEnum is public so it is possible for users to call it with
a non-variant input string.
2021-06-08 16:40:12 -04:00
Donough Liu
6968f340c3 Fix clippy warnings 2021-01-19 02:24:58 +08:00
Pavan Kumar Sunkara
7616a5fa2e Implemented arg_enum for option and vec 2020-04-28 11:35:47 +02:00
Pavan Kumar Sunkara
c4c6c35da7 Added alias support for arg_enum 2020-04-22 14:38:09 +02:00
Pavan Kumar Sunkara
01c179f527 Added arg_enum support 2020-04-22 14:37:45 +02:00