-
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
group flags for a subcommand #1327
Comments
Hi @medyagh, yes this can be done. For example
Here is an example PR that grouped commands for helm based on that package: https://github.com/helm/helm/pull/7917/files |
thanks for the response, I know about the grouping "the commands", (we have added that feature to minikube using the kubectl's lib), but that does Not help with adding groups for Flags. (it only helps with adding groups for Sub Commands) for example i would like to see this to give me all of the flags but grouped into different groups
|
Maybe you can give your support to #1003 which seems to implement this. |
This issue is being marked as stale due to a long period of inactivity |
+1 - this would be a great tool to improve the UX for more complex CLI commands. Curious to hear if anybody has found a way to solve this. |
@marckhouzam My understanding of #1003 is that it implements grouping of subcommands, not flags. |
I think this would also be a really useful command; I'm not sure about implementation but happy to review any PRs for it. |
The ability to group flags would be really beneficial. +1 bump! |
I just noticed that @knqyf263 has implemented this. So all it takes is a release now, or am I wrong? IMO this feature alone would justify a release. |
@marians That implementation is in another project 😉 |
Yes, it is not this project. Sorry for confusing you. @marians |
Oh dear, I didn't notice that. Thanks! And greetings to our partner Aqua. Have you considered bringing this change here? |
It seems like the discussion is suspended. Do the maintainers agree with this enhancement? If yes, I'd be happy to contribute upstream. |
I got the impression that the implementations out there are project-specific (overriding the help text and adding specific flag groups). If we can get a PR with a generic solution with a clean API, then it would be appropriate for Cobra. |
I really like the feature. Now I'm using The flags looks like
|
Yes, it is a workaround as cobra doesn't support the feature.
|
The maintainer might have better solutions. |
I've opened a PR so that we can start a discussion. |
@medyagh can you share a full example of minikube command with flag groups? An example from real command can help to answer:
|
Here's a demo, actually it's really easy: func Test_spf13_cmdflags_groupby(t *testing.T) {
rootCmd := cobra.Command{
Use: "root",
Short: "root cmd description",
Long: "root cmd description",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("command args: %v\n", strings.Join(args, " "))
},
}
groups := map[*pflag.FlagSet]string{}
// group1
fs1 := pflag.NewFlagSet("features", pflag.ExitOnError)
fs1.Bool("feature1", true, "enable feature1")
fs1.Bool("feature2", true, "enable feature2")
fs1.Bool("feature3", true, "enable feature3")
rootCmd.Flags().AddFlagSet(fs1)
groups[fs1] = "features"
// group2
fs2 := pflag.NewFlagSet("patches", pflag.ExitOnError)
fs2.Bool("patch1", true, "apply patch1")
fs2.Bool("patch2", true, "apply patch2")
rootCmd.Flags().AddFlagSet(fs2)
groups[fs2] = "patches"
// cobra will:
// - call helpfunc to print help message if defined, otherwise
// - call usagefunc to print help message if defined, otherwise
// - call the generated default usagefunc
//
// here we use SetUsageFunc is enough.
rootCmd.SetUsageFunc(func(c *cobra.Command) error {
for fs, name := range groups {
usage := fs.FlagUsages()
idx := strings.IndexFunc(usage, func(r rune) bool {
return r != ' '
})
desc := strings.Repeat(" ", idx) + name + ":"
help := desc + "\n" + usage
fmt.Println(help)
}
return nil
})
// run
os.Args = []string{"root", "--feature3=false", "--patch2=false", "--help", "helloworld"}
err := rootCmd.Execute()
require.Nil(t, err)
} |
in minikube our start command has many flags and we would like to group them for better readablility
so something like
is that possible?
The text was updated successfully, but these errors were encountered: