Provide default_values mimicking default_value

This commit is contained in:
Thiago Arrais 2019-10-10 13:48:22 -03:00 committed by Kevin K
parent fed3a8fc29
commit c3b7c01f54
No known key found for this signature in database
GPG key ID: 17218E4B3692F01A
4 changed files with 78 additions and 17 deletions

View file

@ -101,7 +101,7 @@ pub struct Arg<'help> {
#[doc(hidden)]
pub val_delim: Option<char>,
#[doc(hidden)]
pub default_val: Option<&'help OsStr>,
pub default_vals: Option<Vec<&'help OsStr>>,
#[doc(hidden)]
pub default_vals_ifs: Option<VecMap<(Id, Option<&'help OsStr>, &'help OsStr)>>,
#[doc(hidden)]
@ -2265,16 +2265,34 @@ impl<'help> Arg<'help> {
/// [`ArgMatches::is_present`]: ./struct.ArgMatches.html#method.is_present
/// [`Arg::default_value_if`]: ./struct.Arg.html#method.default_value_if
pub fn default_value(self, val: &'help str) -> Self {
self.default_value_os(OsStr::from_bytes(val.as_bytes()))
self.default_values_os(&[OsStr::from_bytes(val.as_bytes())])
}
/// Provides a default value in the exact same manner as [`Arg::default_value`]
/// only using [`OsStr`]s instead.
/// [`Arg::default_value`]: ./struct.Arg.html#method.default_value
/// [`OsStr`]: https://doc.rust-lang.org/std/ffi/struct.OsStr.html
pub fn default_value_os(mut self, val: &'help OsStr) -> Self {
pub fn default_value_os(self, val: &'help OsStr) -> Self {
self.default_values_os(&[val])
}
/// Like [`Arg::default_value'] but for args taking multiple values
/// [`Arg::default_value`]: ./struct.Arg.html#method.default_value
pub fn default_values(self, vals: &[&'help str]) -> Self {
let vals_vec: Vec<_> = vals
.iter()
.map(|val| OsStr::from_bytes(val.as_bytes()))
.collect();
self.default_values_os(&vals_vec[..])
}
/// Provides default values in the exact same manner as [`Arg::default_values`]
/// only using [`OsStr`]s instead.
/// [`Arg::default_values`]: ./struct.Arg.html#method.default_values
/// [`OsStr`]: https://doc.rust-lang.org/std/ffi/struct.OsStr.html
pub fn default_values_os(mut self, vals: &[&'help OsStr]) -> Self {
self.setb(ArgSettings::TakesValue);
self.default_val = Some(val);
self.default_vals = Some(vals.to_vec());
self
}
@ -4227,7 +4245,7 @@ impl<'help> fmt::Debug for Arg<'help> {
self.disp_ord,
self.env,
self.unified_ord,
self.default_val,
self.default_vals,
self.validator.as_ref().map_or("None", |_| "Some(Fn)"),
self.validator_os.as_ref().map_or("None", |_| "Some(Fn)")
)

View file

@ -454,16 +454,23 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
spec_vals.push(env_info);
}
if !a.is_set(ArgSettings::HideDefaultValue) {
if let Some(pv) = a.default_val {
if let Some(ref pv) = a.default_vals {
debugln!("Help::spec_vals: Found default value...[{:?}]", pv);
spec_vals.push(format!(
" [default: {}]",
if self.color {
self.cizer.good(pv.to_string_lossy())
} else {
Format::None(pv.to_string_lossy())
}
));
let pvs = if self.color {
pv
.iter()
.map(|&pvs| format!("{}", self.cizer.good(pvs.to_string_lossy())))
.collect::<Vec<_>>()
.join(" ")
} else {
pv
.iter()
.map(|&pvs| format!("{}", Format::None(pvs.to_string_lossy())))
.collect::<Vec<_>>()
.join(" ")
};
spec_vals.push(format!(" [default: {}]", pvs));
}
}
if let Some(ref aliases) = a.aliases {

View file

@ -1361,7 +1361,7 @@ where
debugln!("Parser::add_defaults;");
macro_rules! add_val {
(@default $_self:ident, $a:ident, $m:ident) => {
if let Some(ref val) = $a.default_val {
if let Some(ref vals) = $a.default_vals {
debugln!("Parser::add_defaults:iter:{}: has default vals", $a.name);
if $m
.get($a.id)
@ -1373,7 +1373,9 @@ where
"Parser::add_defaults:iter:{}: has no user defined vals",
$a.name
);
$_self.add_val_to_arg($a, OsStr::new(val), $m)?;
for val in vals {
$_self.add_val_to_arg($a, val, $m)?;
}
} else if $m.get($a.id).is_some() {
debugln!(
"Parser::add_defaults:iter:{}: has user defined vals",
@ -1382,7 +1384,9 @@ where
} else {
debugln!("Parser::add_defaults:iter:{}: wasn't used", $a.name);
$_self.add_val_to_arg($a, OsStr::new(val), $m)?;
for val in vals {
$_self.add_val_to_arg($a, val, $m)?;
}
}
} else {
debugln!(

View file

@ -447,6 +447,38 @@ fn conditional_reqs_pass() {
assert_eq!(m.value_of("input"), Some("some"));
}
#[test]
fn multiple_defaults() {
let r = App::new("diff")
.arg(
Arg::with_name("files")
.long("files")
.number_of_values(2)
.default_values(&["old", "new"]),
)
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("files"));
assert_eq!(m.values_of_lossy("files").unwrap(), vec!["old", "new"]);
}
#[test]
fn multiple_defaults_override() {
let r = App::new("diff")
.arg(
Arg::with_name("files")
.long("files")
.number_of_values(2)
.default_values(&["old", "new"]),
)
.try_get_matches_from(vec!["", "--files", "other", "mine"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("files"));
assert_eq!(m.values_of_lossy("files").unwrap(), vec!["other", "mine"]);
}
#[test]
fn issue_1050_num_vals_and_defaults() {
let res = App::new("hello")