Skip to content

Commit

Permalink
style(derive): Clean up to make explicit methods easier
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 6, 2022
1 parent 9c85839 commit ede8fd6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
8 changes: 6 additions & 2 deletions clap_derive/src/derives/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,18 @@ pub fn gen_augment(
if override_required {
Some(quote_spanned! { kind.span()=>
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::builder::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = #app_var
#next_help_heading
#next_display_order;
let #app_var = <#ty as clap::Args>::augment_args_for_update(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));
})
} else {
Some(quote_spanned! { kind.span()=>
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::builder::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = #app_var
#next_help_heading
#next_display_order;
let #app_var = <#ty as clap::Args>::augment_args(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));
})
Expand Down
33 changes: 19 additions & 14 deletions clap_derive/src/derives/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ fn gen_augment(
let kind = item.kind();

match &*kind {
Kind::Skip(_, _) => None,
Kind::Skip(_, _) |
Kind::Arg(_) |
Kind::FromGlobal(_) |
Kind::Value => None,

Kind::ExternalSubcommand => {
let ty = match variant.fields {
Expand All @@ -155,19 +158,17 @@ fn gen_augment(
} else {
quote!()
};
let subcommand = match subty_if_name(ty, "Vec") {
Some(subty) => {
quote_spanned! { kind.span()=>
#deprecations
let #app_var = #app_var.external_subcommand_value_parser(clap::value_parser!(#subty));
}
}

None => abort!(
let subty = subty_if_name(ty, "Vec").unwrap_or_else(|| {
abort!(
ty.span(),
"The type must be `Vec<_>` \
to be used with `external_subcommand`."
),
)
});
let subcommand = quote_spanned! { kind.span()=>
#deprecations
let #app_var = #app_var
.external_subcommand_value_parser(clap::value_parser!(#subty));
};
Some(subcommand)
}
Expand All @@ -187,15 +188,19 @@ fn gen_augment(
quote! {
#deprecations
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::builder::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = #app_var
#next_help_heading
#next_display_order;
let #app_var = <#ty as clap::Subcommand>::augment_subcommands_for_update(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));
}
} else {
quote! {
#deprecations
let #old_heading_var = #app_var.get_next_help_heading().map(|s| clap::builder::Str::from(s.to_owned()));
let #app_var = #app_var #next_help_heading #next_display_order;
let #app_var = #app_var
#next_help_heading
#next_display_order;
let #app_var = <#ty as clap::Subcommand>::augment_subcommands(#app_var);
let #app_var = #app_var.next_help_heading(clap::builder::Resettable::from(#old_heading_var));
}
Expand Down Expand Up @@ -259,7 +264,7 @@ fn gen_augment(
Some(subcommand)
}

_ => {
Kind::Command(_) => {
let subcommand_var = Ident::new("__clap_subcommand", Span::call_site());
let sub_augment = match variant.fields {
Named(ref fields) => {
Expand Down

0 comments on commit ede8fd6

Please sign in to comment.