Skip to content

Commit

Permalink
Merge pull request clap-rs#4609 from epage/error
Browse files Browse the repository at this point in the history
fix(error): Try to polish/clarify messages
  • Loading branch information
epage committed Jan 6, 2023
2 parents 689c77a + 762b06f commit 7d57df5
Show file tree
Hide file tree
Showing 23 changed files with 81 additions and 85 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ once_cell = { version = "1.12.0", optional = true }
trybuild = "1.0.73"
rustversion = "1"
# Cutting out `filesystem` feature
trycmd = { version = "0.14.6", default-features = false, features = ["color-auto", "diff", "examples"] }
trycmd = { version = "0.14.9", default-features = false, features = ["color-auto", "diff", "examples"] }
humantime = "2"
snapbox = "0.4"
shlex = "1.1.0"
Expand Down
12 changes: 6 additions & 6 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: found argument '--unknown' which wasn't expected, or isn't valid in this context
error: unexpected argument '--unknown'

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: found argument '--unknown' which wasn't expected, or isn't valid in this context
error: unexpected argument '--unknown'

Usage: interop_augment_subcommands[EXE] derived [OPTIONS]

Expand All @@ -81,7 +81,7 @@ For more information, try '--help'.
```console
$ interop_augment_subcommands unknown
? failed
error: the subcommand 'unknown' wasn't recognized
error: unrecognized subcommand 'unknown'

Usage: interop_augment_subcommands[EXE] [COMMAND]

Expand Down Expand Up @@ -140,7 +140,7 @@ Cli {
```console
$ interop_hand_subcommand add --unknown
? failed
error: found argument '--unknown' which wasn't expected, or isn't valid in this context
error: unexpected argument '--unknown'

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

