add error check to RemoveGroup command

This commit is contained in:
Jun Nishimura 2023-09-01 23:53:31 +09:00
parent 08be2cb981
commit 02094ac5c5

View file

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