fix!: Remove max_occurrences

This commit is contained in:
Ed Page 2022-07-21 15:26:22 -05:00
parent 137924fe48
commit 0f3e1b17cf
5 changed files with 0 additions and 107 deletions

View file

@ -76,7 +76,6 @@ pub struct Arg<'help> {
pub(crate) possible_vals: Vec<PossibleValue<'help>>, pub(crate) possible_vals: Vec<PossibleValue<'help>>,
pub(crate) val_names: Vec<&'help str>, pub(crate) val_names: Vec<&'help str>,
pub(crate) num_vals: Option<usize>, pub(crate) num_vals: Option<usize>,
pub(crate) max_occurs: Option<usize>,
pub(crate) max_vals: Option<usize>, pub(crate) max_vals: Option<usize>,
pub(crate) min_vals: Option<usize>, pub(crate) min_vals: Option<usize>,
pub(crate) validator: Option<Arc<Mutex<Validator<'help>>>>, pub(crate) validator: Option<Arc<Mutex<Validator<'help>>>>,
@ -780,25 +779,6 @@ impl<'help> Arg<'help> {
} }
} }
/// Deprecated, for flags this is replaced with `action(ArgAction::Count).value_parser(value_parser!(u8).range(..max))`
#[inline]
#[must_use]
#[cfg_attr(
feature = "deprecated",
deprecated(
since = "3.2.0",
note = "For flags, replaced with `action(ArgAction::Count).value_parser(value_parser!(u8).range(..max))`"
)
)]
pub fn max_occurrences(mut self, qty: usize) -> Self {
self.max_occurs = Some(qty);
if qty > 1 {
self.multiple_occurrences(true)
} else {
self
}
}
/// Check if the [`ArgSettings`] variant is currently set on the argument. /// Check if the [`ArgSettings`] variant is currently set on the argument.
/// ///
/// [`ArgSettings`]: crate::ArgSettings /// [`ArgSettings`]: crate::ArgSettings

View file

