From 8ed35b4d9f436611fed16db3927f2adadfefc4e5 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 5 Aug 2022 12:06:51 -0500 Subject: [PATCH] fix: Provide convenient access for common cases --- clap_complete/src/shells/zsh.rs | 15 ++++++++------- clap_complete_fig/src/fig.rs | 2 +- src/builder/arg.rs | 4 ++-- src/builder/debug_asserts.rs | 5 +++-- src/builder/range.rs | 14 +++++++++++++- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/clap_complete/src/shells/zsh.rs b/clap_complete/src/shells/zsh.rs index 1dae45aa05e..707cae24813 100644 --- a/clap_complete/src/shells/zsh.rs +++ b/clap_complete/src/shells/zsh.rs @@ -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}' \\", diff --git a/clap_complete_fig/src/fig.rs b/clap_complete_fig/src/fig.rs index d0cc0b2c7f6..1c5f6bbb9fd 100644 --- a/clap_complete_fig/src/fig.rs +++ b/clap_complete_fig/src/fig.rs @@ -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", "", diff --git a/src/builder/arg.rs b/src/builder/arg.rs index 7b2748b7a96..fa7bb4af5c6 100644 --- a/src/builder/arg.rs +++ b/src/builder/arg.rs @@ -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); } } } diff --git a/src/builder/debug_asserts.rs b/src/builder/debug_asserts.rs index 0e29e8024ef..88fa52b92d0 100644 --- a/src/builder/debug_asserts.rs +++ b/src/builder/debug_asserts.rs @@ -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`", diff --git a/src/builder/range.rs b/src/builder/range.rs index e30b0dd73f1..808a6f34ce1 100644 --- a/src/builder/range.rs +++ b/src/builder/range.rs @@ -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 for ValueRange { impl Default for ValueRange { fn default() -> Self { - 0.into() + Self::EMPTY } }