Skip to content

Commit

Permalink
Fix duplication of aliases in subcommands
Browse files Browse the repository at this point in the history
This commit fixes the issue where all top level methods were duplicated
for subcommands with fields (see issue TeXitoi#418). For some attributes, such
as aliases, this resulted in duplicate output.
  • Loading branch information
michiel-de-muynck committed Oct 13, 2021
1 parent 701d6dd commit 51b3333
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
14 changes: 8 additions & 6 deletions structopt-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,14 @@ fn gen_augment_clap_enum(

_ => {
let app_var = Ident::new("subcommand", Span::call_site());
let from_attrs = attrs.top_level_methods();
let version = attrs.version();

let arg_block = match variant.fields {
// If the variant is named, then gen_augmentation already generates the
// top level methods (#from_attrs) and version.
Named(ref fields) => gen_augmentation(&fields.named, &app_var, &attrs),
Unit => quote!( #app_var ),
Unit => quote!( #app_var#from_attrs#version ),
Unnamed(FieldsUnnamed { ref unnamed, .. }) if unnamed.len() == 1 => {
let ty = &unnamed[0];
quote_spanned! { ty.span()=>
Expand All @@ -536,21 +541,18 @@ fn gen_augment_clap_enum(
)
} else {
#app_var
}
}#from_attrs#version
}
}
}
Unnamed(..) => abort!(variant, "non single-typed tuple enums are not supported"),
};

let name = attrs.cased_name();
let from_attrs = attrs.top_level_methods();
let version = attrs.version();
Some(quote! {
let app = app.subcommand({
let #app_var = ::structopt::clap::SubCommand::with_name(#name);
let #app_var = #arg_block;
#app_var#from_attrs#version
#arg_block
});
})
},
Expand Down
28 changes: 28 additions & 0 deletions tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,34 @@ fn issue_359() {
);
}

#[test]
fn issue_418() {
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opts {
#[structopt(subcommand)]
/// The command to run
command: Command,
}

#[derive(Debug, StructOpt)]
enum Command {
/// Reticulate the splines
#[structopt(visible_alias = "ret")]
Reticulate {
/// How many splines
num_splines: u8,
},
/// Frobnicate the rest
#[structopt(visible_alias = "frob")]
Frobnicate,
}

let help = get_long_help::<Opts>();
assert!(help.contains("Reticulate the splines [aliases: ret]"));
}

#[test]
fn issue_490() {
use std::iter::FromIterator;
Expand Down

0 comments on commit 51b3333

Please sign in to comment.