From 74799ea916745a693aea77784f007cb54d502235 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 1 Oct 2021 16:59:09 -0500 Subject: [PATCH] Revert structopt #325 https://github.com/TeXitoi/structopt/pull/325 special cased `version` because a default method would be added if the user did nothing, which caused problems when nesting subcommands. We no longer apply that default method and the highest item in the chain always has precedence, so this can be simplified / clarified. --- clap_derive/src/attrs.rs | 10 ++-------- clap_derive/src/derives/args.rs | 4 +--- clap_derive/src/derives/subcommand.rs | 10 +++------- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/clap_derive/src/attrs.rs b/clap_derive/src/attrs.rs index 64ccd60e040..c05512eda06 100644 --- a/clap_derive/src/attrs.rs +++ b/clap_derive/src/attrs.rs @@ -764,11 +764,12 @@ impl Attrs { /// generate methods from attributes on top of struct or enum pub fn top_level_methods(&self) -> TokenStream { + let version = &self.version; let author = &self.author; let methods = &self.methods; let doc_comment = &self.doc_comment; - quote!( #(#doc_comment)* #author #(#methods)*) + quote!( #(#doc_comment)* #author #version #(#methods)*) } /// generate methods on top of a field @@ -778,13 +779,6 @@ impl Attrs { quote!( #(#doc_comment)* #(#methods)* ) } - pub fn version(&self) -> TokenStream { - self.version - .clone() - .map(|m| m.to_token_stream()) - .unwrap_or_default() - } - pub fn cased_name(&self) -> TokenStream { self.name.clone().translate(*self.casing) } diff --git a/clap_derive/src/derives/args.rs b/clap_derive/src/derives/args.rs index dc0d26e7c04..ad042876b21 100644 --- a/clap_derive/src/derives/args.rs +++ b/clap_derive/src/derives/args.rs @@ -350,12 +350,10 @@ pub fn gen_augment( }); let app_methods = parent_attribute.top_level_methods(); - let version = parent_attribute.version(); quote! {{ #( #args )* #subcmd - let #app_var = #app_var#app_methods; - #app_var#version + #app_var#app_methods }} } diff --git a/clap_derive/src/derives/subcommand.rs b/clap_derive/src/derives/subcommand.rs index 673f7c43904..afca1d2efa6 100644 --- a/clap_derive/src/derives/subcommand.rs +++ b/clap_derive/src/derives/subcommand.rs @@ -215,13 +215,12 @@ fn gen_augment( let name = attrs.cased_name(); let from_attrs = attrs.top_level_methods(); - let version = attrs.version(); let subcommand = quote! { let app = app.subcommand({ let #app_var = clap::App::new(#name); let #app_var = #arg_block; let #app_var = #app_var.setting(::clap::AppSettings::SubcommandRequiredElseHelp); - #app_var#from_attrs#version + #app_var#from_attrs }); }; Some(subcommand) @@ -257,12 +256,11 @@ fn gen_augment( let name = attrs.cased_name(); let from_attrs = attrs.top_level_methods(); - let version = attrs.version(); let subcommand = quote! { let app = app.subcommand({ let #app_var = clap::App::new(#name); let #app_var = #arg_block; - #app_var#from_attrs#version + #app_var#from_attrs }); }; Some(subcommand) @@ -272,11 +270,9 @@ fn gen_augment( .collect(); let app_methods = parent_attribute.top_level_methods(); - let version = parent_attribute.version(); quote! { #( #subcommands )*; - let app = app #app_methods; - app #version + app #app_methods } }