-
Notifications
You must be signed in to change notification settings - Fork 360
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
Requiring one Option or another, not both #88
Comments
Not really, the current way to do that would be to use a set, but that's a different syntax. What would a good api for that be? Maybe passing other options to the |
I was thinking of either having a variadic
Or the ability to make a Group required, such as The first seems more flexible (allows more complicated networks of requirements, and overlapping sets), but the second easily allows expressing this in the auto-generated help string for groups. But then again, we could auto-detect if a group has an "at least N" requirement.. I'm leaning towards the first approach. I'm not really sure.. not a big fan of a variadic Python's argparse takes the second approach. |
Hi, Here is the simple example:
With "--range 1 2 3" argument, I get: "--min is required". A solution would be to remove the "required" flag but then the user may give nonsense. Is there a solution ? Thank you. |
@henryiii, you marked this issue as closed and resolved in 1.5, but it looks like only the "not both" part is actually resolved. At least I was unable to find a way to "Require one option or another". I mean, as @OlivierHartmann shows above, you can require one AND another option to be required, but that doesn't work if they are mutually exclusive. How do you specify OR ? An example would come in handy. There is currently no example for |
This issue is still open, only part of it was resolved in 1.5, which is why it was mentioned in 1.5 but only the 1.5 release issue was closed. I think the proper way to do this might be by expanding groups to be a full object, then allowing a group of options to just require 1 (or x) to pass. It's not in the immediate plans but a PR would be welcome, at least if well done. |
One possibility would be adding The app would count the number of options that had results after parsing just like it does for subcommands. |
That might work, actually. So the idea is just to require N options for an app (say, 1), and then nameless subcommands allow you to build groups. Not sure how it would look in the help, but it would be a simple addition. |
You would probably want a min and max just like for |
This "nameless subcommand" approach sounds to me like an ugly kludge. I would prefer the existing groups to become full-scale objects with properties. However, there definitely are cases where mutually exclusive option groups can exist within a logical options group. Then binding mutual-exclusiveness to a group wouldn't work unless groups would be able to have child groups. |
I think this issue can be closed. As best as I can tell this capability is in place now with the 1.8 release. |
so what do you have to use to require only one option to be provided? I can't find such info |
@ElDesalmado , same here, I am looking for this, and can't any information on how to implement this. Could anyone please point us to some example or explanation of this? Thanks! |
@ElDesalmado and @lczech you can have a look here
Now either |
@Coderx7 but there is no |
Thanks for the suggestion, @Coderx7, but this is not what @ElDesalmado and I are looking for. Excluding other options has been part of CLI11 since its early days. What we are rather interested in is a way to say "exactly one of these options HAS to be provided by the user". To make that an actual part of the CLI11 API might be tricky. I have not yet figured out what I would like the help message for that to even look like, for example... The way I am currently solving this is by having a simple condition in the execution of the program that checks that exactly one of the options is set, and if not, throws an exception. Works well, but compared to build-in ways it does not show up in the help as "one of them has to be set" or the like. Additionally, this way, my program already has started (printing its header etc), instead of failing early on (as is the case with other types of user input errors that CLI11 takes care of). |
Just in case this has not been answered yet group=app.add_option_group("subgroup");
group->add_flag("--cpu", isOnCpu, "Run the computation on the CPU")->ignore_case();
group->add_flag("--gpu", isOnGpu, "Run the computation on the GPU")->ignore_case();
group->require_option(1); The require_option(1) will state that 1 and only 1 option from the group is required. Any more or less is an error. |
Reopening this issue to be closed when additional documentation on this is added to the book |
@phlptp looks like a workaround, rather than an intended way. Though thank you |
Option groups are a way to cluster different flags and options together for logical or control purposes. It may seem like a workaround for this particular case, but if you wanted 1 of 5 options, or 2 of 5 options, or 1 option from a group of 3 and 1 option from another group of 5, all are similar with option groups, whereas trying to do it through another mechanism starts to look ugly fast. |
Hi @phlptp, This solution creates a new group in the help message with new a new subtitle. Is it possible to maintain the original help format? (i.e., the one that I have without calling add_option_group) |
@phlptp what do you recommend for choosing between group A or group B? Either that, or option A vs group B (my group A has only 1 option). |
Hi, is there some way to require that one of { "--option1", "--option2" } is present, but not necessarily both? I looked at the documentation and examples, and couldn't figure out if this is possible. Do I need a custom callback?
By the way, excellent work on this library! Something like this was sorely needed in C++.
The text was updated successfully, but these errors were encountered: