Skip to content

Commit 32c119e

Browse files
committed
refactor(assert): Be more specific than action.takes_values
1 parent 80ea3e7 commit 32c119e

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

clap_builder/src/builder/action.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,21 @@ impl ArgAction {
371371
}
372372
}
373373

374+
#[cfg(debug_assertions)]
375+
pub(crate) fn max_num_args(&self) -> ValueRange {
376+
match self {
377+
Self::Set => ValueRange::FULL,
378+
Self::Append => ValueRange::FULL,
379+
Self::SetTrue => ValueRange::EMPTY,
380+
Self::SetFalse => ValueRange::EMPTY,
381+
Self::Count => ValueRange::EMPTY,
382+
Self::Help => ValueRange::EMPTY,
383+
Self::HelpShort => ValueRange::EMPTY,
384+
Self::HelpLong => ValueRange::EMPTY,
385+
Self::Version => ValueRange::EMPTY,
386+
}
387+
}
388+
374389
pub(crate) fn default_num_args(&self) -> ValueRange {
375390
match self {
376391
Self::Set => ValueRange::SINGLE,

clap_builder/src/builder/debug_asserts.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -690,15 +690,14 @@ fn assert_arg(arg: &Arg) {
690690
arg.get_id(),
691691
);
692692

693-
if arg.is_takes_value_set() {
694-
assert!(
695-
arg.get_action().takes_values(),
696-
"Argument `{}`'s action {:?} is incompatible with `num_args({:?})`",
697-
arg.get_id(),
698-
arg.get_action(),
699-
arg.get_num_args().unwrap_or(1.into())
700-
);
701-
}
693+
assert!(
694+
arg.get_num_args().unwrap_or(1.into()).max_values()
695+
<= arg.get_action().max_num_args().max_values(),
696+
"Argument `{}`'s action {:?} is incompatible with `num_args({:?})`",
697+
arg.get_id(),
698+
arg.get_action(),
699+
arg.get_num_args().unwrap_or(1.into())
700+
);
702701
if let Some(action_type_id) = arg.get_action().value_type_id() {
703702
assert_eq!(
704703
action_type_id,

clap_builder/src/builder/range.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ impl ValueRange {
1818
end_inclusive: 1,
1919
};
2020

21+
pub(crate) const FULL: Self = Self {
22+
start_inclusive: 0,
23+
end_inclusive: usize::MAX,
24+
};
25+
2126
/// Create a range
2227
///
2328
/// # Panics
@@ -135,9 +140,7 @@ impl From<std::ops::Range<usize>> for ValueRange {
135140

136141
impl From<std::ops::RangeFull> for ValueRange {
137142
fn from(_: std::ops::RangeFull) -> Self {
138-
let start_inclusive = 0;
139-
let end_inclusive = usize::MAX;
140-
Self::raw(start_inclusive, end_inclusive)
143+
Self::FULL
141144
}
142145
}
143146

0 commit comments

Comments
 (0)