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

feat(derive): Report the group id #4305

Merged
merged 1 commit into from Sep 30, 2022
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
12 changes: 11 additions & 1 deletion clap_derive/src/derives/args.rs
Expand Up @@ -90,6 +90,13 @@ pub fn gen_for_struct(
let augmentation = gen_augment(fields, &app_var, item, false);
let augmentation_update = gen_augment(fields, &app_var, item, true);

let group_id = if item.skip_group() {
quote!(None)
} else {
let group_id = item.ident().unraw().to_string();
quote!(Some(clap::Id::from(#group_id)))
};

quote! {
#[allow(dead_code, unreachable_code, unused_variables, unused_braces)]
#[allow(
Expand Down Expand Up @@ -140,6 +147,9 @@ pub fn gen_for_struct(
)]
#[deny(clippy::correctness)]
impl #impl_generics clap::Args for #item_name #ty_generics #where_clause {
fn group_id() -> Option<clap::Id> {
#group_id
}
fn augment_args<'b>(#app_var: clap::Command) -> clap::Command {
#augmentation
}
Expand Down Expand Up @@ -313,11 +323,11 @@ pub fn gen_augment(
quote!()
};
let initial_app_methods = parent_item.initial_top_level_methods();
let group_id = parent_item.ident().unraw().to_string();
let final_app_methods = parent_item.final_top_level_methods();
let group_app_methods = if parent_item.skip_group() {
quote!()
} else {
let group_id = parent_item.ident().unraw().to_string();
let literal_group_members = fields
.iter()
.filter_map(|(_field, item)| {
Expand Down
9 changes: 9 additions & 0 deletions tests/derive/groups.rs
Expand Up @@ -53,6 +53,10 @@ Usage: prog --add <CRATES|--path <PATH>|--git <GIT>>
For more information try '--help'
";
assert_output::<Opt>("prog --add", OUTPUT, true);

use clap::Args;
assert_eq!(Source::group_id(), Some(clap::Id::from("Source")));
assert_eq!(Opt::group_id(), Some(clap::Id::from("Opt")));
}

#[test]
Expand Down Expand Up @@ -80,4 +84,9 @@ fn skip_group_avoids_duplicate_ids() {

use clap::CommandFactory;
Opt::command().debug_assert();

use clap::Args;
assert_eq!(Empty::group_id(), None);
assert_eq!(Compose::<Empty, Empty>::group_id(), None);
assert_eq!(Opt::group_id(), Some(clap::Id::from("Opt")));
}