spf13 / cobra

A Commander for modern Go CLI interactions
https://cobra.dev
Apache License 2.0
37.8k stars 2.83k forks source link

How does one mark a flag a dependency of another flag #2177

Closed vague2k closed 1 month ago

vague2k commented 2 months ago

Here's an example, assume you have some output where an item is printed and the item has more info that you would like to show when the proper flags are present.

> myCli print item1 --createdAt
> item1 was created at 3:00:00 PM

In this example a flag called createdAt can be used to show the timestamp of when the item was created. But what if you wanted to show a specific format for that timestamp... you could do something like...

> myCli print item1 --createdAt --24H
> item1 was created at 15:00:00 PM

Is there a way that to make it such that (referring to this example) --24H depends on --createdAt ?

I've dug a bit to see if anyone had a similar question or solution but couldn't find anything note worthy.

If there is a solution and it seems too hacky or janky, I also think it would be cool have a utility method such as cmd.MarkFlagsAsDependent() if it's worth writing

marckhouzam commented 1 month ago

I think you are looking for flag groups: https://github.com/spf13/cobra/blob/main/site/content/user_guide.md#flag-groups

vague2k commented 1 month ago

I think you are looking for flag groups: https://github.com/spf13/cobra/blob/main/site/content/user_guide.md#flag-groups

the difference between the builtin flag groups and what im describing is that (referring again to my example) if you use --24H you ARE REQUIRED to include --createdAt but if you use --createdAt by itself you ARE NOT REQUIRED to include --24H.

I guess you to give it a name i'd call what i'm describing as a one-way MarkFlagsRequiredTogether