mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
Adjust derive
This commit is contained in:
parent
e9759a241b
commit
e6cc49ecde
4 changed files with 12 additions and 29 deletions
|
@ -65,9 +65,7 @@ pub fn gen_for_enum(name: &Ident) -> TokenStream {
|
||||||
#[deny(clippy::correctness)]
|
#[deny(clippy::correctness)]
|
||||||
impl ::clap::FromArgMatches for #name {
|
impl ::clap::FromArgMatches for #name {
|
||||||
fn from_arg_matches(matches: &::clap::ArgMatches) -> Self {
|
fn from_arg_matches(matches: &::clap::ArgMatches) -> Self {
|
||||||
let (name, subcmd) = matches.subcommand();
|
<#name as ::clap::Subcommand>::from_subcommand(matches.subcommand()).unwrap()
|
||||||
<#name as ::clap::Subcommand>::from_subcommand(name, subcmd)
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,11 +105,7 @@ pub fn gen_constructor(fields: &Punctuated<Field, Comma>, parent_attribute: &Att
|
||||||
};
|
};
|
||||||
quote_spanned! { kind.span()=>
|
quote_spanned! { kind.span()=>
|
||||||
#field_name: {
|
#field_name: {
|
||||||
let (name, subcmd) = matches.subcommand();
|
<#subcmd_type as ::clap::Subcommand>::from_subcommand(matches.subcommand())
|
||||||
<#subcmd_type as ::clap::Subcommand>::from_subcommand(
|
|
||||||
name,
|
|
||||||
subcmd
|
|
||||||
)
|
|
||||||
#unwrapper
|
#unwrapper
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -238,7 +238,7 @@ fn gen_from_subcommand(
|
||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
(#sub_name, Some(matches)) => {
|
Some((#sub_name, matches)) => {
|
||||||
Some(#name :: #variant_name #constructor_block)
|
Some(#name :: #variant_name #constructor_block)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -249,7 +249,7 @@ fn gen_from_subcommand(
|
||||||
Unnamed(ref fields) if fields.unnamed.len() == 1 => {
|
Unnamed(ref fields) if fields.unnamed.len() == 1 => {
|
||||||
let ty = &fields.unnamed[0];
|
let ty = &fields.unnamed[0];
|
||||||
quote! {
|
quote! {
|
||||||
if let Some(res) = <#ty as ::clap::Subcommand>::from_subcommand(other.0, other.1) {
|
if let Some(res) = <#ty as ::clap::Subcommand>::from_subcommand(other) {
|
||||||
return Some(#name :: #variant_name (res));
|
return Some(#name :: #variant_name (res));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -263,9 +263,9 @@ fn gen_from_subcommand(
|
||||||
|
|
||||||
let wildcard = match ext_subcmd {
|
let wildcard = match ext_subcmd {
|
||||||
Some((span, var_name, str_ty, values_of)) => quote_spanned! { span=>
|
Some((span, var_name, str_ty, values_of)) => quote_spanned! { span=>
|
||||||
("", ::std::option::Option::None) => ::std::option::Option::None,
|
None => ::std::option::Option::None,
|
||||||
|
|
||||||
(external, Some(matches)) => {
|
Some((external, matches)) => {
|
||||||
::std::option::Option::Some(#name::#var_name(
|
::std::option::Option::Some(#name::#var_name(
|
||||||
::std::iter::once(#str_ty::from(external))
|
::std::iter::once(#str_ty::from(external))
|
||||||
.chain(
|
.chain(
|
||||||
|
@ -274,25 +274,14 @@ fn gen_from_subcommand(
|
||||||
.collect::<::std::vec::Vec<_>>()
|
.collect::<::std::vec::Vec<_>>()
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
(external, None) => {
|
|
||||||
::std::option::Option::Some(#name::#var_name({
|
|
||||||
let mut v = ::std::vec::Vec::with_capacity(1);
|
|
||||||
v.push(#str_ty::from(external));
|
|
||||||
v
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
None => quote!(_ => None),
|
None => quote!(_ => None),
|
||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
fn from_subcommand<'b>(
|
fn from_subcommand<'b>(subcommand: Option<(&'b str, &'b ::clap::ArgMatches)>) -> Option<Self> {
|
||||||
name: &'b str,
|
match subcommand {
|
||||||
sub: Option<&'b ::clap::ArgMatches>) -> Option<Self>
|
|
||||||
{
|
|
||||||
match (name, sub) {
|
|
||||||
#( #match_arms, )*
|
#( #match_arms, )*
|
||||||
other => {
|
other => {
|
||||||
#( #child_subcommands )else*
|
#( #child_subcommands )else*
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub fn from_arg_matches(name: &Ident) {
|
||||||
pub fn subcommand(name: &Ident) {
|
pub fn subcommand(name: &Ident) {
|
||||||
append_dummy(quote! {
|
append_dummy(quote! {
|
||||||
impl ::clap::Subcommand for #name {
|
impl ::clap::Subcommand for #name {
|
||||||
fn from_subcommand(_name: &str, _matches: Option<&::clap::ArgMatches>) -> Option<Self> {
|
fn from_subcommand<'b>(_sub: Option<(&'b str, &'b ::clap::ArgMatches)>) -> Option<Self> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
fn augment_subcommands(_app: ::clap::App<'_>) -> ::clap::App<'_> {
|
fn augment_subcommands(_app: ::clap::App<'_>) -> ::clap::App<'_> {
|
||||||
|
|
|
@ -60,7 +60,7 @@ pub trait FromArgMatches: Sized {
|
||||||
/// @TODO @release @docs
|
/// @TODO @release @docs
|
||||||
pub trait Subcommand: Sized {
|
pub trait Subcommand: Sized {
|
||||||
/// @TODO @release @docs
|
/// @TODO @release @docs
|
||||||
fn from_subcommand(name: &str, matches: Option<&ArgMatches>) -> Option<Self>;
|
fn from_subcommand(subcommand: Option<(&str, &ArgMatches)>) -> Option<Self>;
|
||||||
/// @TODO @release @docs
|
/// @TODO @release @docs
|
||||||
fn augment_subcommands(app: App<'_>) -> App<'_>;
|
fn augment_subcommands(app: App<'_>) -> App<'_>;
|
||||||
}
|
}
|
||||||
|
@ -118,8 +118,8 @@ impl<T: FromArgMatches> FromArgMatches for Box<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Subcommand> Subcommand for Box<T> {
|
impl<T: Subcommand> Subcommand for Box<T> {
|
||||||
fn from_subcommand(name: &str, matches: Option<&ArgMatches>) -> Option<Self> {
|
fn from_subcommand(subcommand: Option<(&str, &ArgMatches)>) -> Option<Self> {
|
||||||
<T as Subcommand>::from_subcommand(name, matches).map(Box::new)
|
<T as Subcommand>::from_subcommand(subcommand).map(Box::new)
|
||||||
}
|
}
|
||||||
fn augment_subcommands(app: App<'_>) -> App<'_> {
|
fn augment_subcommands(app: App<'_>) -> App<'_> {
|
||||||
<T as Subcommand>::augment_subcommands(app)
|
<T as Subcommand>::augment_subcommands(app)
|
||||||
|
|
Loading…
Reference in a new issue