Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplication of aliases in subcommands #504

Merged
merged 1 commit into from Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions structopt-derive/src/lib.rs
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
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