Skip to content

Commit

Permalink
add 'default_missing_value' configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed May 20, 2020
1 parent 010898c commit c37ae4a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/build/arg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub struct Arg<'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_missing_vals: Vec<&'help OsStr>,
pub(crate) env: Option<(&'help OsStr, Option<OsString>)>,
pub(crate) terminator: Option<&'help str>,
pub(crate) index: Option<u64>,
Expand Down Expand Up @@ -2299,6 +2300,32 @@ impl<'help> Arg<'help> {
self
}


/// ... docs ...
#[inline]
pub fn default_missing_value(self, val: &'help str) -> Self {
self.default_missing_values_os(&[OsStr::new(val)])
}
/// ... docs ...
#[inline]
pub fn default_missing_value_os(self, val: &'help OsStr) -> Self {
self.default_missing_values_os(&[val])
}
/// ... docs ...
#[inline]
pub fn default_missing_values(self, vals: &[&'help str]) -> Self {
let vals_vec: Vec<_> = vals.iter().map(|val| OsStr::new(*val)).collect();
self.default_missing_values_os(&vals_vec[..])
}
/// ... docs ...
#[inline]
pub fn default_missing_values_os(mut self, vals: &[&'help OsStr]) -> Self {
self.set_mut(ArgSettings::TakesValue);
self.default_missing_vals = vals.to_vec();
self
}


/// Specifies the value of the argument if `arg` has been used at runtime. If `val` is set to
/// `None`, `arg` only needs to be present. If `val` is set to `"some-val"` then `arg` must be
/// present at runtime **and** have the value `val`.
Expand Down Expand Up @@ -4174,6 +4201,7 @@ impl<'a> From<&'a Yaml> for Arg<'a> {
"default_value" => yaml_to_str!(a, v, default_value),
"default_value_if" => yaml_tuple3!(a, v, default_value_if),
"default_value_ifs" => yaml_tuple3!(a, v, default_value_if),
"default_missing_value" => yaml_to_str!(a, v, default_missing_value),
"env" => yaml_to_str!(a, v, env),
"value_names" => yaml_vec_or_str!(v, a, value_name),
"groups" => yaml_vec_or_str!(v, a, group),
Expand Down Expand Up @@ -4341,7 +4369,8 @@ impl<'help> fmt::Debug for Arg<'help> {
number_of_values: {:?}, max_values: {:?}, min_values: {:?}, value_delimiter: {:?}, \
default_value_ifs: {:?}, value_terminator: {:?}, display_order: {:?}, env: {:?}, \
unified_ord: {:?}, default_value: {:?}, validator: {}, validator_os: {} \
}}",
default_missing_value: {:?}, \
}}",
self.id,
self.name,
self.about,
Expand Down Expand Up @@ -4371,7 +4400,8 @@ impl<'help> fmt::Debug for Arg<'help> {
self.unified_ord,
self.default_vals,
self.validator.as_ref().map_or("None", |_| "Some(Fn)"),
self.validator_os.as_ref().map_or("None", |_| "Some(Fn)")
self.validator_os.as_ref().map_or("None", |_| "Some(Fn)"),
self.default_missing_vals
)
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,17 @@ where
)?);
} else {
debug!("None");
if needs_eq && min_vals_zero {
// OPTION with missing optional value?
if !opt.default_missing_vals.is_empty() {
// debug!("Parser::add_value:iter:{}: has default vals", arg.name);
debug!("Has default_missing_vals");
for val in &opt.default_missing_vals {
debug!("Adding value from default_missing_values; val = {:?}", val);
self.add_val_to_arg(opt, &ArgStr::new(val), matcher, ValueType::CommandLine)?; // ? ValueType::DefaultValue
}
};
}
}

matcher.inc_occurrence_of(&opt.id);
Expand Down

0 comments on commit c37ae4a

Please sign in to comment.