Skip to content

Commit

Permalink
fix: Provide convenient access for common cases
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 5, 2022
1 parent c1468d7 commit 8ed35b4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
15 changes: 8 additions & 7 deletions clap_complete/src/shells/zsh.rs
Expand Up @@ -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}' \\",
Expand Down
2 changes: 1 addition & 1 deletion clap_complete_fig/src/fig.rs
Expand Up @@ -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",
"",
Expand Down
4 changes: 2 additions & 2 deletions src/builder/arg.rs
Expand Up @@ -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);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/builder/debug_asserts.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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`",
Expand Down
14 changes: 13 additions & 1 deletion src/builder/range.rs
Expand Up @@ -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
Expand Down Expand Up @@ -96,7 +108,7 @@ impl std::ops::RangeBounds<usize> for ValueRange {

impl Default for ValueRange {
fn default() -> Self {
0.into()
Self::EMPTY
}
}

Expand Down

0 comments on commit 8ed35b4

Please sign in to comment.