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

Ability to set min_values per occurance when multiple_occurances is used #3542

Closed
2 tasks done
tmccombs opened this issue Mar 7, 2022 · 3 comments
Closed
2 tasks done
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-enhancement Category: Raise on the bar on expectations E-medium Call for participation: Experience needed to fix: Medium / intermediate M-breaking-change Meta: Implementing or merging this will introduce a breaking change.
Milestone

Comments

@tmccombs
Copy link
Contributor

tmccombs commented Mar 7, 2022

Please complete the following tasks

Clap Version

3.1.5

Describe your use case

I have an option that supports multiple occurrences, but each occurrence is independent, and needs a minimum of 1 value passed to it. I'm using grouped_values_of to get the groups of values. If I use .min_values(1), then it requires that there is at least one value in total, not that there is one value per occurance.

Here's some example code:

use clap::*; // 3.1.5

fn main() {
 let app = Command::new("test")
 .arg(Arg::new("example").short('x').min_values(1).multiple_occurrences(true).value_name("x"));
 let matches = app.get_matches_from(&["test", "-x", "a", "-x"]);
 println!("{:?}", matches);

}

This produces a successful match, but I want it to fail, because no argument is given to the second occurance of -x.

Describe the solution you'd like

Ideally, .min_values would behave as I've described. And if the current behavior is unintentional , then this is probably a bug, and it might be worth changing. However, changing min_values's behavior would technically be backwards incompatible, so it may be necessary to introduce a new api like min_values_per_occurance (and probably a similar max_values_per_occurance).

Alternatives, if applicable

The only alternative I know of is to have the application check that enough arguments are provided to each group returned by grouped_values_of rather than letting clap do the validation for me.

Additional Context

See sharkdp/fd#960 (comment).

@tmccombs tmccombs added the C-enhancement Category: Raise on the bar on expectations label Mar 7, 2022
tmccombs added a commit to tmccombs/fd that referenced this issue Mar 7, 2022
Accepting multiple occurances means we need to check this ourselves. See
clap-rs/clap#3542.
@tmccombs
Copy link
Contributor Author

tmccombs commented Mar 7, 2022

See also the workaround in sharkdp/fd@eb65a7e

tmccombs added a commit to tmccombs/fd that referenced this issue Mar 7, 2022
Accepting multiple occurances means we need to check this ourselves. See
clap-rs/clap#3542.
@epage epage added A-parsing Area: Parser's logic and needs it changed somehow. E-medium Call for participation: Experience needed to fix: Medium / intermediate M-breaking-change Meta: Implementing or merging this will introduce a breaking change. labels Mar 7, 2022
@epage epage added this to the 4.0 milestone Mar 7, 2022
@epage
Copy link
Member

epage commented Mar 7, 2022

Thanks for reporting this!

I had thought we had an issue for this (because I was aware of this and agree that this should change).

However, I worry about people relying on this and we'd need to wait until 4.0, though we might be able to do it earlier as part of the unstable-v4 feature flag.

sharkdp pushed a commit to sharkdp/fd that referenced this issue Mar 8, 2022
Accepting multiple occurances means we need to check this ourselves. See
clap-rs/clap#3542.
@epage
Copy link
Member

epage commented Aug 9, 2022

Missed this, its resolved via #2688. The new num_args is per occurrence.

@epage epage closed this as completed Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-enhancement Category: Raise on the bar on expectations E-medium Call for participation: Experience needed to fix: Medium / intermediate M-breaking-change Meta: Implementing or merging this will introduce a breaking change.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants