Skip to content

Commit

Permalink
fix(error): Make whitespace consistent with self/help
Browse files Browse the repository at this point in the history
Sometimes errors would use a tab, sometimes four spaces.  This makes it
always four spaces and shares a definition with help.
  • Loading branch information
epage committed Aug 31, 2022
1 parent 439c9e7 commit 671914b
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 37 deletions.
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 @@ -75,7 +75,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 @@ -150,7 +150,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 @@ -251,7 +251,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
2 changes: 1 addition & 1 deletion examples/tutorial_builder/04_01_enum.md
Expand Up @@ -21,7 +21,7 @@ Tortoise
$ 04_01_enum medium
? failed
error: "medium" isn't a valid value for '<MODE>'
[possible values: fast, slow]
[possible values: fast, slow]

For more information try --help

Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_builder/04_01_possible.md
Expand Up @@ -21,7 +21,7 @@ Tortoise
$ 04_01_possible medium
? failed
error: "medium" isn't a valid value for '<MODE>'
[possible values: fast, slow]
[possible values: fast, slow]

For more information try --help

Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_derive/04_01_enum.md
Expand Up @@ -21,7 +21,7 @@ Tortoise
$ 04_01_enum_derive medium
? failed
error: "medium" isn't a valid value for '<MODE>'
[possible values: fast, slow]
[possible values: fast, slow]

For more information try --help

Expand Down
35 changes: 26 additions & 9 deletions src/error/format.rs
Expand Up @@ -6,6 +6,7 @@ use crate::builder::StyledStr;
use crate::error::ContextKind;
use crate::error::ContextValue;
use crate::error::ErrorKind;
use crate::output::TAB;

