-
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
add MarkIfFlagPresentThenOthersRequired #2200
base: main
Are you sure you want to change the base?
Conversation
@marckhouzam Would love to get this in thanks! Let me know if you have any questions :) |
@dpetersen @markbates @EdwardBetts Would love to get this in thanks! Let me know if you have any questions :) |
@marckhouzam is there anything we can do to get this moving? Alternatively we're happy to have discussions about it being out of scope, it just felt fairly obviously useful to us. |
I like the idea. I’ll put it on my queue to review. Thank you. |
if len(unset) > 0 { | ||
return fmt.Errorf( | ||
"if the first flag in the group [%v] is set, all other flags must be set; the following flags are not set: %v", | ||
flagList, unset, | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if len(unset) > 0 { | |
return fmt.Errorf( | |
"if the first flag in the group [%v] is set, all other flags must be set; the following flags are not set: %v", | |
flagList, unset, | |
) | |
} | |
if len(unset) > 0 { | |
return fmt.Errorf( | |
"%v is set, all other flags [%v] must be set; the following flags are not set: %v", | |
flagList[0], flagList[1:], unset, | |
) | |
} |
Maybe something like this would be more readable and understood
Or this
if len(unset) > 0 { | |
return fmt.Errorf( | |
"if the first flag in the group [%v] is set, all other flags must be set; the following flags are not set: %v", | |
flagList, unset, | |
) | |
} | |
if len(unset) > 0 { | |
return fmt.Errorf( | |
"%v is set, the following flags must be provided: %v", | |
flagList[0], unset, | |
) | |
} |
desc: "No flags no problem even with conflicting groups", | ||
flagGroupsRequired: []string{"a b"}, | ||
flagGroupsExclusive: []string{"a b"}, | ||
flagGroupsIfPresentThenRequired: []string{"a b"}, |
There was a problem hiding this comment.
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 about using
flagGroupsIfPresentThenRequired: []string{"a b"}, | |
flagGroupsIfPresentThenRequired: []string{"a b", "b a"}, |
This one is similar to the required group that also exists.
Then, it's up to you to decide if it should be discouraged or not
Please also consider adding a test about
flagGroupsIfPresentThenRequired: []string{"a b"}, | |
flagGroupsIfPresentThenRequired: []string{"a b", "a c"}, |
Sane here, you should decide if you want to discourage it or not
Same about a test about
flagGroupsIfPresentThenRequired: []string{"a b"}, | |
flagGroupsIfPresentThenRequired: []string{"a b c", "a c b"}, |
flagGroupsIfPresentThenRequired: []string{"a b"}, | |
flagGroupsIfPresentThenRequired: []string{"a b c", "b c"}, |
I'm asking for all these tests because I'm unsure how it would be expected to work
@@ -221,6 +278,7 @@ func sortedKeys(m map[string]map[string]bool) []string { | |||
// - when a flag in a group is present, other flags in the group will be marked required | |||
// - when none of the flags in a one-required group are present, all flags in the group will be marked required | |||
// - when a flag in a mutually exclusive group is present, other flags in the group will be marked as hidden | |||
// - when the first flag in an if-present-then-required group is present, the second flag will be marked as required |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// - when the first flag in an if-present-then-required group is present, the second flag will be marked as required | |
// - when the first flag in an if-present-then-required group is present, the other flags will be marked as required |
This PR introduces a new flag group function,
MarkIfFlagPresentThenOthersRequired
, which enforces a "one-way required together" relationship among flags. This means that if a primary flag is set, then other dependent flags must also be set. This allows users to make certain flags conditionally required based on the presence of another flag, while maintaining flexibility when the primary flag is not provided.Example
Consider a command called
get-range
that uses the flags--start
and--end
to specify a date range. By default, the command can run without either flag, using default start and end values (e.g.,start=someDefaultStart
andend=Now
). However, you may want to enforce that if the user specifies an--end
, they must also specify a--start
.--end=value
, it will trigger an error because--start
is missing.--start
, it will run using the default values or the provided start date.This change makes it easier to enforce dependencies between flags while allowing flexible defaults when the primary flag is not specified.