mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
address epage's remarks
This commit is contained in:
parent
76f7211d8b
commit
ac1a9d6d13
7 changed files with 23 additions and 24 deletions
|
@ -47,9 +47,9 @@ pub fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStr
|
|||
);
|
||||
|
||||
let lits = lits(&e.variants, &attrs);
|
||||
let variants = gen_variants(&lits);
|
||||
let arg_values = gen_arg_values(&lits);
|
||||
let from_str = gen_from_str(&lits);
|
||||
let as_arg = gen_as_arg(&lits);
|
||||
let arg_value = gen_arg_value(&lits);
|
||||
|
||||
quote! {
|
||||
#[allow(dead_code, unreachable_code, unused_variables)]
|
||||
|
@ -65,9 +65,9 @@ pub fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStr
|
|||
)]
|
||||
#[deny(clippy::correctness)]
|
||||
impl clap::ArgEnum for #name {
|
||||
#variants
|
||||
#arg_values
|
||||
#from_str
|
||||
#as_arg
|
||||
#arg_value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -101,11 +101,11 @@ fn lits(
|
|||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
fn gen_variants(lits: &[(TokenStream, Ident)]) -> TokenStream {
|
||||
fn gen_arg_values(lits: &[(TokenStream, Ident)]) -> TokenStream {
|
||||
let lit = lits.iter().map(|l| &l.0).collect::<Vec<_>>();
|
||||
|
||||
quote! {
|
||||
fn variants() -> Vec<clap::ArgValue<'static>> {
|
||||
fn arg_values() -> Vec<clap::ArgValue<'static>> {
|
||||
vec![#(#lit),*]
|
||||
}
|
||||
}
|
||||
|
@ -124,11 +124,11 @@ fn gen_from_str(lits: &[(TokenStream, Ident)]) -> TokenStream {
|
|||
}
|
||||
}
|
||||
|
||||
fn gen_as_arg(lits: &[(TokenStream, Ident)]) -> TokenStream {
|
||||
fn gen_arg_value(lits: &[(TokenStream, Ident)]) -> TokenStream {
|
||||
let (lit, variant): (Vec<TokenStream>, Vec<Ident>) = lits.iter().cloned().unzip();
|
||||
|
||||
quote! {
|
||||
fn as_arg(&self) -> Option<clap::ArgValue<'static>> {
|
||||
fn arg_value(&self) -> Option<clap::ArgValue<'static>> {
|
||||
match self {
|
||||
#(Self::#variant => Some(#lit),)*
|
||||
_ => None
|
||||
|
|
|
@ -361,7 +361,7 @@ pub fn gen_augment(
|
|||
|
||||
fn gen_arg_enum_possible_values(ty: &Type) -> TokenStream {
|
||||
quote_spanned! { ty.span()=>
|
||||
.possible_values(<#ty as clap::ArgEnum>::variants())
|
||||
.possible_values(<#ty as clap::ArgEnum>::arg_values())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -76,13 +76,13 @@ pub fn args(name: &Ident) {
|
|||
pub fn arg_enum(name: &Ident) {
|
||||
append_dummy(quote! {
|
||||
impl clap::ArgEnum for #name {
|
||||
fn variants() -> Vec<clap::ArgValue<'static>> {
|
||||
fn arg_values() -> Vec<clap::ArgValue<'static>> {
|
||||
unimplemented!()
|
||||
}
|
||||
fn from_str(_input: &str, _case_insensitive: bool) -> Result<Self, String> {
|
||||
unimplemented!()
|
||||
}
|
||||
fn as_arg(&self) -> Option<clap::ArgValue<'static>> {
|
||||
fn arg_value(&self) -> Option<clap::ArgValue<'static>> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.as_arg().unwrap().get_name(), f)
|
||||
std::fmt::Display::fmt(self.arg_value().unwrap().get_name(), f)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -368,7 +368,7 @@ fn skip_variant() {
|
|||
}
|
||||
|
||||
assert_eq!(
|
||||
ArgChoice::variants(),
|
||||
ArgChoice::arg_values(),
|
||||
vec![ArgValue::new("foo"), ArgValue::new("bar")]
|
||||
);
|
||||
assert!(ArgChoice::from_str("foo", true).is_ok());
|
||||
|
|
|
@ -4,5 +4,5 @@ use clap::ArgEnum;
|
|||
struct Opt {}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", Opt::VARIANTS);
|
||||
println!("{:?}", Opt::arg_values());
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use std::iter;
|
||||
|
||||
/// The representation of a possible value of an argument.
|
||||
///
|
||||
/// This is used for specifying [possible values] of [Args].
|
||||
|
@ -20,7 +22,7 @@
|
|||
/// [possible values]: crate::Arg::possible_value()
|
||||
/// [hide]: ArgValue::hidden()
|
||||
/// [about]: ArgValue::about()
|
||||
#[derive(Debug, Default, Clone, PartialEq)]
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||
pub struct ArgValue<'help> {
|
||||
pub(crate) name: &'help str,
|
||||
pub(crate) about: Option<&'help str>,
|
||||
|
@ -64,20 +66,17 @@ impl<'help> ArgValue<'help> {
|
|||
}
|
||||
|
||||
/// TODO
|
||||
pub fn get_name_and_aliases(&self) -> Vec<&str> {
|
||||
[self.name].iter().chain(&self.aliases).copied().collect()
|
||||
pub fn get_name_and_aliases(&self) -> impl Iterator<Item = &str> {
|
||||
iter::once(&self.name).chain(&self.aliases).copied()
|
||||
}
|
||||
|
||||
/// TODO
|
||||
pub fn matches(&self, value: &str, ignore_case: bool) -> bool {
|
||||
if ignore_case {
|
||||
self.get_name_and_aliases()
|
||||
.iter()
|
||||
.any(|name| name.eq_ignore_ascii_case(value))
|
||||
} else {
|
||||
self.get_name_and_aliases()
|
||||
.iter()
|
||||
.any(|name| name == &value)
|
||||
self.get_name_and_aliases().any(|name| name == value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -282,8 +282,8 @@ pub trait Subcommand: FromArgMatches + Sized {
|
|||
/// }
|
||||
/// ```
|
||||
pub trait ArgEnum: Sized {
|
||||
/// All possible argument choices, in display order.
|
||||
fn variants() -> Vec<ArgValue<'static>>;
|
||||
/// All possible argument values, in display order.
|
||||
fn arg_values() -> Vec<ArgValue<'static>>;
|
||||
|
||||
/// Parse an argument into `Self`.
|
||||
fn from_str(input: &str, case_insensitive: bool) -> Result<Self, String>;
|
||||
|
@ -291,7 +291,7 @@ pub trait ArgEnum: Sized {
|
|||
/// The canonical argument value.
|
||||
///
|
||||
/// The value is `None` for skipped variants.
|
||||
fn as_arg(&self) -> Option<ArgValue<'static>>;
|
||||
fn arg_value(&self) -> Option<ArgValue<'static>>;
|
||||
}
|
||||
|
||||
impl<T: Clap> Clap for Box<T> {
|
||||
|
|
Loading…
Reference in a new issue