Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): Clarify get_count/get_flag assertion #4764

Merged
merged 1 commit into from Mar 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/parser/matches/arg_matches.rs
Expand Up @@ -139,9 +139,12 @@ impl ArgMatches {
/// ```
#[cfg_attr(debug_assertions, track_caller)]
pub fn get_count(&self, id: &str) -> u8 {
*self
.get_one::<u8>(id)
.expect("ArgAction::Count is defaulted")
*self.get_one::<u8>(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
Expand Down Expand Up @@ -173,7 +176,12 @@ impl ArgMatches {
pub fn get_flag(&self, id: &str) -> bool {
*self
.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.
Expand Down