-
Notifications
You must be signed in to change notification settings - Fork 152
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_value from lazy Default #150
Comments
See #142. You give me a possible solution: supporting arg less default_value when the type implement Default and ToString. You need only one lazy_static, doing |
I did a slightly larger workaround because, for example, using SocketAddr:: default would prevent different socket addresses of having different default values. Their default value would instead come from the parent/grandparent/etc (in the wrapping sense) types. |
Your proposed design have problematic limitation:
And then, structopt has no way to know if a type implement Default, thus you'll need extra annotation to use the default implementation of the config. What is your intend? Having the ability to know the default value of a field? Skip the lazy_static usage? |
I'm 100% agree with this design, can I get to implementing it? This will also fix #142 |
OK. |
Thanks for the implementation, @CreepySkeleton ! @TeXitoi I noticed I didn't reply your question, my apologies.
My intent was related to the situation (at that time): I'd have multiple So my workaround back then was to have a I must say that I currently don't continue working on that same project, so it happens that I don't apply this workaround anymore; and I'm also happy with the resolution that you guys designed and implemented! edit: I actually suspect that it would be possible to have a different newtype wrapper for each of the edit2: another solution - but this would require further design/implementation - would be to not necessarily use de constructor |
You're welcome!
Hmm, Could you please clarify what it would look like? Ideally, show us some pseudocode, an example of such
For now, I would like to push #314 over the finish line, make a new patch release, and then move to |
@CreepySkeleton sure! something like: #[derive(Clone, Debug, StructOpt)]
pub struct Config {
#[structopt(default_value_fn = "fn1")]
pub bind_addr: SocketAddr,
#[structopt(default_value_fn = "fn2")]
pub bind_addr2: SocketAddr,
#[structopt(default_value_fn = "Default::default")]
pub bind_addr3: SocketAddr,
}
fn fn1() -> SocketAddr { FromStr::from_str("127.0.0.1:8080").unwrap() }
fn fn2() -> SocketAddr { FromStr::from_str("127.0.0.1:8081").unwrap() } This comes from the notion that one could reference function names, like how |
@swfsql I see, so it's about being able to
I agree this may be worth implementing, but I'd like to postpone it until after the release of
Quite frankly, we could implement virtually (almost) any syntax here, not just
|
Yup! And that macro syntax is news for me
Good luck with future work!
…On Mon, Dec 30, 2019, 16:17 CreepySkeleton ***@***.***> wrote:
@swfsql <https://github.com/swfsql> I see, so it's about being able to
- skip explicit .to_string() call
- do away with requirement for the Default implementation
- probably skip one extra FromStr parsing call.
I agree this may be worth implementing, but I'd like to postpone it until
after the release of clap_derive.
This comes from the notion that one could reference function names, like
how #[serde(rename(serialize = "ser_name"))] behaves; and then, the Self
type resulting from such function only would have to implement ToString.
Quite frankly, we could implement virtually (almost) any syntax here, not
just #[structopt(method = "lit_str")]. Personally, I would go for #[structopt(default
= path::to::func)].
serde uses string literals because, at the time it was being implemented,
only string literals were allowed after the = sign. We are free from such
a restriction nowadays.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#150?email_source=notifications&email_token=AAI4SCLTXRQUQHGZVMYQJV3Q3I3LHA5CNFSM4GGF77YKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH24DCY#issuecomment-569753995>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAI4SCNX4IT7WUD4CSM4EDTQ3I3LHANCNFSM4GGF77YA>
.
|
(Hello, I'm still not sure if this should be on
structopt
orclap-rs
..),This is a feature request so
default_value
may be used without any value (like short/long), and be lazily derived from Default itself.Quick example:
from this:
into this:
Sorry in advance if this is actually a bad design choice (then I should have no intention of asking for such feature), and that I can't really point into any macro derivation specifics. It's magic !!
The text was updated successfully, but these errors were encountered: