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): Specialize the self-conflicts error #4288

Merged
merged 2 commits into from Sep 29, 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
2 changes: 1 addition & 1 deletion examples/tutorial_builder/03_01_flag_bool.md
Expand Up @@ -17,7 +17,7 @@ verbose: true

$ 03_01_flag_bool --verbose --verbose
? failed
error: The argument '--verbose' cannot be used with '--verbose'
error: The argument '--verbose' was provided more than once, but cannot be used multiple times

Usage: 03_01_flag_bool[EXE] [OPTIONS]

Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_derive/03_01_flag_bool.md
Expand Up @@ -17,7 +17,7 @@ verbose: true

$ 03_01_flag_bool_derive --verbose --verbose
? failed
error: The argument '--verbose' cannot be used with '--verbose'
error: The argument '--verbose' was provided more than once, but cannot be used multiple times

Usage: 03_01_flag_bool_derive[EXE] [OPTIONS]

Expand Down
42 changes: 24 additions & 18 deletions src/error/format.rs
Expand Up @@ -90,26 +90,32 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
if let (Some(ContextValue::String(invalid_arg)), Some(prior_arg)) =
(invalid_arg, prior_arg)
{
styled.none("The argument '");
styled.warning(invalid_arg);
styled.none("' cannot be used with");
if ContextValue::String(invalid_arg.clone()) == *prior_arg {
styled.none("The argument '");
styled.warning(invalid_arg);
styled.none("' was provided more than once, but cannot be used multiple times");
} else {
styled.none("The argument '");
styled.warning(invalid_arg);
styled.none("' cannot be used with");

match prior_arg {
ContextValue::Strings(values) => {
styled.none(":");
for v in values {
styled.none("\n");
styled.none(TAB);
styled.warning(&**v);
match prior_arg {
ContextValue::Strings(values) => {
styled.none(":");
for v in values {
styled.none("\n");
styled.none(TAB);
styled.warning(&**v);
}
}
ContextValue::String(value) => {
styled.none(" '");
styled.warning(value);
styled.none("'");
}
_ => {
styled.none(" one or more of the other specified arguments");
}
}
ContextValue::String(value) => {
styled.none(" '");
styled.warning(value);
styled.none("'");
}
_ => {
styled.none(" one or more of the other specified arguments");
}
}
true
Expand Down
14 changes: 14 additions & 0 deletions tests/builder/conflicts.rs
Expand Up @@ -309,6 +309,20 @@ For more information try '--help'
);
}

#[test]
#[cfg(feature = "error-context")]
fn conflict_output_repeat() {
static ERR: &str = "\
error: The argument '-F' was provided more than once, but cannot be used multiple times

Usage: clap-test [OPTIONS] [positional] [positional2] [positional3]... [COMMAND]

For more information try '--help'
";

utils::assert_output(utils::complex_app(), "clap-test -F -F", ERR, true);
}

#[test]
#[cfg(feature = "error-context")]
fn conflict_output_with_required() {
Expand Down