diff --git a/acmd.go b/acmd.go index 434deab..024cf67 100644 --- a/acmd.go +++ b/acmd.go @@ -171,27 +171,34 @@ func validateCommand(cmd Command) error { return fmt.Errorf("command alias %q must contains only letters, digits, - and _", cmd.Alias) case len(cmds) != 0: - sort.Slice(cmds, func(i, j int) bool { - return cmds[i].Name < cmds[j].Name - }) - - names := make(map[string]struct{}) - for _, cmd := range cmds { - if _, ok := names[cmd.Name]; ok { - return fmt.Errorf("duplicate command %q", cmd.Name) - } - if _, ok := names[cmd.Alias]; ok { - return fmt.Errorf("duplicate command alias %q", cmd.Alias) - } - - names[cmd.Name] = struct{}{} - if cmd.Alias != "" { - names[cmd.Alias] = struct{}{} - } - - if err := validateCommand(cmd); err != nil { - return err - } + if err := validateSubcommands(cmds); err != nil { + return err + } + } + return nil +} + +func validateSubcommands(cmds []Command) error { + sort.Slice(cmds, func(i, j int) bool { + return cmds[i].Name < cmds[j].Name + }) + + names := make(map[string]struct{}) + for _, cmd := range cmds { + if _, ok := names[cmd.Name]; ok { + return fmt.Errorf("duplicate command %q", cmd.Name) + } + if _, ok := names[cmd.Alias]; ok { + return fmt.Errorf("duplicate command alias %q", cmd.Alias) + } + + names[cmd.Name] = struct{}{} + if cmd.Alias != "" { + names[cmd.Alias] = struct{}{} + } + + if err := validateCommand(cmd); err != nil { + return err } } return nil