Skip to content

Commit

Permalink
Merge pull request #3968 from epage/suberror
Browse files Browse the repository at this point in the history
fix(error)!: Merge UnrecognizedSubcommand into InvalidSubcommand
  • Loading branch information
epage committed Jul 22, 2022
2 parents 5a4da7e + 0d45912 commit b8d769a
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 51 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Breaking Changes

- `Error::EmptyValue` replaced with `Error::InvalidValue`
- `ErrorKind::EmptyValue` replaced with `ErrorKind::InvalidValue`
- `ErrorKind::UnrecognizedSubcommand` replaced with `ErrorKind::InvalidSubcommand`

### Features

Expand Down
2 changes: 1 addition & 1 deletion clap_derive/src/derives/subcommand.rs
Expand Up @@ -528,7 +528,7 @@ fn gen_from_arg_matches(
},

None => quote! {
::std::result::Result::Err(clap::Error::raw(clap::ErrorKind::UnrecognizedSubcommand, format!("The subcommand '{}' wasn't recognized", #subcommand_name_var)))
::std::result::Result::Err(clap::Error::raw(clap::ErrorKind::InvalidSubcommand, format!("The subcommand '{}' wasn't recognized", #subcommand_name_var)))
},
};

Expand Down
4 changes: 2 additions & 2 deletions examples/derive_ref/hand_subcommand.rs
Expand Up @@ -26,7 +26,7 @@ impl FromArgMatches for CliSub {
Some(("add", args)) => Ok(Self::Add(AddArgs::from_arg_matches(args)?)),
Some(("remove", args)) => Ok(Self::Remove(RemoveArgs::from_arg_matches(args)?)),
Some((_, _)) => Err(Error::raw(
ErrorKind::UnrecognizedSubcommand,
ErrorKind::InvalidSubcommand,
"Valid subcommands are `add` and `remove`",
)),
None => Err(Error::raw(
Expand All @@ -41,7 +41,7 @@ impl FromArgMatches for CliSub {
Some(("remove", args)) => *self = Self::Remove(RemoveArgs::from_arg_matches(args)?),
Some((_, _)) => {
return Err(Error::raw(
ErrorKind::UnrecognizedSubcommand,
ErrorKind::InvalidSubcommand,
"Valid subcommands are `add` and `remove`",
))
}
Expand Down
28 changes: 0 additions & 28 deletions src/error/kind.rs
Expand Up @@ -56,33 +56,6 @@ pub enum ErrorKind {
/// [`UnknownArgument`]: ErrorKind::UnknownArgument
InvalidSubcommand,

/// Occurs when the user provides an unrecognized [`Subcommand`] which either
/// doesn't meet the threshold for being similar enough to an existing subcommand,
/// or the 'suggestions' feature is disabled.
/// Otherwise the more detailed [`InvalidSubcommand`] error is returned.
///
/// This error typically happens when passing additional subcommand names to the `help`
/// subcommand. Otherwise, the more general [`UnknownArgument`] error is used.
///
/// # Examples
///
/// ```rust
/// # use clap::{Command, Arg, ErrorKind, };
/// let result = Command::new("prog")
/// .subcommand(Command::new("config")
/// .about("Used for configuration")
/// .arg(Arg::new("config_file")
/// .help("The configuration file to use")))
/// .try_get_matches_from(vec!["prog", "help", "nothing"]);
/// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind(), ErrorKind::UnrecognizedSubcommand);
/// ```
///
/// [`Subcommand`]: crate::Subcommand
/// [`InvalidSubcommand`]: ErrorKind::InvalidSubcommand
/// [`UnknownArgument`]: ErrorKind::UnknownArgument
UnrecognizedSubcommand,

/// Occurs when the user doesn't use equals for an option that requires equal
/// sign to provide values.
///
Expand Down Expand Up @@ -361,7 +334,6 @@ impl ErrorKind {
Some("Found an argument which wasn't expected or isn't valid in this context")
}
Self::InvalidSubcommand => Some("A subcommand wasn't recognized"),
Self::UnrecognizedSubcommand => Some("A subcommand wasn't recognized"),
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"),
Expand Down
13 changes: 1 addition & 12 deletions src/error/mod.rs
Expand Up @@ -307,7 +307,7 @@ impl Error {
}

pub(crate) fn unrecognized_subcommand(cmd: &Command, subcmd: String, usage: String) -> Self {
Self::new(ErrorKind::UnrecognizedSubcommand)
Self::new(ErrorKind::InvalidSubcommand)
.with_cmd(cmd)
.extend_context_unchecked([
(ContextKind::InvalidSubcommand, ContextValue::String(subcmd)),
Expand Down Expand Up @@ -603,17 +603,6 @@ impl Error {
false
}
}
ErrorKind::UnrecognizedSubcommand => {
let invalid_sub = self.get_context(ContextKind::InvalidSubcommand);
if let Some(ContextValue::String(invalid_sub)) = invalid_sub {
c.none("The subcommand '");
c.warning(invalid_sub);
c.none("' wasn't recognized");
true
} else {
false
}
}
ErrorKind::MissingRequiredArgument => {
let invalid_arg = self.get_context(ContextKind::InvalidArg);
if let Some(ContextValue::Strings(invalid_arg)) = invalid_arg {
Expand Down
4 changes: 2 additions & 2 deletions tests/builder/app_settings.rs
Expand Up @@ -234,7 +234,7 @@ fn infer_subcommands_fail_no_args() {
.subcommand(Command::new("temp"))
.try_get_matches_from(vec!["prog", "te"]);
assert!(m.is_err(), "{:#?}", m.unwrap());
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnrecognizedSubcommand);
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidSubcommand);
}

#[cfg(feature = "suggestions")]
Expand Down Expand Up @@ -333,7 +333,7 @@ fn infer_subcommands_fail_suggestions() {
.subcommand(Command::new("temp"))
.try_get_matches_from(vec!["prog", "temps"]);
assert!(m.is_err(), "{:#?}", m.unwrap());
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnrecognizedSubcommand);
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidSubcommand);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions tests/builder/help.rs
Expand Up @@ -2599,7 +2599,7 @@ fn disabled_help_flag() {
.try_get_matches_from("foo a".split(' '));
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.kind(), ErrorKind::UnrecognizedSubcommand);
assert_eq!(err.kind(), ErrorKind::InvalidSubcommand);
}

#[test]
Expand All @@ -2611,7 +2611,7 @@ fn disabled_help_flag_and_subcommand() {
.try_get_matches_from("foo help".split(' '));
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.kind(), ErrorKind::UnrecognizedSubcommand);
assert_eq!(err.kind(), ErrorKind::InvalidSubcommand);
assert!(
err.to_string().ends_with('\n'),
"Errors should have a trailing newline, got {:?}",
Expand Down
6 changes: 3 additions & 3 deletions tests/builder/subcommands.rs
Expand Up @@ -539,7 +539,7 @@ fn busybox_like_multicall() {

let m = cmd.clone().try_get_matches_from(&["a.out"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnrecognizedSubcommand);
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidSubcommand);
}

#[test]
Expand All @@ -560,7 +560,7 @@ fn hostname_like_multicall() {

let m = cmd.clone().try_get_matches_from(&["a.out"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::UnrecognizedSubcommand);
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidSubcommand);

let m = cmd.try_get_matches_from_mut(&["hostname", "hostname"]);
assert!(m.is_err());
Expand All @@ -581,7 +581,7 @@ fn bad_multicall_command_error() {
.subcommand(Command::new("bar"));

let err = cmd.clone().try_get_matches_from(&["world"]).unwrap_err();
assert_eq!(err.kind(), ErrorKind::UnrecognizedSubcommand);
assert_eq!(err.kind(), ErrorKind::InvalidSubcommand);
static HELLO_EXPECTED: &str = "\
error: The subcommand 'world' wasn't recognized
Expand Down

0 comments on commit b8d769a

Please sign in to comment.