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

Allow subcmd name to be something else than a literal in clap derive #2751

Merged
merged 1 commit into from
Sep 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions clap_derive/src/derives/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,12 @@ fn gen_has_subcommand(
matches!(&*kind, Kind::Flatten)
});

let match_arms = variants.iter().map(|(_variant, attrs)| {
let subcommands = variants.iter().map(|(_variant, attrs)| {
let sub_name = attrs.cased_name();
quote! {
#sub_name => true,
if #sub_name == name {
return true
}
}
});
let child_subcommands = flatten_variants
Expand All @@ -330,14 +332,11 @@ fn gen_has_subcommand(
quote! { true }
} else {
quote! {
match name {
#( #match_arms )*
_ => {
#( #child_subcommands )else*
#( #subcommands )*

false
}
}
#( #child_subcommands )else*

false
}
}
}
Expand Down Expand Up @@ -421,7 +420,7 @@ fn gen_from_arg_matches(
matches!(&*kind, Kind::Flatten)
});

let match_arms = variants.iter().map(|(variant, attrs)| {
let subcommands = variants.iter().map(|(variant, attrs)| {
let sub_name = attrs.cased_name();
let variant_name = &variant.ident;
let constructor_block = match variant.fields {
Expand All @@ -435,8 +434,8 @@ fn gen_from_arg_matches(
};

quote! {
Some((#sub_name, arg_matches)) => {
Some(#name :: #variant_name #constructor_block)
if #sub_name == name {
return Some(#name :: #variant_name #constructor_block)
}
}
});
Expand All @@ -461,7 +460,7 @@ fn gen_from_arg_matches(
let wildcard = match ext_subcmd {
Some((span, var_name, str_ty, values_of)) => quote_spanned! { span=>
::std::option::Option::Some(#name::#var_name(
::std::iter::once(#str_ty::from(other))
::std::iter::once(#str_ty::from(name))
.chain(
sub_arg_matches.#values_of("").into_iter().flatten().map(#str_ty::from)
)
Expand All @@ -474,14 +473,17 @@ fn gen_from_arg_matches(

quote! {
fn from_arg_matches(arg_matches: &clap::ArgMatches) -> Option<Self> {
match arg_matches.subcommand() {
#( #match_arms, )*
::std::option::Option::Some((other, sub_arg_matches)) => {
#( #child_subcommands )else*

#wildcard
if let Some((name, sub_arg_matches)) = arg_matches.subcommand() {
{
let arg_matches = sub_arg_matches;
#( #subcommands )*
}
::std::option::Option::None => ::std::option::Option::None,

#( #child_subcommands )else*

#wildcard
} else {
None
}
}
}
Expand Down Expand Up @@ -554,7 +556,7 @@ fn gen_update_from_arg_matches(
};

quote! {
(#sub_name, #name :: #variant_name #pattern) => {
#name :: #variant_name #pattern if #sub_name == name => {
let arg_matches = sub_arg_matches;
#updater
}
Expand Down Expand Up @@ -588,9 +590,9 @@ fn gen_update_from_arg_matches(
arg_matches: &clap::ArgMatches,
) {
if let Some((name, sub_arg_matches)) = arg_matches.subcommand() {
match (name, self) {
match self {
#( #subcommands ),*
(other_name, s) => {
s => {
#( #child_subcommands )*
if let Some(sub) = <Self as clap::FromArgMatches>::from_arg_matches(arg_matches) {
*s = sub;
Expand Down
20 changes: 20 additions & 0 deletions clap_derive/tests/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,23 @@ fn update_ext_subcommand() {
opt.try_update_from(&["test", "ext", "42", "44"]).unwrap();
assert_eq!(Opt::parse_from(&["test", "ext", "42", "44"]), opt);
}
#[test]
fn subcommand_name_not_literal() {
fn get_name() -> &'static str {
"renamed"
}

#[derive(Clap, PartialEq, Debug)]
struct Opt {
#[clap(subcommand)]
subcmd: SubCmd,
}

#[derive(Clap, PartialEq, Debug)]
enum SubCmd {
#[clap(name = get_name())]
SubCmd1,
}

assert!(Opt::try_parse_from(&["test", "renamed"]).is_ok());
}