Skip to content

Commit

Permalink
Merge pull request #4639 from epage/error
Browse files Browse the repository at this point in the history
fix(error): Try to soften unexpected argument/value errors
  • Loading branch information
epage authored Jan 14, 2023
2 parents 34d856b + 162a556 commit 4d913fa
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions examples/derive_ref/interop_tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Value of derived: DerivedArgs {
```console
$ interop_augment_args --unknown
? failed
error: unexpected argument '--unknown'
error: unexpected argument '--unknown' found

Usage: interop_augment_args[EXE] [OPTIONS]

Expand Down Expand Up @@ -70,7 +70,7 @@ Derived subcommands: Derived {
```console
$ interop_augment_subcommands derived --unknown
? failed
error: unexpected argument '--unknown'
error: unexpected argument '--unknown' found

Usage: interop_augment_subcommands[EXE] derived [OPTIONS]

Expand Down Expand Up @@ -140,7 +140,7 @@ Cli {
```console
$ interop_hand_subcommand add --unknown
? failed
error: unexpected argument '--unknown'
error: unexpected argument '--unknown' found

note: to pass '--unknown' as a value, use '-- --unknown'

Expand Down Expand Up @@ -239,7 +239,7 @@ Cli {
```console
$ interop_flatten_hand_args --unknown
? failed
error: unexpected argument '--unknown'
error: unexpected argument '--unknown' found

Usage: interop_flatten_hand_args[EXE] [OPTIONS]

Expand Down
2 changes: 1 addition & 1 deletion examples/escaped-positional-derive.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Notice that we can't pass positional arguments before `--`:
```console
$ escaped-positional-derive foo bar
? failed
error: unexpected argument 'foo'
error: unexpected argument 'foo' found

Usage: escaped-positional-derive[EXE] [OPTIONS] [-- <SLOP>...]

Expand Down
2 changes: 1 addition & 1 deletion examples/escaped-positional.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Notice that we can't pass positional arguments before `--`:
```console
$ escaped-positional foo bar
? failed
error: unexpected argument 'foo'
error: unexpected argument 'foo' found

Usage: escaped-positional[EXE] [OPTIONS] [-- <SLOP>...]

Expand Down
4 changes: 2 additions & 2 deletions src/error/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
styled.warning(invalid_value);
styled.none("' for '");
styled.warning(invalid_arg);
styled.none("'; no more were expected");
styled.none("' found; no more were expected");
true
} else {
false
Expand Down Expand Up @@ -360,7 +360,7 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
if let Some(ContextValue::String(invalid_arg)) = invalid_arg {
styled.none("unexpected argument '");
styled.warning(invalid_arg.to_string());
styled.none("'");
styled.none("' found");
true
} else {
false
Expand Down
4 changes: 2 additions & 2 deletions src/error/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,11 @@ impl ErrorKind {
pub fn as_str(self) -> Option<&'static str> {
match self {
Self::InvalidValue => Some("one of the values isn't valid for an argument"),
Self::UnknownArgument => Some("unexpected argument"),
Self::UnknownArgument => Some("unexpected argument found"),
Self::InvalidSubcommand => Some("unrecognized subcommand"),
Self::NoEquals => Some("equal is needed when assigning values to one of the arguments"),
Self::ValueValidation => Some("invalid value for one of the arguments"),
Self::TooManyValues => Some("unexpected value for an argument"),
Self::TooManyValues => Some("unexpected value for an argument found"),
Self::TooFewValues => Some("more values required for an argument"),
Self::WrongNumberOfValues => Some("too many or too few values for an argument"),
Self::ArgumentConflict => {
Expand Down
2 changes: 1 addition & 1 deletion tests/builder/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ fn args_negate_subcommands_two_levels() {
#[cfg(feature = "error-context")]
fn subcommand_conflict_error_message() {
static CONFLICT_ERR: &str = "\
error: unexpected argument 'sub1'
error: unexpected argument 'sub1' found
Usage: test [OPTIONS]
test <COMMAND>
Expand Down
10 changes: 5 additions & 5 deletions tests/builder/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn kind_formats_validation_error() {
let err = res.unwrap_err();
let expected_kind = ErrorKind::UnknownArgument;
static MESSAGE: &str = "\
error: unexpected argument
error: unexpected argument found
";
assert_error(err, expected_kind, MESSAGE, true);
}
Expand All @@ -120,7 +120,7 @@ fn rich_formats_validation_error() {
let err = res.unwrap_err();
let expected_kind = ErrorKind::UnknownArgument;
static MESSAGE: &str = "\
error: unexpected argument 'unused'
error: unexpected argument 'unused' found
Usage: test
Expand All @@ -139,7 +139,7 @@ fn suggest_trailing() {
let err = res.unwrap_err();
let expected_kind = ErrorKind::UnknownArgument;
static MESSAGE: &str = "\
error: unexpected argument '--foo'
error: unexpected argument '--foo' found
note: to pass '--foo' as a value, use '-- --foo'
Expand All @@ -160,7 +160,7 @@ fn trailing_already_in_use() {
let err = res.unwrap_err();
let expected_kind = ErrorKind::UnknownArgument;
static MESSAGE: &str = "\
error: unexpected argument '--foo'
error: unexpected argument '--foo' found
Usage: rg [PATTERN]
Expand All @@ -179,7 +179,7 @@ fn cant_use_trailing() {
let err = res.unwrap_err();
let expected_kind = ErrorKind::UnknownArgument;
static MESSAGE: &str = "\
error: unexpected argument '--foo'
error: unexpected argument '--foo' found
Usage: test
Expand Down
6 changes: 3 additions & 3 deletions tests/builder/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn multiple_flags_in_single() {
#[cfg(feature = "error-context")]
fn unexpected_value_error() {
const USE_FLAG_AS_ARGUMENT: &str = "\
error: unexpected value 'foo' for '--a-flag'; no more were expected
error: unexpected value 'foo' for '--a-flag' found; no more were expected
Usage: mycat --a-flag [filename]
Expand All @@ -158,7 +158,7 @@ For more information, try '--help'.
#[cfg(feature = "error-context")]
fn issue_1284_argument_in_flag_style() {
const USE_FLAG_AS_ARGUMENT: &str = "\
error: unexpected argument '--another-flag'
error: unexpected argument '--another-flag' found
note: to pass '--another-flag' as a value, use '-- --another-flag'
Expand Down Expand Up @@ -202,7 +202,7 @@ For more information, try '--help'.
#[cfg(feature = "error-context")]
fn issue_2308_multiple_dashes() {
static MULTIPLE_DASHES: &str = "\
error: unexpected argument '-----'
error: unexpected argument '-----' found
note: to pass '-----' as a value, use '-- -----'
Expand Down
4 changes: 2 additions & 2 deletions tests/builder/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ fn leading_hyphen_with_only_pos_follows() {
#[cfg(feature = "error-context")]
fn did_you_mean() {
static DYM: &str = "\
error: unexpected argument '--optio'
error: unexpected argument '--optio' found
note: argument '--option' exists
Expand Down Expand Up @@ -544,7 +544,7 @@ fn issue_1105_empty_value_short_explicit_no_space() {
#[cfg(feature = "error-context")]
fn issue_1073_suboptimal_flag_suggestion() {
static DYM_ISSUE_1073: &str = "\
error: unexpected argument '--files-without-matches'
error: unexpected argument '--files-without-matches' found
note: argument '--files-without-match' exists
Expand Down
6 changes: 3 additions & 3 deletions tests/builder/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ For more information, try '--help'.
#[cfg(feature = "error-context")]
fn subcmd_did_you_mean_output_arg() {
static EXPECTED: &str = "\
error: unexpected argument '--subcmarg'
error: unexpected argument '--subcmarg' found
note: 'subcmd --subcmdarg' exists
Expand All @@ -159,7 +159,7 @@ For more information, try '--help'.
#[cfg(feature = "error-context")]
fn subcmd_did_you_mean_output_arg_false_positives() {
static EXPECTED: &str = "\
error: unexpected argument '--subcmarg'
error: unexpected argument '--subcmarg' found
Usage: dym [COMMAND]
Expand Down Expand Up @@ -351,7 +351,7 @@ fn subcommand_placeholder_test() {
#[cfg(feature = "error-context")]
fn subcommand_used_after_double_dash() {
static SUBCMD_AFTER_DOUBLE_DASH: &str = "\
error: unexpected argument 'subcmd'
error: unexpected argument 'subcmd' found
note: subcommand 'subcmd' exists; to use it, remove the '--' before it
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error_stderr.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ args = ["--unknown-argument"]
status.code = 2
stdout = ""
stderr = """
error: unexpected argument '--unknown-argument'
error: unexpected argument '--unknown-argument' found
Usage: stdio-fixture[EXE] [OPTIONS] [COMMAND]
Expand Down

0 comments on commit 4d913fa

Please sign in to comment.