-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat(derive): Specify defaults by native expressions #2635
Conversation
@pksunkara FYI looks like this is another sporadic failure: https://github.com/clap-rs/clap/runs/3183588763?check_suite_focus=true |
@pksunkara CI failure is in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, but we don't really need default_value_t
. Everything can be under default_value
.
clap_derive/tests/default_value.rs
Outdated
#[derive(Clap, PartialEq, Debug)] | ||
struct Opt { | ||
#[clap(default_value)] | ||
#[clap(default_value = "0")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use something other than 0
or 1
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is it you feel missing with using 0/1 that I should cover?
For the raw case, "0" is non-default. 1 would never show up from defaults.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just we are testing for 0
& 1
and I wanted this to be "2"
Could you clarify what you intend for how all of this would work under We need to distinguish intent between a raw default and a typed default, Personally, I'm unsure of doing that based purely on string literal vs not string literal which is my only guess as to what you intend. |
Yup, since these are macros, we can use the freedom of syntax. Let's do
|
|
"raw method" is nomenclature in clap derive that denotes it is simply forwarded to clap.
Hmm.. I do agree on that. But, that is what structopt has been doing and what These attributes will be hardest to migrate for end users, which is why I was suggesting to keep the original Reference on how structopt behaves currently: https://docs.rs/structopt/0.3.22/structopt/#default-values Btw, I removed the underlying issue from 3.0 milestone because I mistakenly tagged it originally. |
My change does not have overloaded intent. As I think we should make this breaking change, I think this is still relevant for 3.0. As I said, this makes clap more self-consistent and removes ambiguity which will help on-boarding. We have to weigh both migration and future adoption. As an end-user, this is relatively easy change to migrate to. |
I am now convinced. Let's remove the special case handling of |
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 clap-rs#1694
PR updates include
|
abort!(name, | ||
"`#[clap(default_value)` attribute (without a value) has been replaced by `#[clap(default_value_t)]`."; | ||
help = "Change the attribute to `#[clap(default_value_t)]`"; | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cargo-up
should be able to handle this easily without us needing the abort. This should be deleted when I finish the structopt to clap upgrader then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally would find this useful because I'm unlikely to download a special tool just to handle a library migration. The likelihood of downloading is proportionate to the number of CLIs I maintain.
Right now
default_value="something"
is a raw methoddefault_value
uses native typesThis commit splits the meanings
default_value="something"
is a raw methoddefault_value_t
usesT::default()
default_value_t=expr
uses an expression that evaluates toT
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'sdefault 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 meansusers 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