Skip to content

Commit

Permalink
fix!: Rename ArgValue to PossibleValue
Browse files Browse the repository at this point in the history
In considering potential work for clap-rs#2683, I realized we might need a type to carry data for
each of the `multiple_values`.  `ArgValue` works both for that and for
possible values, so we need to come up with a better name for one or
both.  Changing `ArgValue`s name now would be ideal since its new in
clap3 and by renaming it, we can reduce churn for users.

While thinking about this, I realized I regularly get these mixed
up, so renaming `ArgValue` to `PossibleValue` I think will help clear
things up, regardless of clap-rs#2683.
  • Loading branch information
epage committed Oct 19, 2021
1 parent fe29af7 commit 9f12bfe
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 91 deletions.
16 changes: 8 additions & 8 deletions clap_derive/src/derives/arg_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStr
);

let lits = lits(&e.variants, &attrs);
let arg_values = gen_arg_values(&lits);
let to_arg_value = gen_to_arg_value(&lits);
let value_variants = gen_value_variants(&lits);
let to_possible_value = gen_to_possible_value(&lits);

quote! {
#[allow(dead_code, unreachable_code, unused_variables)]
Expand All @@ -64,8 +64,8 @@ pub fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStr
)]
#[deny(clippy::correctness)]
impl clap::ArgEnum for #name {
#arg_values
#to_arg_value
#value_variants
#to_possible_value
}
}
}
Expand All @@ -89,7 +89,7 @@ fn lits(
let name = attrs.cased_name();
Some((
quote! {
clap::ArgValue::new(#name)
clap::PossibleValue::new(#name)
#fields
},
variant.ident.clone(),
Expand All @@ -99,7 +99,7 @@ fn lits(
.collect::<Vec<_>>()
}

fn gen_arg_values(lits: &[(TokenStream, Ident)]) -> TokenStream {
fn gen_value_variants(lits: &[(TokenStream, Ident)]) -> TokenStream {
let lit = lits.iter().map(|l| &l.1).collect::<Vec<_>>();

quote! {
Expand All @@ -109,11 +109,11 @@ fn gen_arg_values(lits: &[(TokenStream, Ident)]) -> TokenStream {
}
}

fn gen_to_arg_value(lits: &[(TokenStream, Ident)]) -> TokenStream {
fn gen_to_possible_value(lits: &[(TokenStream, Ident)]) -> TokenStream {
let (lit, variant): (Vec<TokenStream>, Vec<Ident>) = lits.iter().cloned().unzip();

quote! {
fn to_arg_value<'a>(&self) -> Option<clap::ArgValue<'a>> {
fn to_possible_value<'a>(&self) -> Option<clap::PossibleValue<'a>> {
match self {
#(Self::#variant => Some(#lit),)*
_ => None
Expand Down
2 changes: 1 addition & 1 deletion clap_derive/src/derives/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ pub fn gen_augment(

fn gen_arg_enum_possible_values(ty: &Type) -> TokenStream {
quote_spanned! { ty.span()=>
.possible_values(<#ty as clap::ArgEnum>::value_variants().iter().filter_map(clap::ArgEnum::to_arg_value))
.possible_values(<#ty as clap::ArgEnum>::value_variants().iter().filter_map(clap::ArgEnum::to_possible_value))
}
}

Expand Down
2 changes: 1 addition & 1 deletion clap_derive/src/dummies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn arg_enum(name: &Ident) {
fn from_str(_input: &str, _case_insensitive: bool) -> Result<Self, String> {
unimplemented!()
}
fn to_arg_value<'a>(&self) -> Option<clap::ArgValue<'a>>{
fn to_possible_value<'a>(&self) -> Option<clap::PossibleValue<'a>>{
unimplemented!()
}
}
Expand Down
8 changes: 4 additions & 4 deletions clap_derive/tests/arg_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use clap::{ArgEnum, ArgValue, Parser};
use clap::{ArgEnum, Parser, PossibleValue};

#[test]
fn basic() {
Expand Down Expand Up @@ -54,7 +54,7 @@ fn default_value() {

impl std::fmt::Display for ArgChoice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
std::fmt::Display::fmt(self.to_arg_value().unwrap().get_name(), f)
std::fmt::Display::fmt(self.to_possible_value().unwrap().get_name(), f)
}
}

Expand Down Expand Up @@ -370,10 +370,10 @@ fn skip_variant() {
assert_eq!(
ArgChoice::value_variants()
.iter()
.map(ArgEnum::to_arg_value)
.map(ArgEnum::to_possible_value)
.map(Option::unwrap)
.collect::<Vec<_>>(),
vec![ArgValue::new("foo"), ArgValue::new("bar")]
vec![PossibleValue::new("foo"), PossibleValue::new("bar")]
);
assert!(ArgChoice::from_str("foo", true).is_ok());
assert!(ArgChoice::from_str("bar", true).is_ok());
Expand Down
2 changes: 1 addition & 1 deletion clap_generate/examples/value_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn build_cli() -> App<'static> {
.arg(
Arg::new("generator")
.long("generate")
.possible_values(Shell::arg_values()),
.possible_values(Shell::possible_values()),
)
.arg(
Arg::new("unknown")
Expand Down
2 changes: 1 addition & 1 deletion clap_generate/src/generators/shells/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn vals_for(o: &Arg) -> String {
format!(
"$(compgen -W \"{}\" -- \"${{cur}}\")",
vals.iter()
.filter_map(ArgValue::get_visible_name)
.filter_map(PossibleValue::get_visible_name)
.collect::<Vec<_>>()
.join(" ")
)
Expand Down
2 changes: 1 addition & 1 deletion clap_generate/src/generators/shells/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ fn value_completion(arg: &Arg) -> Option<String> {
"({})",
values
.iter()
.filter_map(ArgValue::get_visible_name)
.filter_map(PossibleValue::get_visible_name)
.collect::<Vec<_>>()
.join(" ")
))
Expand Down
2 changes: 1 addition & 1 deletion clap_generate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! .arg(
//! Arg::new("generator")
//! .long("generate")
//! .possible_values(Shell::arg_values()),
//! .possible_values(Shell::possible_values()),
//! )
//! }
//!
Expand Down
22 changes: 11 additions & 11 deletions clap_generate/src/shell.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Display;
use std::str::FromStr;

use clap::{ArgEnum, ArgValue};
use clap::{ArgEnum, PossibleValue};

use crate::{generators, Generator};

Expand All @@ -23,16 +23,16 @@ pub enum Shell {

impl Shell {
/// Report all `possible_values`
pub fn arg_values() -> impl Iterator<Item = ArgValue<'static>> {
pub fn possible_values() -> impl Iterator<Item = PossibleValue<'static>> {
Shell::value_variants()
.iter()
.filter_map(ArgEnum::to_arg_value)
.filter_map(ArgEnum::to_possible_value)
}
}

impl Display for Shell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.to_arg_value()
self.to_possible_value()
.expect("no values are skipped")
.get_name()
.fmt(f)
Expand All @@ -44,7 +44,7 @@ impl FromStr for Shell {

fn from_str(s: &str) -> Result<Self, Self::Err> {
for variant in Self::value_variants() {
if variant.to_arg_value().unwrap().matches(s, false) {
if variant.to_possible_value().unwrap().matches(s, false) {
return Ok(*variant);
}
}
Expand All @@ -64,13 +64,13 @@ impl ArgEnum for Shell {
]
}

fn to_arg_value<'a>(&self) -> Option<ArgValue<'a>> {
fn to_possible_value<'a>(&self) -> Option<PossibleValue<'a>> {
Some(match self {
Shell::Bash => ArgValue::new("bash"),
Shell::Elvish => ArgValue::new("elvish"),
Shell::Fish => ArgValue::new("fish"),
Shell::PowerShell => ArgValue::new("powershell"),
Shell::Zsh => ArgValue::new("zsh"),
Shell::Bash => PossibleValue::new("bash"),
Shell::Elvish => PossibleValue::new("elvish"),
Shell::Fish => PossibleValue::new("fish"),
Shell::PowerShell => PossibleValue::new("powershell"),
Shell::Zsh => PossibleValue::new("zsh"),
})
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/build/arg/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
mod arg_value;
#[cfg(debug_assertions)]
pub mod debug_asserts;
mod possible_value;
mod settings;
#[cfg(test)]
mod tests;
mod value_hint;

pub use self::arg_value::ArgValue;
pub use self::possible_value::PossibleValue;
pub use self::settings::{ArgFlags, ArgSettings};
pub use self::value_hint::ValueHint;

Expand Down Expand Up @@ -102,7 +102,7 @@ pub struct Arg<'help> {
pub(crate) short_aliases: Vec<(char, bool)>, // (name, visible)
pub(crate) disp_ord: usize,
pub(crate) unified_ord: usize,
pub(crate) possible_vals: Vec<ArgValue<'help>>,
pub(crate) possible_vals: Vec<PossibleValue<'help>>,
pub(crate) val_names: Vec<&'help str>,
pub(crate) num_vals: Option<usize>,
pub(crate) max_occurs: Option<usize>,
Expand Down Expand Up @@ -231,7 +231,7 @@ impl<'help> Arg<'help> {

/// Get the list of the possible values for this argument, if any
#[inline]
pub fn get_possible_values(&self) -> Option<&[ArgValue]> {
pub fn get_possible_values(&self) -> Option<&[PossibleValue]> {
if self.possible_vals.is_empty() {
None
} else {
Expand Down Expand Up @@ -1959,7 +1959,7 @@ impl<'help> Arg<'help> {
///
/// **NOTE:** This setting only applies to [options] and [positional arguments]
///
/// **NOTE:** You can use both strings directly or use [`ArgValue`] if you want more control
/// **NOTE:** You can use both strings directly or use [`PossibleValue`] if you want more control
/// over single possible values.
///
/// # Examples
Expand All @@ -1971,16 +1971,16 @@ impl<'help> Arg<'help> {
/// .possible_values(["fast", "slow", "medium"])
/// # ;
/// ```
/// The same using [`ArgValue`]:
/// The same using [`PossibleValue`]:
///
/// ```rust
/// # use clap::{App, Arg, ArgValue};
/// # use clap::{App, Arg, PossibleValue};
/// Arg::new("mode").takes_value(true).possible_values([
/// ArgValue::new("fast"),
/// PossibleValue::new("fast"),
/// // value with a help text
/// ArgValue::new("slow").about("not that fast"),
/// PossibleValue::new("slow").about("not that fast"),
/// // value that is hidden from completion and help text
/// ArgValue::new("medium").hidden(true),
/// PossibleValue::new("medium").hidden(true),
/// ])
/// # ;
/// ```
Expand Down Expand Up @@ -2020,7 +2020,7 @@ impl<'help> Arg<'help> {
pub fn possible_values<I, T>(mut self, values: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<ArgValue<'help>>,
T: Into<PossibleValue<'help>>,
{
self.possible_vals
.extend(values.into_iter().map(|value| value.into()));
Expand All @@ -2032,7 +2032,7 @@ impl<'help> Arg<'help> {
///
/// **NOTE:** This setting only applies to [options] and [positional arguments]
///
/// **NOTE:** You can use both strings directly or use [`ArgValue`] if you want more control
/// **NOTE:** You can use both strings directly or use [`PossibleValue`] if you want more control
/// over single possible values.
///
/// # Examples
Expand All @@ -2046,16 +2046,16 @@ impl<'help> Arg<'help> {
/// .possible_value("medium")
/// # ;
/// ```
/// The same using [`ArgValue`]:
/// The same using [`PossibleValue`]:
///
/// ```rust
/// # use clap::{App, Arg, ArgValue};
/// # use clap::{App, Arg, PossibleValue};
/// Arg::new("mode").takes_value(true)
/// .possible_value(ArgValue::new("fast"))
/// .possible_value(PossibleValue::new("fast"))
/// // value with a help text
/// .possible_value(ArgValue::new("slow").about("not that fast"))
/// .possible_value(PossibleValue::new("slow").about("not that fast"))
/// // value that is hidden from completion and help text
/// .possible_value(ArgValue::new("medium").hidden(true))
/// .possible_value(PossibleValue::new("medium").hidden(true))
/// # ;
/// ```
///
Expand Down Expand Up @@ -2097,7 +2097,7 @@ impl<'help> Arg<'help> {
/// [positional arguments]: Arg::index()
pub fn possible_value<T>(mut self, value: T) -> Self
where
T: Into<ArgValue<'help>>,
T: Into<PossibleValue<'help>>,
{
self.possible_vals.push(value.into());
self.takes_value(true)
Expand Down
Loading

0 comments on commit 9f12bfe

Please sign in to comment.