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

Conversation

klnusbaum
Copy link

@klnusbaum klnusbaum commented Feb 1, 2023

Towards completing #4574 , let's add the other methods for group derive.
This is my first PR for Clap and a lot of this was Cargo coding.
Please forgive anything that seems silly.
I'm very open to feedback :)

@klnusbaum klnusbaum force-pushed the required_group branch 2 times, most recently from 2d3acf1 to 931eac5 Compare February 1, 2023 06:02
clap_derive/src/attr.rs Outdated Show resolved Hide resolved
tests/derive/groups.rs Outdated Show resolved Hide resolved
Comment on lines 152 to 165
use clap::CommandFactory;
let source_id = clap::Id::from("Source");
let dest_id = clap::Id::from("Dest");
let opt_command = Opt::command();
let source_group = opt_command
.get_groups()
.find(|g| g.get_id() == &source_id)
.unwrap();
let dest_group = opt_command
.get_groups()
.find(|g| g.get_id() == &dest_id)
.unwrap();
assert!(source_group.is_required_set());
assert!(dest_group.is_required_set());
Copy link
Member

Choose a reason for hiding this comment

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

Generally we do integration tests where we verify the behavior of the feature, like with assert_output

@epage
Copy link
Member

epage commented Feb 1, 2023

Thanks for moving this forward!

@klnusbaum klnusbaum changed the title add "required" option for group attribute add ability to parse additional group methods Feb 3, 2023
@klnusbaum
Copy link
Author

klnusbaum commented Feb 3, 2023

@epage I've made this more general, which I think addresses your comment about switching to a group_methods field on the Item struct. If this is the right direction, I can go ahead and beef up the tests.

Comment on lines +330 to +331
match kind {
AttrKind::Group => self.group_methods.push(Method::new(name, quote!(#arg))),
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.

@epage
Copy link
Member

epage commented Feb 3, 2023

If this is the right direction, I can go ahead and beef up the tests.

Yes it is, thanks!

This adds the "required" derive option for the group creation.
Needed for clap-rs#4574.
@klnusbaum
Copy link
Author

@epage I have a question I was hoping you might be able to help me with.

I can't figure out how to address the tests in tests/derive/groups.rs. The only way I can trigger them to run is cargo test --all. But I'd really like to be able to run just the single, new test I'm writing. Also, I'd like to be able to use cargo-expand to see what code is actually getting generated from my code changes.

I've never seen a dedicated tests folder like this before. Do you know how I can specifically refer to the groups tests?

@epage
Copy link
Member

epage commented Feb 17, 2023

Normally tests/ are in individual files which makes them individual targets to be run like cargo test --test file -- testname. To reduce compilation overhead, we've merged them into larger test targets by using folders.

To run a specific test for derive, it would be cargo test -F derive --test derive -- testname. You can add the --exact flag if a similar test contains your tests name + more.

You would also run cargo expand --features derive --test derive. The file will be quite large but if you search in it for your test name, you can look at that and ignore the rest.

This adds the ability derive additional options for the group creation.
Needed for clap-rs#4574.
Copy link
Member

@epage epage left a comment

Choose a reason for hiding this comment

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

Thanks for continuing to move this forward!

.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
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.

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

Comment on lines +330 to +331
match kind {
AttrKind::Group => self.group_methods.push(Method::new(name, quote!(#arg))),
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.

@epage
Copy link
Member

epage commented Mar 27, 2023

FYI I've done some more work on this and closing this PR in favor of #4795

@epage epage closed this Mar 27, 2023
@klnusbaum
Copy link
Author

@epage
This fell off my radar and I'm so sorry I didn't complete it in a timely manner. I really appreciate all the help you gave me trying to get this through. Thank you so much for everything and I'm super stoked to use the new stuff you got working.

Cheers :)

@klnusbaum klnusbaum deleted the required_group branch April 3, 2023 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants