clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
13.93k stars 1.03k forks source link

Add functions `required_unless_eq_any` and `required_unless_eq_all` for `Arg` and `ArgGroup` #4682

Open rob-p opened 1 year ago

rob-p commented 1 year ago

Please complete the following tasks

Clap Version

4.1.4

Describe your use case

I would like to define an argument or argument group that is required unless some other argument takes on a specific value.

This would be very useful in my use case (a tool processing single-cell gene expression data), because in some situations, when a particular common protocol is used and passed as the value to some argument (in this case called --geometry), the values of several other arguments can be inferred and should no longer be required. However, if the --geometry is not one of these common values, then the other arguments should be required.

If the --geometry argument only took on a small number of possible values, then something like this might be possible with required_if, by listing all of the other values for which the argument should still be required. However, in my use case, the potential values to --geometry are potentially unbounded since the user can provide custom values that are later parsed and interpreted by the program. Thus, I need a way to specify a list of specific values for which other arguments should not be required.

Describe the solution you'd like

Just as there are now methods on Arg (and ArgGroup, I believe?) like required_ifs and required_if_eq_any and, I propose the addition of methods:

These do pretty much what it says on the label. The first says an argument or argument group is required unless any of the listed conditions are satisfied (i.e. some other argument has a particular value), and the second says an argument or argument group is required unless all of the listed conditions are satisfied.

Given the existence of the other functions that are already present for related functionality, these seem a natural addition.

Alternatives, if applicable

No response

Additional Context

I brought up a discussion about this here, but I am pretty sure that no way currently exists to accomplish this without the functionality requested above.

epage commented 1 year ago

Sorry for the slow response!

Another route for this is to extend ArgPredicate.

A challenge we have is there is an arbitrary set of functionality people ask of clap while we are working on shrinking clap (#2037, #1365) and we have been exploring other ways of handling this in #3476. This makes me hesitant every time there is a new request for an additional variant of existing functionality.