Skip to content

Commit 83b0437

Browse files
authored
Merge pull request #4764 from epage/true
fix(parser): Clarify get_count/get_flag assertion
2 parents 4fa1ec6 + c0dc1cd commit 83b0437

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/parser/matches/arg_matches.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,12 @@ impl ArgMatches {
139139
/// ```
140140
#[cfg_attr(debug_assertions, track_caller)]
141141
pub fn get_count(&self, id: &str) -> u8 {
142-
*self
143-
.get_one::<u8>(id)
144-
.expect("ArgAction::Count is defaulted")
142+
*self.get_one::<u8>(id).unwrap_or_else(|| {
143+
panic!(
144+
"arg `{}`'s `ArgAction` should be `Count` which should provide a default",
145+
id
146+
)
147+
})
145148
}
146149

147150
/// Gets the value of a specific [`ArgAction::SetTrue`][crate::ArgAction::SetTrue] or [`ArgAction::SetFalse`][crate::ArgAction::SetFalse] flag
@@ -173,7 +176,12 @@ impl ArgMatches {
173176
pub fn get_flag(&self, id: &str) -> bool {
174177
*self
175178
.get_one::<bool>(id)
176-
.expect("ArgAction::SetTrue / ArgAction::SetFalse is defaulted")
179+
.unwrap_or_else(|| {
180+
panic!(
181+
"arg `{}`'s `ArgAction` should be one of `SetTrue`, `SetFalse` which should provide a default",
182+
id
183+
)
184+
})
177185
}
178186

179187
/// Iterate over values of a specific option or positional argument.

0 commit comments

Comments
 (0)