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(error): Be more accurate in smart usage #3390

Merged
merged 1 commit into from Feb 2, 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
22 changes: 15 additions & 7 deletions src/build/app/mod.rs
Expand Up @@ -3238,16 +3238,24 @@ impl<'help> App<'help> {
args
}

pub(crate) fn unroll_requirements_for_arg(&self, arg: &Id, matcher: &ArgMatcher) -> Vec<Id> {
pub(crate) fn unroll_requirements_for_arg(
&self,
arg: &Id,
matcher: Option<&ArgMatcher>,
) -> Vec<Id> {
let requires_if_or_not = |(val, req_arg): &(ArgPredicate<'_>, Id)| -> Option<Id> {
match val {
ArgPredicate::Equals(v) => {
if matcher
.get(arg)
.map(|ma| ma.contains_val_os(v))
.unwrap_or(false)
{
Some(req_arg.clone())
if let Some(matcher) = matcher {
if matcher
.get(arg)
.map(|ma| ma.contains_val_os(v))
.unwrap_or(false)
{
Some(req_arg.clone())
} else {
None
}
} else {
None
}
Expand Down
10 changes: 4 additions & 6 deletions src/output/usage.rs
Expand Up @@ -360,12 +360,10 @@ impl<'help, 'app> Usage<'help, 'app> {
let mut unrolled_reqs = IndexSet::new();

for a in self.required.iter() {
if let Some(m) = matcher {
for aa in self.app.unroll_requirements_for_arg(a, m) {
// if we don't check for duplicates here this causes duplicate error messages
// see https://github.com/clap-rs/clap/issues/2770
unrolled_reqs.insert(aa);
}
for aa in self.app.unroll_requirements_for_arg(a, matcher) {
// if we don't check for duplicates here this causes duplicate error messages
// see https://github.com/clap-rs/clap/issues/2770
unrolled_reqs.insert(aa);
}
// always include the required arg itself. it will not be enumerated
// by unroll_requirements_for_arg.
Expand Down
6 changes: 5 additions & 1 deletion src/parse/validator.rs
Expand Up @@ -272,7 +272,11 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
for name in matcher.arg_names() {
debug!("Validator::gather_requirements:iter:{:?}", name);
if let Some(arg) = self.p.app.find(name) {
for req in self.p.app.unroll_requirements_for_arg(&arg.id, matcher) {
for req in self
.p
.app
.unroll_requirements_for_arg(&arg.id, Some(matcher))
{
self.p.required.insert(req);
}
} else if let Some(g) = self.p.app.find_group(name) {
Expand Down
4 changes: 2 additions & 2 deletions tests/builder/double_require.rs
Expand Up @@ -16,7 +16,7 @@ static ONLY_B_ERROR: &str = "error: The following required arguments were not pr
-c

USAGE:
prog [OPTIONS] -c -b
prog [OPTIONS] -b -c

For more information try --help
";
Expand All @@ -25,7 +25,7 @@ static ONLY_C_ERROR: &str = "error: The following required arguments were not pr
-b

USAGE:
prog [OPTIONS] -b -c
prog [OPTIONS] -c -b

For more information try --help
";
Expand Down
75 changes: 75 additions & 0 deletions tests/builder/require.rs
Expand Up @@ -115,6 +115,81 @@ fn positional_required_2() {
assert_eq!(m.value_of("flag").unwrap(), "someval");
}

#[test]
fn positional_required_with_requires() {
let app = App::new("positional_required")
.arg(Arg::new("flag").required(true).requires("opt"))
.arg(Arg::new("opt"))
.arg(Arg::new("bar"));

assert!(utils::compare_output(
app,
"clap-test",
POSITIONAL_REQ,
true
));
}

static POSITIONAL_REQ: &str = "error: The following required arguments were not provided:
<flag>
<opt>

USAGE:
clap-test <flag> <opt> [ARGS]

For more information try --help
";

#[test]
fn positional_required_with_requires_if_no_value() {
let app = App::new("positional_required")
.arg(Arg::new("flag").required(true).requires_if("val", "opt"))
.arg(Arg::new("opt"))
.arg(Arg::new("bar"));

assert!(utils::compare_output(
app,
"clap-test",
POSITIONAL_REQ_IF_NO_VAL,
true
));
}

static POSITIONAL_REQ_IF_NO_VAL: &str = "error: The following required arguments were not provided:
<flag>

USAGE:
clap-test <flag> [ARGS]

For more information try --help
";

#[test]
fn positional_required_with_requires_if_value() {
let app = App::new("positional_required")
.arg(Arg::new("flag").required(true).requires_if("val", "opt"))
.arg(Arg::new("foo").required(true))
.arg(Arg::new("opt"))
.arg(Arg::new("bar"));

assert!(utils::compare_output(
app,
"clap-test val",
POSITIONAL_REQ_IF_VAL,
true
));
}

static POSITIONAL_REQ_IF_VAL: &str = "error: The following required arguments were not provided:
<foo>
<opt>

USAGE:
clap-test <flag> <foo> <opt> [ARGS]

For more information try --help
";

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