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

add ability to parse additional group methods #4688

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions clap_derive/src/derives/args.rs
Expand Up @@ -400,10 +400,13 @@ pub fn gen_augment(
}};
}

let group_methods = parent_item.group_methods();

quote!(
.group(
clap::ArgGroup::new(#group_id)
.multiple(true)
epage marked this conversation as resolved.
Show resolved Hide resolved
#group_methods
.args(#literal_group_members)
)
)
Expand Down
12 changes: 11 additions & 1 deletion clap_derive/src/item.rs
Expand Up @@ -49,6 +49,7 @@ pub struct Item {
is_enum: bool,
is_positional: bool,
skip_group: bool,
group_methods: Vec<Method>,
kind: Sp<Kind>,
}

Expand Down Expand Up @@ -273,6 +274,7 @@ impl Item {
is_enum: false,
is_positional: true,
skip_group: false,
group_methods: vec![],
kind,
}
}
Expand Down Expand Up @@ -325,7 +327,10 @@ impl Item {
if name == "short" || name == "long" {
self.is_positional = false;
}
self.methods.push(Method::new(name, quote!(#arg)));
match kind {
AttrKind::Group => self.group_methods.push(Method::new(name, quote!(#arg))),
Comment on lines +330 to +331
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test where the group's id is overridden? I think that will break and instead overwrite the command's name.

Similarly, we need a ui-test ensuring that #[group(name = "foo")] fails as name isn't a group attribute

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ui-test exists but not the overridden id.

The problem I'm trying to highlight is that we are only setting group_methods in one branch and instead setting things on the Command in the other branches.

Maybe we should have a push_group_method function and make the switch between them higher up.

_ => self.methods.push(Method::new(name, quote!(#arg))),
};
}
}

Expand Down Expand Up @@ -964,6 +969,11 @@ impl Item {
quote!( #(#doc_comment)* #(#methods)* )
}

pub fn group_methods(&self) -> TokenStream {
let group_methods = &self.group_methods;
quote!( #(#group_methods)* )
}

pub fn deprecations(&self) -> proc_macro2::TokenStream {
let deprecations = &self.deprecations;
quote!( #(#deprecations)* )
Expand Down
45 changes: 45 additions & 0 deletions tests/derive/groups.rs
Expand Up @@ -120,3 +120,48 @@ fn helpful_panic_on_duplicate_groups() {
use clap::CommandFactory;
Opt::command().debug_assert();
}

#[test]
fn required_group() {
epage marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Parser, Debug)]
struct Opt {
#[command(flatten)]
source: Source,

#[arg(long)]
alt_source: String,
}

#[derive(clap::Args, Debug)]
#[group(required = true, multiple = false, conflicts_with = "alt_source")]
struct Source {
#[arg(long)]
path: Option<std::path::PathBuf>,
#[arg(long)]
git: Option<String>,
}

const OUTPUT: &str = "\
error: the following required arguments were not provided:
--alt-source <ALT_SOURCE>
<--path <PATH>|--git <GIT>>

Usage: prog --alt-source <ALT_SOURCE> <--path <PATH>|--git <GIT>>

For more information, try '--help'.
";
assert_output::<Opt>("prog", OUTPUT, true);

use clap::Args;
assert_eq!(Opt::group_id(), Some(clap::Id::from("Opt")));
assert_eq!(Source::group_id(), Some(clap::Id::from("Source")));
use clap::CommandFactory;
let source_id = clap::Id::from("Source");
let opt_command = Opt::command();
let source_group = opt_command
.get_groups()
.find(|g| g.get_id() == &source_id)
.unwrap();
assert!(source_group.is_required_set());
// assert!(source_group.is_multiple()); currently broken. Fixed by PR #4704
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make a note about this on the issue, not PR (as PRs come and go) and delete it.,

As-is, we have no way to track fixing this and it will get lost.

Comment on lines +159 to +166
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also tend to test the behavior from setting things rather than checking that they are set.

}
23 changes: 23 additions & 0 deletions tests/derive_ui/group_name_attribute.rs
@@ -0,0 +1,23 @@
use clap::Parser;

#[derive(Parser, Debug)]
#[command(name = "basic", subcommand)]
struct Opt {
#[command(flatten)]
source: Source,
}

#[derive(clap::Args, Debug)]
#[group(required = true, name = "src")]
struct Source {
#[arg(short)]
git: String,

#[arg(short)]
path: String,
}

fn main() {
let opt = Opt::parse();
println!("{:?}", opt);
}
7 changes: 7 additions & 0 deletions tests/derive_ui/group_name_attribute.stderr
@@ -0,0 +1,7 @@
error: `#[group(required = true, name = "src")]` does not support name
--> $DIR/group_name_attribute.rs:3:10
|
11 |#[group(required = true, name = "src")]
| ^^^^^^^^^^^^
|
= note: this error originates in the derive macro `group` (in Nightly builds, run with -Z macro-backtrace for more info)