Skip to content

Commit 4e9f3cc

Browse files
committed
fix(error): Specialize the self-conflicts error
Inspired by rust-lang/cargo#11159
1 parent 2c09781 commit 4e9f3cc

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

examples/tutorial_builder/03_01_flag_bool.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ verbose: true
1717

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

2222
Usage: 03_01_flag_bool[EXE] [OPTIONS]
2323

examples/tutorial_derive/03_01_flag_bool.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ verbose: true
1717

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

2222
Usage: 03_01_flag_bool_derive[EXE] [OPTIONS]
2323

src/error/format.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,32 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
9090
if let (Some(ContextValue::String(invalid_arg)), Some(prior_arg)) =
9191
(invalid_arg, prior_arg)
9292
{
93-
styled.none("The argument '");
94-
styled.warning(invalid_arg);
95-
styled.none("' cannot be used with");
93+
if ContextValue::String(invalid_arg.clone()) == *prior_arg {
94+
styled.none("The argument '");
95+
styled.warning(invalid_arg);
96+
styled.none("' was provided more than once, but cannot be used multiple times");
97+
} else {
98+
styled.none("The argument '");
99+
styled.warning(invalid_arg);
100+
styled.none("' cannot be used with");
96101

97-
match prior_arg {
98-
ContextValue::Strings(values) => {
99-
styled.none(":");
100-
for v in values {
101-
styled.none("\n");
102-
styled.none(TAB);
103-
styled.warning(&**v);
102+
match prior_arg {
103+
ContextValue::Strings(values) => {
104+
styled.none(":");
105+
for v in values {
106+
styled.none("\n");
107+
styled.none(TAB);
108+
styled.warning(&**v);
109+
}
110+
}
111+
ContextValue::String(value) => {
112+
styled.none(" '");
113+
styled.warning(value);
114+
styled.none("'");
115+
}
116+
_ => {
117+
styled.none(" one or more of the other specified arguments");
104118
}
105-
}
106-
ContextValue::String(value) => {
107-
styled.none(" '");
108-
styled.warning(value);
109-
styled.none("'");
110-
}
111-
_ => {
112-
styled.none(" one or more of the other specified arguments");
113119
}
114120
}
115121
true

tests/builder/conflicts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ For more information try '--help'
313313
#[cfg(feature = "error-context")]
314314
fn conflict_output_repeat() {
315315
static ERR: &str = "\
316-
error: The argument '-F' cannot be used with '-F'
316+
error: The argument '-F' was provided more than once, but cannot be used multiple times
317317
318318
Usage: clap-test [OPTIONS] [positional] [positional2] [positional3]... [COMMAND]
319319

0 commit comments

Comments
 (0)