Added Test and User Guide

This commit is contained in:
vallabh 2023-02-15 09:58:31 +00:00
parent bfb48042b2
commit 45413ca7b0
3 changed files with 21 additions and 3 deletions

View file

@ -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

View file

@ -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}

View file

@ -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