From da4990848697bcc204d4a4defc680a27f4e5efb5 Mon Sep 17 00:00:00 2001 From: Burak Ok Date: Wed, 21 Dec 2022 16:57:06 +0100 Subject: [PATCH] Command: Added new method getClashingFlagnames --- command.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/command.go b/command.go index 1b0b17a2..27385c9b 100644 --- a/command.go +++ b/command.go @@ -1641,6 +1641,41 @@ func (c *Command) LocalFlags() *flag.FlagSet { return c.lflags } +// getClashingFlagnames returns all flags which names are used more than once +func (c *Command) getClashingFlagnames() []*flag.Flag { + allFlags := make(map[string]*flag.Flag, 0) + clashingFlagsSet := make(map[*flag.Flag]interface{}, 0) + + checkFlagFunc := func(f *flag.Flag) { + savedFlag, exists := allFlags[f.Name] + if !exists { + allFlags[f.Name] = f + } else if savedFlag != f /* Different flag or not */ { + clashingFlagsSet[f] = nil + clashingFlagsSet[savedFlag] = nil + } + } + + c.VisitParents(func(parent *Command) { + if parent.pflags != nil { + parent.pflags.VisitAll(checkFlagFunc) + } + }) + + if c.pflags != nil { + c.pflags.VisitAll(checkFlagFunc) + } + + // Possibility that we Visit some of the flags which we already visited in c.pflags + c.LocalFlags().VisitAll(checkFlagFunc) + + clashingFlags := make([]*flag.Flag, 0, len(clashingFlagsSet)) + for flag := range clashingFlagsSet { + clashingFlags = append(clashingFlags, flag) + } + return clashingFlags +} + // InheritedFlags returns all flags which were inherited from parent commands. func (c *Command) InheritedFlags() *flag.FlagSet { c.mergePersistentFlags()