fix!: Remove possible_values

This commit is contained in:
Ed Page 2022-07-21 15:54:26 -05:00
parent d7618c79af
commit f10809f0cc
8 changed files with 8 additions and 127 deletions

View file

@ -128,13 +128,8 @@ pub fn flags<'help>(p: &Command<'help>) -> Vec<Arg<'help>> {
/// Get the possible values for completion
pub fn possible_values<'help>(a: &Arg<'help>) -> Option<Vec<clap::PossibleValue<'help>>> {
#![allow(deprecated)]
if !a.is_takes_value_set() {
None
} else if let Some(pvs) = a.get_possible_values() {
// Check old first in case the user explicitly set possible values and the derive inferred
// a `ValueParser` with some.
Some(pvs.to_vec())
} else {
a.get_value_parser()
.possible_values()

View file

@ -73,7 +73,6 @@ pub struct Arg<'help> {
pub(crate) aliases: Vec<(&'help str, bool)>, // (name, visible)
pub(crate) short_aliases: Vec<(char, bool)>, // (name, visible)
pub(crate) disp_ord: DisplayOrder,
pub(crate) possible_vals: Vec<PossibleValue<'help>>,
pub(crate) val_names: Vec<&'help str>,
pub(crate) num_vals: Option<usize>,
pub(crate) max_vals: Option<usize>,
@ -1509,43 +1508,7 @@ impl<'help> Arg<'help> {
self
}
/// Deprecated, replaced with [`Arg::value_parser(PossibleValuesParser::new(...))`]
#[cfg_attr(
feature = "deprecated",
deprecated(
since = "3.2.0",
note = "Replaced with `Arg::value_parser(PossibleValuesParser::new(...)).takes_value(true)`"
)
)]
#[must_use]
pub fn possible_value<T>(mut self, value: T) -> Self
where
T: Into<PossibleValue<'help>>,
{
self.possible_vals.push(value.into());
self.takes_value(true)
}
/// Deprecated, replaced with [`Arg::value_parser(PossibleValuesParser::new(...))`]
#[cfg_attr(
feature = "deprecated",
deprecated(
since = "3.2.0",
note = "Replaced with `Arg::value_parser(PossibleValuesParser::new(...)).takes_value(true)`"
)
)]
#[must_use]
pub fn possible_values<I, T>(mut self, values: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<PossibleValue<'help>>,
{
self.possible_vals
.extend(values.into_iter().map(|value| value.into()));
self.takes_value(true)
}
/// Match values against [`Arg::possible_values`] without matching case.
/// Match values against [`PossibleValuesParser`][crate::builder::PossibleValuesParser] without matching case.
///
/// When other arguments are conditionally required based on the
/// value of a case-insensitive argument, the equality check done
@ -4249,30 +4212,9 @@ impl<'help> Arg<'help> {
Some(longs)
}
/// Deprecated, replaced with [`Arg::get_value_parser().possible_values()`]
#[cfg_attr(
feature = "deprecated",
deprecated(
since = "3.2.0",
note = "Replaced with `Arg::get_value_parser().possible_values()`"
)
)]
pub fn get_possible_values(&self) -> Option<&[PossibleValue<'help>]> {
if self.possible_vals.is_empty() {
None
} else {
Some(&self.possible_vals)
}
}
pub(crate) fn get_possible_values2(&self) -> Vec<PossibleValue<'help>> {
#![allow(deprecated)]
pub(crate) fn get_possible_values(&self) -> Vec<PossibleValue<'help>> {
if !self.is_takes_value_set() {
vec![]
} else if let Some(pvs) = self.get_possible_values() {
// Check old first in case the user explicitly set possible values and the derive inferred
// a `ValueParser` with some.
pvs.to_vec()
} else {
self.get_value_parser()
.possible_values()
@ -4721,7 +4663,6 @@ impl<'help> fmt::Debug for Arg<'help> {
.field("aliases", &self.aliases)
.field("short_aliases", &self.short_aliases)
.field("disp_ord", &self.disp_ord)
.field("possible_vals", &self.possible_vals)
.field("val_names", &self.val_names)
.field("num_vals", &self.num_vals)
.field("max_vals", &self.max_vals)

View file

@ -4675,7 +4675,7 @@ impl<'help> Command<'help> {
|| v.is_hide_long_help_set()
|| v.is_hide_short_help_set()
|| cfg!(feature = "unstable-v4")
&& v.get_possible_values2()
&& v.get_possible_values()
.iter()
.any(PossibleValue::should_show_help)
};

View file

@ -754,32 +754,6 @@ fn assert_defaults<'d>(
) {
for default_os in defaults {
if let Some(default_s) = default_os.to_str() {
if !arg.possible_vals.is_empty() {
if let Some(delim) = arg.get_value_delimiter() {
for part in default_s.split(delim) {
assert!(
arg.possible_vals.iter().any(|possible_val| {
possible_val.matches(part, arg.is_ignore_case_set())
}),
"Argument `{}`'s {}={} doesn't match possible values",
arg.name,
field,
part
)
}
} else {
assert!(
arg.possible_vals.iter().any(|possible_val| {
possible_val.matches(default_s, arg.is_ignore_case_set())
}),
"Argument `{}`'s {}={} doesn't match possible values",
arg.name,
field,
default_s
);
}
}
if let Some(validator) = arg.validator.as_ref() {
let mut validator = validator.lock().unwrap();
if let Some(delim) = arg.get_value_delimiter() {

View file

@ -50,7 +50,7 @@ impl<'help> PossibleValue<'help> {
/// # ;
/// ```
/// [hidden]: PossibleValue::hide
/// [possible value]: crate::Arg::possible_values
/// [possible value]: crate::builder::PossibleValuesParser
/// [`Arg::hide_possible_values(true)`]: crate::Arg::hide_possible_values()
pub fn new(name: &'help str) -> Self {
PossibleValue {

View file

@ -341,7 +341,7 @@ pub trait Subcommand: FromArgMatches + Sized {
///
/// When deriving [`Parser`], a field whose type implements `ValueEnum` can have the attribute
/// `#[clap(value_enum)]` which will
/// - Call [`Arg::possible_values`][crate::Arg::possible_values]
/// - Call [EnumValueParser`][crate::builder::EnumValueParser]`
/// - Allowing using the `#[clap(default_value_t)]` attribute without implementing `Display`.
///
/// See the [derive reference](crate::_derive) for attributes and best practices.
@ -354,7 +354,7 @@ pub trait Subcommand: FromArgMatches + Sized {
#[cfg_attr(feature = "derive", doc = " ```")]
/// #[derive(clap::Parser)]
/// struct Args {
/// #[clap(value_enum)]
/// #[clap(value_enum, value_parser)]
/// level: Level,
/// }
///

View file

@ -481,7 +481,7 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> {
if let Some(arg) = arg {
const DASH_SPACE: usize = "- ".len();
const COLON_SPACE: usize = ": ".len();
let possible_vals = arg.get_possible_values2();
let possible_vals = arg.get_possible_values();
if self.use_long
&& !arg.is_hide_possible_values_set()
&& possible_vals.iter().any(PossibleValue::should_show_help)
@ -660,7 +660,7 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> {
}
}
let possible_vals = a.get_possible_values2();
let possible_vals = a.get_possible_values();
if !(a.is_hide_possible_values_set()
|| possible_vals.is_empty()
|| cfg!(feature = "unstable-v4")

View file

@ -91,30 +91,6 @@ impl<'help, 'cmd> Validator<'help, 'cmd> {
fn validate_arg_values(&self, arg: &Arg, ma: &MatchedArg) -> ClapResult<()> {
debug!("Validator::validate_arg_values: arg={:?}", arg.name);
for val in ma.raw_vals_flatten() {
if !arg.possible_vals.is_empty() {
debug!(
"Validator::validate_arg_values: possible_vals={:?}",
arg.possible_vals
);
let val_str = val.to_string_lossy();
let ok = arg
.possible_vals
.iter()
.any(|pv| pv.matches(&val_str, arg.is_ignore_case_set()));
if !ok {
return Err(Error::invalid_value(
self.cmd,
val_str.into_owned(),
&arg.possible_vals
.iter()
.filter(|pv| !pv.is_hide_set())
.map(PossibleValue::get_name)
.collect::<Vec<_>>(),
arg.to_string(),
));
}
}
if let Some(ref vtor) = arg.validator {
debug!("Validator::validate_arg_values: checking validator...");
let mut vtor = vtor.lock().unwrap();
@ -643,13 +619,8 @@ impl Conflicts {
}
fn get_possible_values<'help>(a: &Arg<'help>) -> Vec<PossibleValue<'help>> {
#![allow(deprecated)]
if !a.is_takes_value_set() {
vec![]
} else if let Some(pvs) = a.get_possible_values() {
// Check old first in case the user explicitly set possible values and the derive inferred
// a `ValueParser` with some.
pvs.to_vec()
} else {
a.get_value_parser()
.possible_values()