Expand Down Expand Up @@ -185,7 +185,7 @@ Cli {
```console
$ interop_hand_subcommand unknown
? failed
error: the subcommand 'unknown' wasn't recognized
error: unrecognized subcommand 'unknown'

Usage: interop_hand_subcommand[EXE] [OPTIONS] <COMMAND>

Expand Down Expand Up @@ -239,7 +239,7 @@ Cli {
```console
$ interop_flatten_hand_args --unknown
? failed
error: found argument '--unknown' which wasn't expected, or isn't valid in this context
error: unexpected argument '--unknown'

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: found argument 'foo' which wasn't expected, or isn't valid in this context
error: unexpected argument 'foo'

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: found argument 'foo' which wasn't expected, or isn't valid in this context
error: unexpected argument 'foo'

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

Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_builder/03_01_flag_bool.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ verbose: true

$ 03_01_flag_bool --verbose --verbose
? failed
error: the argument '--verbose' was provided more than once, but cannot be used multiple times
error: the argument '--verbose' cannot be used multiple times

Usage: 03_01_flag_bool[EXE] [OPTIONS]

Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_builder/04_01_enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Tortoise

$ 04_01_enum medium
? failed
error: 'medium' isn't a valid value for '<MODE>'
error: invalid value 'medium' for '<MODE>'
[possible values: fast, slow]

For more information, try '--help'.
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_builder/04_01_possible.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Tortoise

$ 04_01_possible medium
? failed
error: 'medium' isn't a valid value for '<MODE>'
error: invalid value 'medium' for '<MODE>'
[possible values: fast, slow]

For more information, try '--help'.
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_derive/03_01_flag_bool.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ verbose: true

$ 03_01_flag_bool_derive --verbose --verbose
? failed
error: the argument '--verbose' was provided more than once, but cannot be used multiple times
error: the argument '--verbose' cannot be used multiple times

Usage: 03_01_flag_bool_derive[EXE] [OPTIONS]

Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial_derive/04_01_enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Tortoise

$ 04_01_enum_derive medium
? failed
error: 'medium' isn't a valid value for '<MODE>'
error: invalid value 'medium' for '<MODE>'
[possible values: fast, slow]

For more information, try '--help'.
Expand Down
8 changes: 4 additions & 4 deletions examples/typed-derive.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ Args { optimization: None, include: None, bind: None, sleep: None, defines: [],

$ typed-derive --port
? failed
error: the argument '--port <PORT>' requires a value but none was supplied
error: a value is required for '--port <PORT>' but none was supplied
[possible values: 22, 80]

For more information, try '--help'.

$ typed-derive --port 3000
? failed
error: '3000' isn't a valid value for '--port <PORT>'
error: invalid value '3000' for '--port <PORT>'
[possible values: 22, 80]

For more information, try '--help'.
Expand All @@ -116,14 +116,14 @@ Args { optimization: None, include: None, bind: None, sleep: None, defines: [],

$ typed-derive --log-level
? failed
error: the argument '--log-level <LOG_LEVEL>' requires a value but none was supplied
error: a value is required for '--log-level <LOG_LEVEL>' but none was supplied
[possible values: info, debug, info, warn, error]

For more information, try '--help'.

$ typed-derive --log-level critical
? failed
error: 'critical' isn't a valid value for '--log-level <LOG_LEVEL>'
error: invalid value 'critical' for '--log-level <LOG_LEVEL>'
[possible values: info, debug, info, warn, error]

For more information, try '--help'.
Expand Down
38 changes: 18 additions & 20 deletions src/error/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
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");
styled.none("' cannot be used multiple times");
} else {
styled.none("the argument '");
styled.warning(invalid_arg);
Expand Down Expand Up @@ -181,13 +181,13 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
) = (invalid_arg, invalid_value)
{
if invalid_value.is_empty() {
styled.none("the argument '");
styled.none("a value is required for '");
styled.warning(invalid_arg);
styled.none("' requires a value but none was supplied");
styled.none("' but none was supplied");
} else {
styled.none("'");
styled.none("invalid value '");
styled.none(invalid_value);
styled.none("' isn't a valid value for '");
styled.none("' for '");
styled.warning(invalid_arg);
styled.none("'");
}
Expand Down Expand Up @@ -216,9 +216,9 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
ErrorKind::InvalidSubcommand => {
let invalid_sub = error.get(ContextKind::InvalidSubcommand);
if let Some(ContextValue::String(invalid_sub)) = invalid_sub {
styled.none("the subcommand '");
styled.none("unrecognized subcommand '");
styled.warning(invalid_sub);
styled.none("' wasn't recognized");
styled.none("'");
true
} else {
false
Expand Down Expand Up @@ -276,11 +276,11 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
Some(ContextValue::String(invalid_value)),
) = (invalid_arg, invalid_value)
{
styled.none("the value '");
styled.none("unexpected value '");
styled.warning(invalid_value);
styled.none("' was provided to '");
styled.none("' for '");
styled.warning(invalid_arg);
styled.none("' but it wasn't expecting any more values");
styled.none("'; no more were expected");
true
} else {
false
Expand All @@ -297,11 +297,10 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
) = (invalid_arg, actual_num_values, min_values)
{
let were_provided = singular_or_plural(*actual_num_values as usize);
styled.none("the argument '");
styled.warning(invalid_arg);
styled.none("' requires at least ");
styled.warning(min_values.to_string());
styled.none(" values but only ");
styled.none(" more values required by '");
styled.warning(invalid_arg);
styled.none("'; only ");
styled.warning(actual_num_values.to_string());
styled.none(were_provided);
true
Expand Down Expand Up @@ -343,11 +342,10 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
) = (invalid_arg, actual_num_values, num_values)
{
let were_provided = singular_or_plural(*actual_num_values as usize);
styled.none("the argument '");
styled.warning(invalid_arg);
styled.none("' requires ");
styled.warning(num_values.to_string());
styled.none(" values, but ");
styled.none(" values required for '");
styled.warning(invalid_arg);
styled.none("' but ");
styled.warning(actual_num_values.to_string());
styled.none(were_provided);
true
Expand All @@ -358,9 +356,9 @@ fn write_dynamic_context(error: &crate::error::Error, styled: &mut StyledStr) ->
ErrorKind::UnknownArgument => {
let invalid_arg = error.get(ContextKind::InvalidArg);
if let Some(ContextValue::String(invalid_arg)) = invalid_arg {
styled.none("found argument '");
styled.none("unexpected argument '");
styled.warning(invalid_arg.to_string());
styled.none("' which wasn't expected, or isn't valid in this context");
styled.none("'");
true
} else {
false
Expand Down
12 changes: 5 additions & 7 deletions src/error/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,13 @@ 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("found an argument which wasn't expected or isn't valid in this context")
}
Self::InvalidSubcommand => Some("a subcommand wasn't recognized"),
Self::UnknownArgument => Some("unexpected argument"),
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("an argument received an unexpected value"),
Self::TooFewValues => Some("an argument requires more values"),
Self::WrongNumberOfValues => Some("an argument received too many or too few values"),
Self::TooManyValues => Some("unexpected value for an argument"),
Self::TooFewValues => Some("more values required for an argument"),
Self::WrongNumberOfValues => Some("too many or too few values for an argument"),
Self::ArgumentConflict => {
Some("an argument cannot be used with one or more of the other specified arguments")
}
Expand Down
4 changes: 2 additions & 2 deletions tests/builder/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ For more information, try '--help'.
#[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
error: the argument '-F' cannot be used multiple times
Usage: clap-test [OPTIONS] [positional] [positional2] [positional3]... [COMMAND]
Expand Down 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: found argument 'sub1' which wasn't expected, or isn't valid in this context
error: unexpected argument 'sub1'
Usage: test [OPTIONS]
test <COMMAND>
Expand Down
2 changes: 1 addition & 1 deletion tests/builder/default_missing_vals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ fn delimited_missing_value() {
#[cfg(debug_assertions)]
#[test]
#[cfg(feature = "error-context")]
#[should_panic = "Argument `arg`'s default_missing_value=\"value\" failed validation: error: 'value' isn't a valid value for '[arg]'"]
#[should_panic = "Argument `arg`'s default_missing_value=\"value\" failed validation: error: invalid value 'value' for '[arg]'"]
fn default_missing_values_are_possible_values() {
use clap::{Arg, Command};

Expand Down
4 changes: 2 additions & 2 deletions tests/builder/default_vals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn opt_without_value_fail() {
assert_eq!(err.kind(), ErrorKind::InvalidValue);
assert!(err
.to_string()
.contains("the argument '-o <opt>' requires a value but none was supplied"));
.contains("a value is required for '-o <opt>' but none was supplied"));
}

#[test]
Expand Down Expand Up @@ -796,7 +796,7 @@ fn required_args_with_default_values() {
#[cfg(debug_assertions)]
#[test]
#[cfg(feature = "error-context")]
#[should_panic = "Argument `arg`'s default_value=\"value\" failed validation: error: 'value' isn't a valid value for '[arg]'"]
#[should_panic = "Argument `arg`'s default_value=\"value\" failed validation: error: invalid value 'value' for '[arg]'"]
fn default_values_are_possible_values() {
use clap::{Arg, Command};

Expand Down
Loading

0 comments on commit 7d57df5

Please sign in to comment.