-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Any best practice to imitate the "either-or" flags? #1216
Comments
@stevefan1999-personal, this is an interesting problem but I'm not so sure that there needs to be a bespoke solution for this. Have you considered separating your flags into two |
I'm faced with this need as well. Would you mind elaborating on the |
This issue is being marked as stale due to a long period of inactivity |
/stale go away |
I'm not sure what is meant by flagset
and I got A FlagSet has the same API methods as a Flag so there's no other way I can tell of how enable this. |
This issue is being marked as stale due to a long period of inactivity |
I need this functionality as well actually. Can anyone expand a bit on the flagsets solution? |
Related to numerous other flag issues including #1238. After using cobra/pflag for so long and looking at the backlog these are one of the most common places for improvement. |
doesn't #1654 solves this ? |
@fmenezes I've been trying to solve the same issue, and #1654 seems to solve a bit different use case. By my understanding, what you can currently do is this:
What you cannot, as far as I can see, and what I am trying to do is: As an example I have this code:
I want to require that either |
@terbolous I think I understand now, If we follow that logic you're missing a |
+1 |
Yeah, MarkFlagsMutuallyExclusiveAndRequired would be great I am currently stuck without this feature. Advance thanks to all contributors working this :) |
@kpramesh2212 In the same situation I've worked around with I'm curious if there's a standard way of implementing a custom flag validator that will be found by the Cobra core and will work with flag annotations? I believe, there's a variety of validation use cases where developers need complex validation that definitely will be missed in the Cobra core. |
I also would like to have this behavior. There is no way to require one of a group of flags and this would be excellent if you could add it. My use case is to configure credentials with the format:
This cannot be logically done with cobra's API. |
Hi, do you want me to implement the |
I think the question here is whether that is even in the scope of cobra/pflag. Personally I was not sure so I just evaluated this for my flags afterwards. But the fact that |
I think this can be closed due to #1952 |
Thanks @iTrooz |
Background
Suppose you want to make a simple app that lets you migrate from database to database, and I want to allow the end user to supply either a DSN string or fill in the connection structure, both represented as flags. If you know Java, this is comparable to Spring DB config.
Now, I have problems trying to imitate such relation: it's an either-or relation, i.e. logically mutually exclusive. However, whether the (colloquial) either-or is XOR or IMPLY is still debatable, so I want to discuss with others if they had any more clues here.
Current workarounds
Ideal solution
Implement a MarkMutualExclusionForFlagGroups method to let it correctly represent the relation: only one flag group among the "mutual exclusion set" can be activated.
Before that, we also need to implement FlagGroups -- flags that are chained to be required. It should set error if more than one groups are fulfilled. This is the exact solution to this problem but it is a very heavy engineering, i.e. no one will do this.
The text was updated successfully, but these errors were encountered: