Panic on duplicate command names (#317)

This commit is contained in:
Michal Kralik
2022-07-17 11:31:21 +02:00
committed by GitHub
parent 81070a043b
commit f9bc630ef8
2 changed files with 54 additions and 0 deletions
+22
View File
@@ -158,6 +158,11 @@ MAIN:
}
}
// Validate if there are no duplicate names
if err := checkDuplicateNames(node, v); err != nil {
return nil, err
}
// "Unsee" flags.
for _, flag := range node.Flags {
delete(seenFlags, "--"+flag.Name)
@@ -311,3 +316,20 @@ func buildGroupForKey(k *Kong, key string) *Group {
Title: key,
}
}
func checkDuplicateNames(node *Node, v reflect.Value) error {
seenNames := make(map[string]struct{})
for _, node := range node.Children {
if _, ok := seenNames[node.Name]; ok {
name := v.Type().Name()
if name == "" {
name = "<anonymous struct>"
}
return fmt.Errorf("duplicate command name %q in command %q", node.Name, name)
}
seenNames[node.Name] = struct{}{}
}
return nil
}