fix(parser): Clarify get_count/get_flag assertion

Fixes #4763
This commit is contained in:
Ed Page 2023-03-16 10:23:34 -05:00
parent 4fa1ec6d68
commit c0dc1cd008

View file

@ -139,9 +139,12 @@ impl ArgMatches {
/// ``` /// ```
#[cfg_attr(debug_assertions, track_caller)] #[cfg_attr(debug_assertions, track_caller)]
pub fn get_count(&self, id: &str) -> u8 { pub fn get_count(&self, id: &str) -> u8 {
*self *self.get_one::<u8>(id).unwrap_or_else(|| {
.get_one::<u8>(id) panic!(
.expect("ArgAction::Count is defaulted") "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 /// 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 { pub fn get_flag(&self, id: &str) -> bool {
*self *self
.get_one::<bool>(id) .get_one::<bool>(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. /// Iterate over values of a specific option or positional argument.