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

Missing support for removing group #1911 #1912

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(groupID string) bool {
index := c.getGroupIndex(groupID)
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{}
Expand Down
18 changes: 18 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down