/// Defines how to format an error for displaying to the user
pub trait ErrorFormatter: Sized {
Expand Down Expand Up @@ -110,7 +111,8 @@ fn write_dynamic_context(error: &crate::Error, styled: &mut StyledStr) -> bool {
ContextValue::Strings(values) => {
styled.none(":");
for v in values {
styled.none("\n ");
styled.none("\n");
styled.none(TAB);
styled.warning(&**v);
}
}
Expand Down Expand Up @@ -161,7 +163,9 @@ fn write_dynamic_context(error: &crate::Error, styled: &mut StyledStr) -> bool {
let possible_values = error.get(ContextKind::ValidValue);
if let Some(ContextValue::Strings(possible_values)) = possible_values {
if !possible_values.is_empty() {
styled.none("\n\t[possible values: ");
styled.none("\n");
styled.none(TAB);
styled.none("[possible values: ");
if let Some((last, elements)) = possible_values.split_last() {
for v in elements {
styled.good(escape(v));
Expand All @@ -175,7 +179,9 @@ fn write_dynamic_context(error: &crate::Error, styled: &mut StyledStr) -> bool {

let suggestion = error.get(ContextKind::SuggestedValue);
if let Some(ContextValue::String(suggestion)) = suggestion {
styled.none("\n\n\tDid you mean ");
styled.none("\n\n");
styled.none(TAB);
styled.none("Did you mean ");
styled.good(quote(suggestion));
styled.none("?");
}
Expand All @@ -193,7 +199,9 @@ fn write_dynamic_context(error: &crate::Error, styled: &mut StyledStr) -> bool {

let valid_sub = error.get(ContextKind::SuggestedSubcommand);
if let Some(ContextValue::String(valid_sub)) = valid_sub {
styled.none("\n\n\tDid you mean ");
styled.none("\n\n");
styled.none(TAB);
styled.none("Did you mean ");
styled.good(valid_sub);
styled.none("?");
}
Expand All @@ -216,7 +224,8 @@ fn write_dynamic_context(error: &crate::Error, styled: &mut StyledStr) -> bool {
if let Some(ContextValue::Strings(invalid_arg)) = invalid_arg {
styled.none("The following required arguments were not provided:");
for v in invalid_arg {
styled.none("\n ");
styled.none("\n");
styled.none(TAB);
styled.good(&**v);
}
true
Expand Down Expand Up @@ -337,15 +346,19 @@ fn write_dynamic_context(error: &crate::Error, styled: &mut StyledStr) -> bool {
Some(ContextValue::String(valid_sub)),
Some(ContextValue::String(valid_arg)),
) => {
styled.none("\n\n\tDid you mean ");
styled.none("\n\n");
styled.none(TAB);
styled.none("Did you mean ");
styled.none("to put '");
styled.good(valid_arg);
styled.none("' after the subcommand '");
styled.good(valid_sub);
styled.none("'?");
}
(None, Some(ContextValue::String(valid_arg))) => {
styled.none("\n\n\tDid you mean '");
styled.none("\n\n");
styled.none(TAB);
styled.none("Did you mean '");
styled.good(valid_arg);
styled.none("'?");
}
Expand All @@ -355,16 +368,20 @@ fn write_dynamic_context(error: &crate::Error, styled: &mut StyledStr) -> bool {
let invalid_arg = error.get(ContextKind::InvalidArg);
if let Some(ContextValue::String(invalid_arg)) = invalid_arg {
if invalid_arg.starts_with('-') {
styled.none("\n\n");
styled.none(TAB);
styled.none(format!(
"\n\n\tIf 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 `-- {}`",
invalid_arg, invalid_arg
));
}

let trailing_arg = error.get(ContextKind::TrailingArg);
if trailing_arg == Some(&ContextValue::Bool(true)) {
styled.none("\n\n");
styled.none(TAB);
styled.none(format!(
"\n\n\tIf you tried to supply `{}` as a subcommand, remove the '--' before it.",
"If you tried to supply `{}` as a subcommand, remove the '--' before it.",
invalid_arg
));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/builder/flags.rs
Expand Up @@ -4,7 +4,7 @@ use clap::{arg, Arg, ArgAction, Command};
const USE_FLAG_AS_ARGUMENT: &str =
"error: Found argument '--another-flag' which wasn't expected, or isn't valid in this context
\tIf 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 @@ -181,7 +181,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
8 changes: 4 additions & 4 deletions tests/builder/opts.rs
Expand Up @@ -6,9 +6,9 @@ use clap::{arg, error::ErrorKind, Arg, ArgAction, ArgMatches, Command};
static DYM: &str =
"error: Found argument '--optio' which wasn't expected, or isn't valid in this context
\tDid you mean '--option'?
Did you mean '--option'?
\tIf 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 All @@ -20,9 +20,9 @@ For more information try --help
static DYM_ISSUE_1073: &str =
"error: Found argument '--files-without-matches' which wasn't expected, or isn't valid in this context
\tDid you mean '--files-without-match'?
Did you mean '--files-without-match'?
\tIf 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
14 changes: 7 additions & 7 deletions tests/builder/possible_values.rs
Expand Up @@ -4,32 +4,32 @@ use clap::{builder::PossibleValue, error::ErrorKind, Arg, ArgAction, Command};

#[cfg(feature = "suggestions")]
static PV_ERROR: &str = "error: \"slo\" isn't a valid value for '-O <option>'
\t[possible values: slow, fast, \"ludicrous speed\"]
[possible values: slow, fast, \"ludicrous speed\"]
\tDid you mean \"slow\"?
Did you mean \"slow\"?
For more information try --help
";

#[cfg(not(feature = "suggestions"))]
static PV_ERROR: &str = "error: \"slo\" isn't a valid value for '-O <option>'
\t[possible values: slow, fast, \"ludicrous speed\"]
[possible values: slow, fast, \"ludicrous speed\"]
For more information try --help
";

#[cfg(feature = "suggestions")]
static PV_ERROR_ESCAPED: &str = "error: \"ludicrous\" isn't a valid value for '-O <option>'
\t[possible values: slow, fast, \"ludicrous speed\"]
[possible values: slow, fast, \"ludicrous speed\"]
\tDid you mean \"ludicrous speed\"?
Did you mean \"ludicrous speed\"?
For more information try --help
";

#[cfg(not(feature = "suggestions"))]
static PV_ERROR_ESCAPED: &str = "error: \"ludicrous\" isn't a valid value for '-O <option>'
\t[possible values: slow, fast, \"ludicrous speed\"]
[possible values: slow, fast, \"ludicrous speed\"]
For more information try --help
";
Expand Down Expand Up @@ -295,7 +295,7 @@ fn missing_possible_value_error() {

static MISSING_PV_ERROR: &str =
"error: The argument '-O <option>' requires a value but none was supplied
\t[possible values: slow, fast, \"ludicrous speed\"]
[possible values: slow, fast, \"ludicrous speed\"]
For more information try --help
";
Expand Down
14 changes: 7 additions & 7 deletions tests/builder/subcommands.rs
Expand Up @@ -31,7 +31,7 @@ Options:
#[cfg(feature = "suggestions")]
static DYM_SUBCMD: &str = "error: The subcommand 'subcm' wasn't recognized
Did you mean 'subcmd'?
Did you mean 'subcmd'?
If you believe you received this message in error, try re-running with 'dym -- subcm'
Expand All @@ -44,7 +44,7 @@ For more information try --help
#[cfg(feature = "suggestions")]
static DYM_SUBCMD_AMBIGUOUS: &str = "error: The subcommand 'te' wasn't recognized
Did you mean 'test' or 'temp'?
Did you mean 'test' or 'temp'?
If you believe you received this message in error, try re-running with 'dym -- te'
Expand All @@ -57,7 +57,7 @@ For more information try --help
static SUBCMD_AFTER_DOUBLE_DASH: &str =
"error: Found argument 'subcmd' which wasn't expected, or isn't valid in this context
\tIf 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 Expand Up @@ -177,9 +177,9 @@ fn subcmd_did_you_mean_output_arg() {
static EXPECTED: &str =
"error: Found argument '--subcmarg' which wasn't expected, or isn't valid in this context
\tDid you mean to put '--subcmdarg' after the subcommand 'subcmd'?
Did you mean to put '--subcmdarg' after the subcommand 'subcmd'?
\tIf 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 @@ -200,7 +200,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
\tIf 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 @@ -529,7 +529,7 @@ For more information try help
static BAZ_EXPECTED: &str = "\
error: The subcommand 'baz' wasn't recognized
\tDid you mean 'bar'?
Did you mean 'bar'?
If you believe you received this message in error, try re-running with ' -- baz'
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
\tIf 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

0 comments on commit 671914b

Please sign in to comment.