Merge pull request #3140 from epage/panic

Improve error reporting for bad arg/group/subcommand names
This commit is contained in:
Ed Page 2021-12-09 19:35:17 -06:00 committed by GitHub
commit ca48e22827
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -987,10 +987,17 @@ impl ArgMatches {
// Private methods
impl ArgMatches {
#[inline]
#[cfg_attr(debug_assertions, track_caller)]
fn get_arg(&self, arg: &Id) -> Option<&MatchedArg> {
#[cfg(debug_assertions)]
{
if *arg != Id::empty_hash() && !self.valid_args.contains(arg) {
if *arg == Id::empty_hash() || self.valid_args.contains(arg) {
} else if self.valid_subcommands.contains(arg) {
panic!(
"Subcommand `'{:?}' used where an argument or group name was expected.",
arg
);
} else {
panic!(
"`'{:?}' is not a name of an argument or a group.\n\
Make sure you're using the name of the argument itself \
@ -1004,10 +1011,17 @@ impl ArgMatches {
}
#[inline]
#[cfg_attr(debug_assertions, track_caller)]
fn get_subcommand(&self, id: &Id) -> Option<&SubCommand> {
#[cfg(debug_assertions)]
{
if *id != Id::empty_hash() && !self.valid_subcommands.contains(id) {
if *id == Id::empty_hash() || self.valid_subcommands.contains(id) {
} else if self.valid_args.contains(id) {
panic!(
"Argument or group `'{:?}' used where a subcommand name was expected.",
id
);
} else {
panic!("'{:?}' is not a name of a subcommand.", id);
}
}