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

unset default value if some other Arg is present (or has a specific value) #1406

Closed
delan opened this issue Jan 27, 2019 · 5 comments · Fixed by #2400
Closed

unset default value if some other Arg is present (or has a specific value) #1406

delan opened this issue Jan 27, 2019 · 5 comments · Fixed by #2400
Milestone

Comments

@delan
Copy link

delan commented Jan 27, 2019

Rust Version

rustc 1.32.0 (9fda7c223 2019-01-16)

Affected Version of clap

2.32.0

Feature Request Summary

Some way to unset the default value for an Arg based on the presence or value of some other Arg. One alternative the current API allows is to pass the empty string (or some other value that means nothing) to Arg::default_value_if (at the expense of additional code to treat Some("") like None):

.default_value_if("other", None, "").default_value("foo")

The signature I’m imagining looks something like:

fn no_default_value_if(self, arg: &'a str, val: Option<&'b str>) -> Self
@delan
Copy link
Author

delan commented Jan 27, 2019

@Moongoodboy-K suggests default_value_unless over no_default_value_if

@pksunkara pksunkara added this to the 3.1 milestone Apr 9, 2020
@patrickelectric
Copy link

That's a really important feature!

@stevenengler
Copy link
Contributor

Instead of creating a new method, could the signature of default_value_if() be changed in version 3 to support an Option<&str> for the default rather than just a &str? This way the default can be unset without needing to add more functions to the API.

@pksunkara pksunkara modified the milestones: 3.1, 3.0 Jan 3, 2021
@delan
Copy link
Author

delan commented Jan 4, 2021

Instead of creating a new method, could the signature of default_value_if() be changed in version 3 to support an Option<&str> for the default rather than just a &str?

Good idea (fwiw). The parameter type could even change without adding to the version 3 migration burden with impl Into<Option<&str>>, which allows you to continue passing &str values without wrapping them in Some.

@pksunkara
Copy link
Member

@logansquirel And then you can take this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants