Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(help): ArgsRequiredElseHelp should ignore defaults #3421

Merged
merged 1 commit into from Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/parse/arg_matcher.rs
Expand Up @@ -101,10 +101,6 @@ impl ArgMatcher {
self.0.args.contains_key(arg)
}

pub(crate) fn is_empty(&self) -> bool {
self.0.args.is_empty()
}

pub(crate) fn arg_names(&self) -> indexmap::map::Keys<Id, MatchedArg> {
self.0.args.keys()
}
Expand Down
6 changes: 5 additions & 1 deletion src/parse/validator.rs
Expand Up @@ -54,7 +54,11 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
}
}

if matcher.is_empty()
let num_user_values = matcher
.arg_names()
.filter(|arg_id| matcher.check_explicit(arg_id, ArgPredicate::IsPresent))
.count();
if num_user_values == 0
&& matcher.subcommand_name().is_none()
&& self.p.is_set(AS::ArgRequiredElseHelp)
{
Expand Down
15 changes: 15 additions & 0 deletions tests/builder/app_settings.rs
Expand Up @@ -259,6 +259,21 @@ fn arg_required_else_help_over_reqs() {
);
}

#[test]
fn arg_required_else_help_with_default() {
let result = App::new("arg_required")
.setting(AppSettings::ArgRequiredElseHelp)
.arg(arg!(--input <PATH>).required(false).default_value("-"))
.try_get_matches_from(vec![""]);

assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(
err.kind(),
ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
);
}

#[test]
fn arg_required_else_help_error_message() {
let app = App::new("test")
Expand Down