fix: Provide convenient access for common cases

This commit is contained in:
Ed Page 2022-08-05 12:06:51 -05:00
parent c1468d78b1
commit 8ed35b4d9f
5 changed files with 27 additions and 13 deletions

View file

@ -623,13 +623,14 @@ fn write_positionals_of(p: &Command) -> String {
debug!("write_positionals_of:iter: arg={}", arg.get_id()); debug!("write_positionals_of:iter: arg={}", arg.get_id());
let num_args = arg.get_num_args().expect("built"); let num_args = arg.get_num_args().expect("built");
let cardinality = if num_args != 1.into() && num_args != 0.into() { let cardinality =
"*:" if num_args != builder::ValueRange::EMPTY && num_args != builder::ValueRange::SINGLE {
} else if !arg.is_required_set() { "*:"
":" } else if !arg.is_required_set() {
} else { ":"
"" } else {
}; ""
};
let a = format!( let a = format!(
"'{cardinality}:{name}{help}:{value_completion}' \\", "'{cardinality}:{name}{help}:{value_completion}' \\",

View file

@ -335,7 +335,7 @@ fn gen_args(arg: &Arg, indent: usize) -> String {
)); ));
let num_args = arg.get_num_args().expect("built"); 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!( buffer.push_str(&format!(
"{:indent$}isVariadic: true,\n", "{:indent$}isVariadic: true,\n",
"", "",

View file

@ -3945,9 +3945,9 @@ impl<'help> Arg<'help> {
self.num_vals.get_or_insert(val_names_len.into()); self.num_vals.get_or_insert(val_names_len.into());
} else { } else {
if self.is_takes_value_set() { if self.is_takes_value_set() {
self.num_vals.get_or_insert(1.into()); self.num_vals.get_or_insert(ValueRange::SINGLE);
} else { } else {
self.num_vals.get_or_insert(0.into()); self.num_vals.get_or_insert(ValueRange::EMPTY);
} }
} }
} }

View file

@ -3,6 +3,7 @@ use std::cmp::Ordering;
use clap_lex::RawOsStr; use clap_lex::RawOsStr;
use crate::builder::arg::ArgProvider; use crate::builder::arg::ArgProvider;
use crate::builder::ValueRange;
use crate::mkeymap::KeyType; use crate::mkeymap::KeyType;
use crate::ArgAction; use crate::ArgAction;
use crate::INTERNAL_ERROR_MSG; 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); let num_vals = arg.get_num_args().expect(INTERNAL_ERROR_MSG);
// This can be the cause of later asserts, so put this first // 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 // HACK: Don't check for flags to make the derive easier
let num_val_names = arg.get_value_names().unwrap_or(&[]).len(); let num_val_names = arg.get_value_names().unwrap_or(&[]).len();
if num_vals.max_values() < num_val_names { 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!( assert!(
!arg.is_multiple_values_set(), !arg.is_multiple_values_set(),
"Argument {}: mismatch between `num_args` and `multiple_values`", "Argument {}: mismatch between `num_args` and `multiple_values`",

View file

@ -6,6 +6,18 @@ pub struct ValueRange {
} }
impl 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 /// Create a range
/// ///
/// # Panics /// # Panics
@ -96,7 +108,7 @@ impl std::ops::RangeBounds<usize> for ValueRange {
impl Default for ValueRange { impl Default for ValueRange {
fn default() -> Self { fn default() -> Self {
0.into() Self::EMPTY
} }
} }