mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 15:27:16 +00:00
fix: Provide convenient access for common cases
This commit is contained in:
parent
c1468d78b1
commit
8ed35b4d9f
5 changed files with 27 additions and 13 deletions
|
@ -623,13 +623,14 @@ fn write_positionals_of(p: &Command) -> String {
|
|||
debug!("write_positionals_of:iter: arg={}", arg.get_id());
|
||||
|
||||
let num_args = arg.get_num_args().expect("built");
|
||||
let cardinality = if num_args != 1.into() && num_args != 0.into() {
|
||||
"*:"
|
||||
} else if !arg.is_required_set() {
|
||||
":"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let cardinality =
|
||||
if num_args != builder::ValueRange::EMPTY && num_args != builder::ValueRange::SINGLE {
|
||||
"*:"
|
||||
} else if !arg.is_required_set() {
|
||||
":"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
let a = format!(
|
||||
"'{cardinality}:{name}{help}:{value_completion}' \\",
|
||||
|
|
|
@ -335,7 +335,7 @@ fn gen_args(arg: &Arg, indent: usize) -> String {
|
|||
));
|
||||
|
||||
let num_args = arg.get_num_args().expect("built");
|
||||
if num_args != 0.into() && num_args != 1.into() {
|
||||
if num_args != builder::ValueRange::EMPTY && num_args != builder::ValueRange::SINGLE {
|
||||
buffer.push_str(&format!(
|
||||
"{:indent$}isVariadic: true,\n",
|
||||
"",
|
||||
|
|
|
@ -3945,9 +3945,9 @@ impl<'help> Arg<'help> {
|
|||
self.num_vals.get_or_insert(val_names_len.into());
|
||||
} else {
|
||||
if self.is_takes_value_set() {
|
||||
self.num_vals.get_or_insert(1.into());
|
||||
self.num_vals.get_or_insert(ValueRange::SINGLE);
|
||||
} else {
|
||||
self.num_vals.get_or_insert(0.into());
|
||||
self.num_vals.get_or_insert(ValueRange::EMPTY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ use std::cmp::Ordering;
|
|||
use clap_lex::RawOsStr;
|
||||
|
||||
use crate::builder::arg::ArgProvider;
|
||||
use crate::builder::ValueRange;
|
||||
use crate::mkeymap::KeyType;
|
||||
use crate::ArgAction;
|
||||
use crate::INTERNAL_ERROR_MSG;
|
||||
|
@ -693,7 +694,7 @@ fn assert_arg(arg: &Arg) {
|
|||
|
||||
let num_vals = arg.get_num_args().expect(INTERNAL_ERROR_MSG);
|
||||
// This can be the cause of later asserts, so put this first
|
||||
if num_vals != 0.into() {
|
||||
if num_vals != ValueRange::EMPTY {
|
||||
// HACK: Don't check for flags to make the derive easier
|
||||
let num_val_names = arg.get_value_names().unwrap_or(&[]).len();
|
||||
if num_vals.max_values() < num_val_names {
|
||||
|
@ -728,7 +729,7 @@ fn assert_arg(arg: &Arg) {
|
|||
);
|
||||
}
|
||||
|
||||
if num_vals == 1.into() {
|
||||
if num_vals == ValueRange::SINGLE {
|
||||
assert!(
|
||||
!arg.is_multiple_values_set(),
|
||||
"Argument {}: mismatch between `num_args` and `multiple_values`",
|
||||
|
|
|
@ -6,6 +6,18 @@ pub struct ValueRange {
|
|||
}
|
||||
|
||||
impl ValueRange {
|
||||
/// Nor argument values, or a flag
|
||||
pub const EMPTY: Self = Self {
|
||||
start_inclusive: 0,
|
||||
end_inclusive: 0,
|
||||
};
|
||||
|
||||
/// A single argument value, the most common case for options
|
||||
pub const SINGLE: Self = Self {
|
||||
start_inclusive: 1,
|
||||
end_inclusive: 1,
|
||||
};
|
||||
|
||||
/// Create a range
|
||||
///
|
||||
/// # Panics
|
||||
|
@ -96,7 +108,7 @@ impl std::ops::RangeBounds<usize> for ValueRange {
|
|||
|
||||
impl Default for ValueRange {
|
||||
fn default() -> Self {
|
||||
0.into()
|
||||
Self::EMPTY
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue