Skip to content

Commit

Permalink
feat(derive): Report the group id
Browse files Browse the repository at this point in the history
This will be needed when we support flattening for clap-rs#3165
  • Loading branch information
epage committed Sep 30, 2022
1 parent 1877e31 commit eb6285f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
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")));
}

0 comments on commit eb6285f

Please sign in to comment.