-
-
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
Support unsetting/removing the default if an option/flag is set #2296
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,7 +93,7 @@ pub struct Arg<'help> { | |
pub(crate) validator_os: Option<Arc<Mutex<ValidatorOs<'help>>>>, | ||
pub(crate) val_delim: Option<char>, | ||
pub(crate) default_vals: Vec<&'help OsStr>, | ||
pub(crate) default_vals_ifs: VecMap<(Id, Option<&'help OsStr>, &'help OsStr)>, | ||
pub(crate) default_vals_ifs: VecMap<(Id, Option<&'help OsStr>, Option<&'help OsStr>)>, | ||
pub(crate) default_missing_vals: Vec<&'help OsStr>, | ||
pub(crate) env: Option<(&'help OsStr, Option<OsString>)>, | ||
pub(crate) terminator: Option<&'help str>, | ||
|
@@ -2527,7 +2527,7 @@ impl<'help> Arg<'help> { | |
/// .long("flag")) | ||
/// .arg(Arg::new("other") | ||
/// .long("other") | ||
/// .default_value_if("flag", None, "default")) | ||
/// .default_value_if("flag", None, Some("default"))) | ||
/// .get_matches_from(vec![ | ||
/// "prog", "--flag" | ||
/// ]); | ||
|
@@ -2544,7 +2544,7 @@ impl<'help> Arg<'help> { | |
/// .long("flag")) | ||
/// .arg(Arg::new("other") | ||
/// .long("other") | ||
/// .default_value_if("flag", None, "default")) | ||
/// .default_value_if("flag", None, Some("default"))) | ||
/// .get_matches_from(vec![ | ||
/// "prog" | ||
/// ]); | ||
|
@@ -2562,7 +2562,7 @@ impl<'help> Arg<'help> { | |
/// .long("opt")) | ||
/// .arg(Arg::new("other") | ||
/// .long("other") | ||
/// .default_value_if("opt", Some("special"), "default")) | ||
/// .default_value_if("opt", Some("special"), Some("default"))) | ||
/// .get_matches_from(vec![ | ||
/// "prog", "--opt", "special" | ||
/// ]); | ||
|
@@ -2581,38 +2581,60 @@ impl<'help> Arg<'help> { | |
/// .long("opt")) | ||
/// .arg(Arg::new("other") | ||
/// .long("other") | ||
/// .default_value_if("opt", Some("special"), "default")) | ||
/// .default_value_if("opt", Some("special"), Some("default"))) | ||
/// .get_matches_from(vec![ | ||
/// "prog", "--opt", "hahaha" | ||
/// ]); | ||
/// | ||
/// assert_eq!(m.value_of("other"), None); | ||
/// ``` | ||
/// | ||
/// We can also remove a default if the flag was set by setting `default` to `None`. | ||
/// | ||
/// ```rust | ||
/// # use clap::{App, Arg}; | ||
/// let m = App::new("prog") | ||
/// .arg(Arg::new("flag") | ||
/// .long("flag")) | ||
/// .arg(Arg::new("other") | ||
/// .long("other") | ||
/// .default_value("default") | ||
/// .default_value_if("flag", None, None)) | ||
/// .get_matches_from(vec![ | ||
/// "prog", "--flag" | ||
/// ]); | ||
/// | ||
/// assert_eq!(m.value_of("other"), None); | ||
/// ``` | ||
/// [`Arg::takes_value(true)`]: ./struct.Arg.html#method.takes_value | ||
/// [`Arg::default_value`]: ./struct.Arg.html#method.default_value | ||
pub fn default_value_if<T: Key>( | ||
pub fn default_value_if<T: Key, S: Into<Option<&'help str>>>( | ||
self, | ||
arg_id: T, | ||
val: Option<&'help str>, | ||
default: &'help str, | ||
default: S, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type param should be |
||
) -> Self { | ||
self.default_value_if_os(arg_id, val.map(OsStr::new), OsStr::new(default)) | ||
self.default_value_if_os( | ||
arg_id, | ||
val.map(OsStr::new), | ||
default.into().map(|s| OsStr::new(s)), | ||
) | ||
} | ||
|
||
/// Provides a conditional default value in the exact same manner as [`Arg::default_value_if`] | ||
/// only using [`OsStr`]s instead. | ||
/// | ||
/// [`Arg::default_value_if`]: ./struct.Arg.html#method.default_value_if | ||
/// [`OsStr`]: https://doc.rust-lang.org/std/ffi/struct.OsStr.html | ||
pub fn default_value_if_os<T: Key>( | ||
pub fn default_value_if_os<T: Key, S: Into<Option<&'help OsStr>>>( | ||
mut self, | ||
arg_id: T, | ||
val: Option<&'help OsStr>, | ||
default: &'help OsStr, | ||
default: S, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type params should be |
||
) -> Self { | ||
let l = self.default_vals_ifs.len(); | ||
self.default_vals_ifs | ||
.insert(l, (arg_id.into(), val, default)); | ||
.insert(l, (arg_id.into(), val, default.into())); | ||
pksunkara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.takes_value(true) | ||
} | ||
|
||
|
@@ -2700,12 +2722,14 @@ impl<'help> Arg<'help> { | |
/// ``` | ||
/// [`Arg::takes_value(true)`]: ./struct.Arg.html#method.takes_value | ||
/// [`Arg::default_value_if`]: ./struct.Arg.html#method.default_value_if | ||
pub fn default_value_ifs<T: Key>( | ||
pub fn default_value_ifs<T: Key, S: Into<Option<&'help str>> + Copy>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function needs documentation addition. |
||
mut self, | ||
ifs: &[(T, Option<&'help str>, &'help str)], | ||
ifs: &[(T, Option<&'help str>, S)], | ||
) -> Self { | ||
for (arg, val, default) in ifs { | ||
self = self.default_value_if_os(arg, val.map(OsStr::new), OsStr::new(*default)); | ||
let default: Option<&'help str> = (*default).into(); | ||
self = | ||
self.default_value_if_os(arg, val.map(OsStr::new), default.map(|s| OsStr::new(s))); | ||
} | ||
self | ||
} | ||
|
@@ -2715,11 +2739,12 @@ impl<'help> Arg<'help> { | |
/// | ||
/// [`Arg::default_value_ifs`]: ./struct.Arg.html#method.default_value_ifs | ||
/// [`OsStr`]: https://doc.rust-lang.org/std/ffi/struct.OsStr.html | ||
pub fn default_value_ifs_os<T: Key>( | ||
pub fn default_value_ifs_os<T: Key, S: Into<Option<&'help OsStr>> + Copy>( | ||
mut self, | ||
ifs: &[(T, Option<&'help OsStr>, &'help OsStr)], | ||
ifs: &[(T, Option<&'help OsStr>, S)], | ||
) -> Self { | ||
for (arg, val, default) in ifs { | ||
let default: Option<&'help OsStr> = (*default).into(); | ||
self = self.default_value_if_os(arg.key(), *val, default); | ||
} | ||
self | ||
|
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.
This needs better phrasing. It does not explain things properly as it is right now.