2161: Fix parser skipping options without heading set r=pksunkara a=CertainLach



Co-authored-by: Yaroslav Bolyukin <iam@lach.pw>
This commit is contained in:
bors[bot] 2020-10-11 08:55:33 +00:00 committed by GitHub
commit 5a1a209965
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 7 deletions

View file

@ -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<Item = &Arg<'help>> {
/// Iterate through the *flags*.
pub fn get_flags(&self) -> impl Iterator<Item = &Arg<'help>> {
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<Item = &Arg<'help>> {
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<Item = &Arg<'help>> {
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<Item = &Arg<'help>> {
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.

View file

@ -1560,7 +1560,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)?;
}

View file

@ -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())
}