-
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 group flags #1778
base: main
Are you sure you want to change the base?
Add group flags #1778
Conversation
@@ -0,0 +1,89 @@ | |||
package cobra |
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.
Should this file be in spf13/pflag?
} | ||
|
||
// NamedFlags returns the specific named FlagSet that applies to this command. | ||
func (c *Command) NamedFlags(name string) *flag.FlagSet { |
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.
NamedFlags doesn't support persistent flags at the moment.
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.
Do you have any idea of what should be done to add this support?
@marckhouzam I don't think this is the best implementation, but I hope this is a good starting point for discussion. I borrowed an idea of NamedFlagSets from Kubernetes. |
The Cobra project currently lacks enough contributors to adequately respond to all PRs. This bot triages issues and PRs according to the following rules:
|
The Cobra project currently lacks enough contributors to adequately respond to all PRs. This bot triages issues and PRs according to the following rules:
|
This would still be a very valuable feature and should not go stale. |
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.
Hi.
I tested this PR and it works fine:
$ tail go.mod
go 1.18
require github.com/spf13/cobra v1.7.0
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
replace github.com/spf13/cobra => github.com/knqyf263/cobra v1.5.1-0.20220818091539-ec6fe6b61cd4
$ go run .
Output: dummy.txt
My root command
Usage:
root [sub] [flags]
Flags:
-h, --help help for root
--replicas int replicas
Cache Flags:
--dir string cache dir
--max-age duration cache ttl
Result Flags:
-f, --filename string filename
-o, --output string output format
I also applied the given patch to latest spf13/cobra
main branch and it applies cleanly.
This contribution would really help the community once merged, so I think it is totally worth it continuing to work on it.
I have some comments regarding the code but nothing critical with regard to my basic knowledge of spf13/cobra
.
Best regards.
} | ||
|
||
// NamedFlags returns the specific named FlagSet that applies to this command. | ||
func (c *Command) NamedFlags(name string) *flag.FlagSet { |
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.
Do you have any idea of what should be done to add this support?
// NamedFlagSets returns all the named FlagSet that applies to this command. | ||
func (c *Command) NamedFlagSets() *NamedFlagSets { | ||
if c.lnamedFlagSets == nil { | ||
c.lnamedFlagSets = NewNamedFlagSets(c.Name(), flag.ContinueOnError) |
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.
Should the flat.ContinueOnError
be hardcoded?
if nfs.flagSets == nil { | ||
nfs.flagSets = map[string]*pflag.FlagSet{} | ||
} |
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.
Should this operation be done in NewNamedFlagSets()
?
} | ||
|
||
func (nfs *NamedFlagSets) FlagUsagesWrapped(cols int) string { | ||
var buf bytes.Buffer |
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.
Would it be better to use a string builder?
if cols > 24 { | ||
i := strings.Index(s, zzz) | ||
lines := strings.Split(s[:i], "\n") | ||
fmt.Fprint(&buf, strings.Join(lines[:len(lines)-1], "\n")) | ||
fmt.Fprintln(&buf) |
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.
Is this code really needed? And why 24?
Hi, is there any updates do this PR status? It's a really nice feature. A lot of projects would benefit from it |
@pedromotita I wanted to kick off the discussion, but I've not received any response from maintainers. |
Hi.
I am not in maintainers heads, but you can try to publish this PR as ready for review and they may take a look (cost nothing to try by the way). Best regards. |
Thanks @knqyf263 for your patience (and everyone else interested in this), I will start looking at this PR. |
There is a PR to support this in |
@knqyf263 Although I think your API is very elegant, I'm concerned it complicates the code in Cobra and would make maintenance more difficult. I'm worried about creating yet another "type" of flags and then have: local flags, inherited flags and named flags. The grouping of flags in the help does not affect their actual behaviour. So from a behavioural perspective we still have either local or inherited flags. I would prefer keeping those two groups and adding an independent concept of "flag group" on top of this. For better or worse we already have an API for command groups (see doc here), so it may be good for users of Cobra to have a similar API for flag groups. With that in mind, what would you think of an API that looked something like:
Notice the use of the word "Help" to qualify the flag groups; I think we need something along those lines because Cobra has a concept of flag group for mutual exclusion and such things. I admit I like your API better, but I am hoping that something along the lines of the above will be safer to implement. And it should handle global flags transparently, I'm hoping. Hopefully this can start a discussion leading to something we all agree is useful. |
@knqyf263 any thoughts on @marckhouzam API proposal? He has a fair point Let us know if you're still willing to work on this PR. I'm new to the community, but would be very happy to help 😄 |
@marckhouzam I've been trying to wrap my head around a solution using your API suggestion. The main problem is that Whereas dealing with The more I think about this feature, the more I get convinced part of it should be implemented in |
I believe flags have an annotation field. Maybe you can use that to associate the group Id? |
@marckhouzam I had not thought of that. Thanks! I opened the PR #2117 with your API proposal and some test cases :) |
Description
Add group flags
Close #1327
Example
main.go