Skip to content

Commit

Permalink
Revert structopt clap-rs#325
Browse files Browse the repository at this point in the history
TeXitoi/structopt#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.
  • Loading branch information
epage committed Oct 5, 2021
1 parent 90a5e7c commit b900d55
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
10 changes: 2 additions & 8 deletions clap_derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,11 +768,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
Expand All @@ -782,13 +783,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)
}
Expand Down
4 changes: 1 addition & 3 deletions clap_derive/src/derives/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}}
}

Expand Down
10 changes: 3 additions & 7 deletions clap_derive/src/derives/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,12 @@ fn gen_augment(

let name = attrs.cased_name();
let from_attrs = attrs.top_level_methods();
let version = attrs.version();
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
});
}
}
Expand Down Expand Up @@ -252,12 +251,11 @@ fn gen_augment(

let name = attrs.cased_name();
let from_attrs = attrs.top_level_methods();
let version = attrs.version();
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
});
}
}
Expand All @@ -266,11 +264,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
}
}

Expand Down

0 comments on commit b900d55

Please sign in to comment.