From c0dc1cd0084df94b0ffc02d8562ee4634f7cce66 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 16 Mar 2023 10:23:34 -0500 Subject: [PATCH] fix(parser): Clarify get_count/get_flag assertion Fixes #4763 --- src/parser/matches/arg_matches.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/parser/matches/arg_matches.rs b/src/parser/matches/arg_matches.rs index 13ceea33f17..67d2918d0bd 100644 --- a/src/parser/matches/arg_matches.rs +++ b/src/parser/matches/arg_matches.rs @@ -139,9 +139,12 @@ impl ArgMatches { /// ``` #[cfg_attr(debug_assertions, track_caller)] pub fn get_count(&self, id: &str) -> u8 { - *self - .get_one::(id) - .expect("ArgAction::Count is defaulted") + *self.get_one::(id).unwrap_or_else(|| { + panic!( + "arg `{}`'s `ArgAction` should be `Count` which should provide a default", + id + ) + }) } /// Gets the value of a specific [`ArgAction::SetTrue`][crate::ArgAction::SetTrue] or [`ArgAction::SetFalse`][crate::ArgAction::SetFalse] flag @@ -173,7 +176,12 @@ impl ArgMatches { pub fn get_flag(&self, id: &str) -> bool { *self .get_one::(id) - .expect("ArgAction::SetTrue / ArgAction::SetFalse is defaulted") + .unwrap_or_else(|| { + panic!( + "arg `{}`'s `ArgAction` should be one of `SetTrue`, `SetFalse` which should provide a default", + id + ) + }) } /// Iterate over values of a specific option or positional argument.