mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
add error check to RemoveGroup command
This commit is contained in:
parent
08be2cb981
commit
02094ac5c5
1 changed files with 7 additions and 1 deletions
|
@ -1325,18 +1325,23 @@ func (c *Command) AddGroup(groups ...*Group) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveGroup removes one or more command groups to this parent command.
|
// RemoveGroup removes one or more command groups to this parent command.
|
||||||
func (c *Command) RemoveGroup(groupIDs ...string) {
|
func (c *Command) RemoveGroup(groupIDs ...string) error {
|
||||||
// remove groups from commandgroups
|
// remove groups from commandgroups
|
||||||
groups := []*Group{}
|
groups := []*Group{}
|
||||||
|
hasRemoved := false
|
||||||
main:
|
main:
|
||||||
for _, group := range c.commandgroups {
|
for _, group := range c.commandgroups {
|
||||||
for _, groupID := range groupIDs {
|
for _, groupID := range groupIDs {
|
||||||
if group.ID == groupID {
|
if group.ID == groupID {
|
||||||
|
hasRemoved = true
|
||||||
continue main
|
continue main
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
groups = append(groups, group)
|
groups = append(groups, group)
|
||||||
}
|
}
|
||||||
|
if !hasRemoved {
|
||||||
|
return fmt.Errorf("following group ID does not exist; %s", strings.Join(groupIDs, ", "))
|
||||||
|
}
|
||||||
c.commandgroups = groups
|
c.commandgroups = groups
|
||||||
// remove the groupID from the target commands
|
// remove the groupID from the target commands
|
||||||
for _, command := range c.commands {
|
for _, command := range c.commands {
|
||||||
|
@ -1357,6 +1362,7 @@ main:
|
||||||
c.resetCompletionCommandGroupID()
|
c.resetCompletionCommandGroupID()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveCommand removes one or more commands from a parent command.
|
// RemoveCommand removes one or more commands from a parent command.
|
||||||
|
|
Loading…
Reference in a new issue