From 90dde89ec44a3320b0d5da3c04eb0114bcaef548 Mon Sep 17 00:00:00 2001 From: patrick-gu <55641350+patrick-gu@users.noreply.github.com> Date: Sat, 18 Sep 2021 15:15:25 -0700 Subject: [PATCH 1/2] Apply `value_delimiter` to default values --- src/parse/parser.rs | 19 +++++++++++++++++-- tests/default_vals.rs | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 5135fbad..572045bf 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -1685,6 +1685,21 @@ impl<'help, 'app> Parser<'help, 'app> { debug!("Parser::add_value: doesn't have conditional defaults"); } + fn process_default_vals(arg: &Arg<'_>, default_vals: &[&OsStr]) -> Vec { + if let Some(delim) = arg.val_delim { + let mut vals = vec![]; + for val in default_vals { + let val = RawOsStr::new(val); + for val in val.split(delim) { + vals.push(val.to_os_str().into_owned()); + } + } + vals + } else { + default_vals.iter().map(OsString::from).collect() + } + } + if !arg.default_vals.is_empty() { debug!("Parser::add_value:iter:{}: has default vals", arg.name); if matcher.get(&arg.id).is_some() { @@ -1695,7 +1710,7 @@ impl<'help, 'app> Parser<'help, 'app> { self.add_multiple_vals_to_arg( arg, - arg.default_vals.iter().map(OsString::from), + process_default_vals(arg, &arg.default_vals).into_iter(), matcher, ty, false, @@ -1723,7 +1738,7 @@ impl<'help, 'app> Parser<'help, 'app> { ); self.add_multiple_vals_to_arg( arg, - arg.default_missing_vals.iter().map(OsString::from), + process_default_vals(arg, &arg.default_missing_vals).into_iter(), matcher, ty, false, diff --git a/tests/default_vals.rs b/tests/default_vals.rs index aaa9c03f..88feca65 100644 --- a/tests/default_vals.rs +++ b/tests/default_vals.rs @@ -651,3 +651,21 @@ fn required_args_with_default_values() { .arg(Arg::new("arg").required(true).default_value("value")) .try_get_matches(); } + +#[test] +fn with_value_delimiter() { + let app = App::new("multiple_values").arg( + Arg::new("option") + .long("option") + .about("multiple options") + .value_delimiter(';') + .default_value("first;second"), + ); + + let matches = app.get_matches_from(vec![""]); + + assert_eq!( + matches.values_of("option").unwrap().collect::>(), + ["first", "second"] + ); +} From d79c59ab1718d886a8dff7bf799e1cb90b9f3296 Mon Sep 17 00:00:00 2001 From: patrick-gu <55641350+patrick-gu@users.noreply.github.com> Date: Sat, 18 Sep 2021 15:47:03 -0700 Subject: [PATCH 2/2] Add test for `default_missing_values` with `value_delimiter` --- tests/default_vals.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/default_vals.rs b/tests/default_vals.rs index 88feca65..d9fb6b46 100644 --- a/tests/default_vals.rs +++ b/tests/default_vals.rs @@ -669,3 +669,20 @@ fn with_value_delimiter() { ["first", "second"] ); } + +#[test] +fn missing_with_value_delimiter() { + let app = App::new("program").arg( + Arg::new("option") + .long("option") + .value_delimiter(';') + .default_missing_values(&["value1;value2;value3", "value4;value5"]), + ); + + let matches = app.get_matches_from(vec!["program", "--option"]); + + assert_eq!( + matches.values_of("option").unwrap().collect::>(), + ["value1", "value2", "value3", "value4", "value5"] + ); +}