From 659d688543e88f36877e61c052d23958701e953b Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Fri, 9 Oct 2020 23:43:46 +0500 Subject: [PATCH 1/2] test: add failing test for #2022 Signed-off-by: Yaroslav Bolyukin --- tests/opts.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/opts.rs b/tests/opts.rs index 0a9813ac..1c273a77 100644 --- a/tests/opts.rs +++ b/tests/opts.rs @@ -510,3 +510,12 @@ fn long_eq_val_starts_with_eq() { assert_eq!("=value", matches.value_of("opt").unwrap()); } + +#[test] +fn issue_2022_get_flags_misuse() { + let app = App::new("test") + .help_heading("test") + .arg(Arg::new("a").long("a").default_value("32")); + let matches = app.get_matches_from(&[""]); + assert!(matches.value_of("a").is_some()) +} From 6050a1702436188e22392e8780fd3bbf23024546 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Fri, 9 Oct 2020 23:22:51 +0500 Subject: [PATCH 2/2] fix: #2022 Parser was skipping default values for options with headings Signed-off-by: Yaroslav Bolyukin --- src/build/app/mod.rs | 20 ++++++++++++++------ src/parse/parser.rs | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/build/app/mod.rs b/src/build/app/mod.rs index 7b5f83eb..fafce272 100644 --- a/src/build/app/mod.rs +++ b/src/build/app/mod.rs @@ -214,18 +214,26 @@ impl<'help> App<'help> { self.get_arguments().filter(|a| a.is_positional()) } - /// Iterate through the *flags* that don't have custom heading. - pub fn get_flags_with_no_heading(&self) -> impl Iterator> { + /// Iterate through the *flags*. + pub fn get_flags(&self) -> impl Iterator> { self.get_arguments() .filter(|a| !a.is_set(ArgSettings::TakesValue) && a.get_index().is_none()) - .filter(|a| a.get_help_heading().is_none()) + } + + /// Iterate through the *options*. + pub fn get_opts(&self) -> impl Iterator> { + self.get_arguments() + .filter(|a| a.is_set(ArgSettings::TakesValue) && a.get_index().is_none()) + } + + /// Iterate through the *flags* that don't have custom heading. + pub fn get_flags_with_no_heading(&self) -> impl Iterator> { + self.get_flags().filter(|a| a.get_help_heading().is_none()) } /// Iterate through the *options* that don't have custom heading. pub fn get_opts_with_no_heading(&self) -> impl Iterator> { - self.get_arguments() - .filter(|a| a.is_set(ArgSettings::TakesValue) && a.get_index().is_none()) - .filter(|a| a.get_help_heading().is_none()) + self.get_opts().filter(|a| a.get_help_heading().is_none()) } /// Get a list of all arguments the given argument conflicts with. diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 014484ba..be375f1f 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -1562,7 +1562,7 @@ impl<'help, 'app> Parser<'help, 'app> { pub(crate) fn add_defaults(&mut self, matcher: &mut ArgMatcher) -> ClapResult<()> { debug!("Parser::add_defaults"); - for o in self.app.get_opts_with_no_heading() { + for o in self.app.get_opts() { debug!("Parser::add_defaults:iter:{}:", o.name); self.add_value(o, matcher, ValueType::DefaultValue)?; }