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): Polish up errors a little #4287

Merged
merged 3 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
8 changes: 4 additions & 4 deletions examples/derive_ref/interop_tests.md
Expand Up @@ -37,7 +37,7 @@ $ interop_augment_args --unknown
? failed
error: Found argument '--unknown' which wasn't expected, or isn't valid in this context

If you tried to supply `--unknown` as a value rather than a flag, use `-- --unknown`
If you tried to supply '--unknown' as a value rather than a flag, use '-- --unknown'

Usage: interop_augment_args[EXE] [OPTIONS]

Expand Down Expand Up @@ -74,7 +74,7 @@ $ interop_augment_subcommands derived --unknown
? failed
error: Found argument '--unknown' which wasn't expected, or isn't valid in this context

If you tried to supply `--unknown` as a value rather than a flag, use `-- --unknown`
If you tried to supply '--unknown' as a value rather than a flag, use '-- --unknown'

Usage: interop_augment_subcommands[EXE] derived [OPTIONS]

Expand Down Expand Up @@ -146,7 +146,7 @@ $ interop_hand_subcommand add --unknown
? failed
error: Found argument '--unknown' which wasn't expected, or isn't valid in this context

If you tried to supply `--unknown` as a value rather than a flag, use `-- --unknown`
If you tried to supply '--unknown' as a value rather than a flag, use '-- --unknown'

Usage: interop_hand_subcommand[EXE] add [NAME]...

Expand Down Expand Up @@ -245,7 +245,7 @@ $ interop_flatten_hand_args --unknown
? failed
error: Found argument '--unknown' which wasn't expected, or isn't valid in this context

If you tried to supply `--unknown` as a value rather than a flag, use `-- --unknown`
If you tried to supply '--unknown' as a value rather than a flag, use '-- --unknown'

Usage: interop_flatten_hand_args[EXE] [OPTIONS]

Expand Down
1 change: 1 addition & 0 deletions src/builder/styled_str.rs
Expand Up @@ -40,6 +40,7 @@ impl StyledStr {
self.stylize_(Some(Style::Placeholder), msg.into());
}

#[cfg_attr(not(feature = "error-context"), allow(dead_code))]
pub(crate) fn good(&mut self, msg: impl Into<String>) {
self.stylize_(Some(Style::Good), msg.into());
}
Expand Down
21 changes: 12 additions & 9 deletions src/error/format.rs
Expand Up @@ -357,20 +357,23 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
if invalid_arg.starts_with('-') {
styled.none("\n\n");
styled.none(TAB);
styled.none(format!(
"If you tried to supply `{}` as a value rather than a flag, use `-- {}`",
invalid_arg, invalid_arg
));
styled.none("If you tried to supply '");
styled.warning(invalid_arg);
styled.none("' as a value rather than a flag, use '");
styled.good("-- ");
styled.good(invalid_arg);
styled.none("'");
}

let trailing_arg = error.get(ContextKind::TrailingArg);
if trailing_arg == Some(&ContextValue::Bool(true)) {
styled.none("\n\n");
styled.none(TAB);
styled.none(format!(
"If you tried to supply `{}` as a subcommand, remove the '--' before it.",
invalid_arg
));
styled.none("If you tried to supply '");
styled.warning(invalid_arg);
styled.none("' as a subcommand, remove the '");
styled.warning("--");
styled.none("' before it.");
}
}
true
Expand Down Expand Up @@ -430,7 +433,7 @@ pub(crate) fn get_help_flag(cmd: &Command) -> Option<&'static str> {
fn try_help(styled: &mut StyledStr, help: Option<&str>) {
if let Some(help) = help {
styled.none("\n\nFor more information try '");
styled.good(help.to_owned());
styled.literal(help.to_owned());
styled.none("'\n");
} else {
styled.none("\n");
Expand Down
4 changes: 2 additions & 2 deletions tests/builder/flags.rs
Expand Up @@ -160,7 +160,7 @@ fn issue_1284_argument_in_flag_style() {
const USE_FLAG_AS_ARGUMENT: &str = "\
error: Found argument '--another-flag' which wasn't expected, or isn't valid in this context
If you tried to supply `--another-flag` as a value rather than a flag, use `-- --another-flag`
If you tried to supply '--another-flag' as a value rather than a flag, use '-- --another-flag'
Usage: mycat [OPTIONS] [filename]
Expand Down Expand Up @@ -204,7 +204,7 @@ fn issue_2308_multiple_dashes() {
static MULTIPLE_DASHES: &str = "\
error: Found argument '-----' which wasn't expected, or isn't valid in this context
If you tried to supply `-----` as a value rather than a flag, use `-- -----`
If you tried to supply '-----' as a value rather than a flag, use '-- -----'
Usage: test <arg>
Expand Down
4 changes: 2 additions & 2 deletions tests/builder/opts.rs
Expand Up @@ -451,7 +451,7 @@ error: Found argument '--optio' which wasn't expected, or isn't valid in this co
Did you mean '--option'?
If you tried to supply `--optio` as a value rather than a flag, use `-- --optio`
If you tried to supply '--optio' as a value rather than a flag, use '-- --optio'
Usage: clap-test --option <opt>... [positional] [positional2] [positional3]...
Expand Down Expand Up @@ -551,7 +551,7 @@ error: Found argument '--files-without-matches' which wasn't expected, or isn't
Did you mean '--files-without-match'?
If you tried to supply `--files-without-matches` as a value rather than a flag, use `-- --files-without-matches`
If you tried to supply '--files-without-matches' as a value rather than a flag, use '-- --files-without-matches'
Usage: ripgrep-616 --files-without-match
Expand Down
6 changes: 3 additions & 3 deletions tests/builder/subcommands.rs
Expand Up @@ -145,7 +145,7 @@ error: Found argument '--subcmarg' which wasn't expected, or isn't valid in this
Did you mean to put '--subcmdarg' after the subcommand 'subcmd'?
If you tried to supply `--subcmarg` as a value rather than a flag, use `-- --subcmarg`
If you tried to supply '--subcmarg' as a value rather than a flag, use '-- --subcmarg'
Usage: dym [COMMAND]
Expand All @@ -165,7 +165,7 @@ fn subcmd_did_you_mean_output_arg_false_positives() {
static EXPECTED: &str = "\
error: Found argument '--subcmarg' which wasn't expected, or isn't valid in this context
If you tried to supply `--subcmarg` as a value rather than a flag, use `-- --subcmarg`
If you tried to supply '--subcmarg' as a value rather than a flag, use '-- --subcmarg'
Usage: dym [COMMAND]
Expand Down Expand Up @@ -359,7 +359,7 @@ fn subcommand_used_after_double_dash() {
static SUBCMD_AFTER_DOUBLE_DASH: &str = "\
error: Found argument 'subcmd' which wasn't expected, or isn't valid in this context
If you tried to supply `subcmd` as a subcommand, remove the '--' before it.
If you tried to supply 'subcmd' as a subcommand, remove the '--' before it.
Usage: cmd [COMMAND]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error_stderr.toml
Expand Up @@ -5,7 +5,7 @@ stdout = ""
stderr = """
error: Found argument '--unknown-argument' which wasn't expected, or isn't valid in this context
If you tried to supply `--unknown-argument` as a value rather than a flag, use `-- --unknown-argument`
If you tried to supply '--unknown-argument' as a value rather than a flag, use '-- --unknown-argument'
Usage: stdio-fixture[EXE] [OPTIONS] [COMMAND]
Expand Down