Skip to content

Commit

Permalink
Merge pull request #4288 from epage/conflict
Browse files Browse the repository at this point in the history
fix(error): Specialize the self-conflicts error
  • Loading branch information
epage committed Sep 29, 2022
2 parents 337a9e0 + 4e9f3cc commit 7ea950a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
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

0 comments on commit 7ea950a

Please sign in to comment.