@ -18,10 +18,6 @@ pub enum ContextKind {
ExpectedNumValues, ExpectedNumValues,
/// Minimum number of allowed values /// Minimum number of allowed values
MinValues, MinValues,
/// Number of occurrences present
ActualNumOccurrences,
/// Maximum number of allowed occurrences
MaxOccurrences,
/// Potential fix for the user /// Potential fix for the user
SuggestedCommand, SuggestedCommand,
/// Potential fix for the user /// Potential fix for the user

View file

@ -175,24 +175,6 @@ pub enum ErrorKind {
/// [`Arg::min_values`]: crate::Arg::min_values() /// [`Arg::min_values`]: crate::Arg::min_values()
TooFewValues, TooFewValues,
/// Occurs when a user provides more occurrences for an argument than were defined by setting
/// [`Arg::max_occurrences`].
///
/// # Examples
///
/// ```rust
/// # use clap::{Command, Arg, ErrorKind};
/// let result = Command::new("prog")
/// .arg(Arg::new("verbosity")
/// .short('v')
/// .max_occurrences(2))
/// .try_get_matches_from(vec!["prog", "-vvv"]);
/// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind(), ErrorKind::TooManyOccurrences);
/// ```
/// [`Arg::max_occurrences`]: crate::Arg::max_occurrences()
TooManyOccurrences,
/// Occurs when the user provides a different number of values for an argument than what's /// Occurs when the user provides a different number of values for an argument than what's
/// been defined by setting [`Arg::number_of_values`] or than was implicitly set by /// been defined by setting [`Arg::number_of_values`] or than was implicitly set by
/// [`Arg::value_names`]. /// [`Arg::value_names`].
@ -403,7 +385,6 @@ impl ErrorKind {
Self::ValueValidation => Some("Invalid value for one of the arguments"), Self::ValueValidation => Some("Invalid value for one of the arguments"),
Self::TooManyValues => Some("An argument received an unexpected value"), Self::TooManyValues => Some("An argument received an unexpected value"),
Self::TooFewValues => Some("An argument requires more values"), Self::TooFewValues => Some("An argument requires more values"),
Self::TooManyOccurrences => Some("An argument occurred too many times"),
Self::WrongNumberOfValues => Some("An argument received too many or too few values"), Self::WrongNumberOfValues => Some("An argument received too many or too few values"),
Self::ArgumentConflict => { Self::ArgumentConflict => {
Some("An argument cannot be used with one or more of the other specified arguments") Some("An argument cannot be used with one or more of the other specified arguments")

View file

@ -352,29 +352,6 @@ impl Error {
.extend_context_unchecked([(ContextKind::Usage, ContextValue::String(usage))]) .extend_context_unchecked([(ContextKind::Usage, ContextValue::String(usage))])
} }
pub(crate) fn too_many_occurrences(
cmd: &Command,
arg: String,
max_occurs: usize,
curr_occurs: usize,
usage: String,
) -> Self {
Self::new(ErrorKind::TooManyOccurrences)
.with_cmd(cmd)
.extend_context_unchecked([
(ContextKind::InvalidArg, ContextValue::String(arg)),
(
ContextKind::MaxOccurrences,
ContextValue::Number(max_occurs as isize),
),
(
ContextKind::ActualNumValues,
ContextValue::Number(curr_occurs as isize),
),
(ContextKind::Usage, ContextValue::String(usage)),
])
}
pub(crate) fn too_many_values(cmd: &Command, val: String, arg: String, usage: String) -> Self { pub(crate) fn too_many_values(cmd: &Command, val: String, arg: String, usage: String) -> Self {
Self::new(ErrorKind::TooManyValues) Self::new(ErrorKind::TooManyValues)
.with_cmd(cmd) .with_cmd(cmd)
@ -689,29 +666,6 @@ impl Error {
} }
} }
ErrorKind::InvalidUtf8 => false, ErrorKind::InvalidUtf8 => false,
ErrorKind::TooManyOccurrences => {
let invalid_arg = self.get_context(ContextKind::InvalidArg);
let actual_num_occurs = self.get_context(ContextKind::ActualNumOccurrences);
let max_occurs = self.get_context(ContextKind::MaxOccurrences);
if let (
Some(ContextValue::String(invalid_arg)),
Some(ContextValue::Number(actual_num_occurs)),
Some(ContextValue::Number(max_occurs)),
) = (invalid_arg, actual_num_occurs, max_occurs)
{
let were_provided = Error::singular_or_plural(*actual_num_occurs as usize);
c.none("The argument '");
c.warning(invalid_arg);
c.none("' allows at most ");
c.warning(max_occurs.to_string());
c.none(" occurrences but ");
c.warning(actual_num_occurs.to_string());
c.none(were_provided);
true
} else {
false
}
}
ErrorKind::TooManyValues => { ErrorKind::TooManyValues => {
let invalid_arg = self.get_context(ContextKind::InvalidArg); let invalid_arg = self.get_context(ContextKind::InvalidArg);
let invalid_value = self.get_context(ContextKind::InvalidValue); let invalid_value = self.get_context(ContextKind::InvalidValue);

View file

@ -333,24 +333,6 @@ impl<'help, 'cmd> Validator<'help, 'cmd> {
.create_usage_with_title(&[]), .create_usage_with_title(&[]),
)); ));
} }
if let Some(max_occurs) = a.max_occurs {
debug!(
"Validator::validate_arg_num_occurs: max_occurs set...{}",
max_occurs
);
let occurs = ma.get_occurrences() as usize;
if occurs > max_occurs {
return Err(Error::too_many_occurrences(
self.cmd,
a.to_string(),
max_occurs,
occurs,
Usage::new(self.cmd)
.required(&self.required)
.create_usage_with_title(&[]),
));
}
}
Ok(()) Ok(())
} }