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

add MarkIfFlagPresentThenOthersRequired #2200

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

faizan-siddiqui
Copy link

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 and end=Now). However, you may want to enforce that if the user specifies an --end, they must also specify a --start.

cmd := &cobra.Command{
    Use: "get-range",
}

cmd.Flags().String("start", "", "Start date")
cmd.Flags().String("end", "", "End date")

// If --end is set, --start is required
cmd.MarkIfFlagPresentThenRequired("end", "start")
  • If the command is called with only --end=value, it will trigger an error because --start is missing.
  • If called without any flags or only with --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.

@CLAassistant
Copy link

CLAassistant commented Oct 23, 2024

CLA assistant check
All committers have signed the CLA.

@faizan-siddiqui
Copy link
Author

@marckhouzam Would love to get this in thanks! Let me know if you have any questions :)

@faizan-siddiqui
Copy link
Author

@dpetersen @markbates @EdwardBetts Would love to get this in thanks! Let me know if you have any questions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants