From bfb48042b2a2f1c1b327c3e10fbfb27fcec842e5 Mon Sep 17 00:00:00 2001 From: vallabh Date: Wed, 15 Feb 2023 09:45:25 +0000 Subject: [PATCH 1/2] Fixed Missing support for removing group #1911 --- command.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/command.go b/command.go index b6e3f4a1c..08b274be3 100644 --- a/command.go +++ b/command.go @@ -1311,6 +1311,26 @@ func (c *Command) AddGroup(groups ...*Group) { c.commandgroups = append(c.commandgroups, groups...) } +// RemoveGroup removes command group from parent command. +func (c *Command) RemoveGroup(group *Group) bool { + index := c.getGroupIndex(group.ID) + if index >= 0 { + c.commandgroups = append(c.commandgroups[:index], c.commandgroups[index+1:]...) + return true + } + return false +} + +// getGroupIndex returns index of groupID if it exists in the list of command groups else -1. +func (c *Command) getGroupIndex(groupID string) int { + for index, x := range c.commandgroups { + if x.ID == groupID { + return index + } + } + return -1 +} + // RemoveCommand removes one or more commands from a parent command. func (c *Command) RemoveCommand(cmds ...*Command) { commands := []*Command{} From 45413ca7b09cc33458770be0555ae316c2908eec Mon Sep 17 00:00:00 2001 From: vallabh Date: Wed, 15 Feb 2023 09:58:31 +0000 Subject: [PATCH 2/2] Added Test and User Guide --- command.go | 4 ++-- command_test.go | 18 ++++++++++++++++++ user_guide.md | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/command.go b/command.go index 08b274be3..00a6a26ce 100644 --- a/command.go +++ b/command.go @@ -1312,8 +1312,8 @@ func (c *Command) AddGroup(groups ...*Group) { } // RemoveGroup removes command group from parent command. -func (c *Command) RemoveGroup(group *Group) bool { - index := c.getGroupIndex(group.ID) +func (c *Command) RemoveGroup(groupID string) bool { + index := c.getGroupIndex(groupID) if index >= 0 { c.commandgroups = append(c.commandgroups[:index], c.commandgroups[index+1:]...) return true diff --git a/command_test.go b/command_test.go index 18011325d..d0910f9a2 100644 --- a/command_test.go +++ b/command_test.go @@ -1862,6 +1862,24 @@ func TestAddGroup(t *testing.T) { checkStringContains(t, output, "\nTest group\n cmd") } +func TestRemoveGroup(t *testing.T) { + var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun} + + rootCmd.AddGroup(&Group{ID: "group1", Title: "Test group 1"}) + rootCmd.AddGroup(&Group{ID: "group2", Title: "Test group 2"}) + rootCmd.AddGroup(&Group{ID: "group3", Title: "Test group 3"}) + rootCmd.AddGroup(&Group{ID: "group4", Title: "Test group 4"}) + + rootCmd.RemoveGroup("group2") + + for _, group := range rootCmd.Groups() { + if group.ID == "group2" { + t.Error(`Expected to not contain "group2"`) + } + } + +} + func TestWrongGroupFirstLevel(t *testing.T) { var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun} diff --git a/user_guide.md b/user_guide.md index 00b53d03c..c90db13a6 100644 --- a/user_guide.md +++ b/user_guide.md @@ -495,7 +495,7 @@ around it. In fact, you can provide your own if you want. Cobra supports grouping of available commands in the help output. To group commands, each group must be explicitly defined using `AddGroup()` on the parent command. Then a subcommand can be added to a group using the `GroupID` element of that subcommand. The groups will appear in the help output in the same order as they are defined using different -calls to `AddGroup()`. If you use the generated `help` or `completion` commands, you can set their group ids using +calls to `AddGroup()`. To remove added groups you can use `RemoveGroup(groupID string)`. If you use the generated `help` or `completion` commands, you can set their group ids using `SetHelpCommandGroupId()` and `SetCompletionCommandGroupId()` on the root command, respectively. ### Defining your own help