Skip to content

Commit

Permalink
fix missing optional values (v3-compatible version; feature?)
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed Oct 30, 2019
1 parent 4fdca7b commit 4566667
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/build/arg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ pub struct Arg<'help> {
#[doc(hidden)]
pub default_vals_ifs: Option<VecMap<(Id, Option<&'help OsStr>, &'help OsStr)>>,
#[doc(hidden)]
pub default_missing_value: Option<&'help OsStr>,
#[doc(hidden)]
pub env: Option<(&'help OsStr, Option<OsString>)>,
#[doc(hidden)]
pub terminator: Option<&'help str>,
Expand Down Expand Up @@ -208,6 +210,7 @@ impl<'help> Arg<'help> {
"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 @@ -2268,6 +2271,16 @@ impl<'help> Arg<'help> {
self.default_values_os(&[OsStr::from_bytes(val.as_bytes())])
}

/// ... docs ...
pub fn default_missing_value(self, val: &'help str) -> Self {
self.default_missing_value_os(OsStr::from_bytes(val.as_bytes()))
}
/// ... docs ...
pub fn default_missing_value_os(mut self, val: &'help OsStr) -> Self {
self.default_missing_value = Some(val);
self
}

/// 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
Expand Down Expand Up @@ -4218,6 +4231,7 @@ impl<'help> fmt::Debug for Arg<'help> {
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,
Expand Down Expand Up @@ -4247,7 +4261,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_value
)
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,15 @@ where
));
} else {
sdebugln!("None");
if needs_eq && min_vals_zero {
// OPTION with missing optional value?
let default_missing_value = opt.default_missing_value;
debugln!("default_missing_value = {:?}", default_missing_value);
if let Some(ref value) = default_missing_value {
debugln!("setting value from default_missing_value = {:?}", value);
self.add_val_to_arg(opt, value, matcher)?;
};
}
}

matcher.inc_occurrence_of(opt.id);
Expand Down

0 comments on commit 4566667

Please sign in to comment.