From 5049ee8606fe9fcab9c1067598526a1152ce5fb1 Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Fri, 13 Feb 2015 19:44:12 -0500 Subject: [PATCH] new InheritedFlags() command to tell all flags which persisted from a parent --- command.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/command.go b/command.go index bf6f3c79..25947b28 100644 --- a/command.go +++ b/command.go @@ -713,6 +713,34 @@ func (c *Command) LocalFlags() *flag.FlagSet { return local } +// All Flags which were inherited from parents commands +func (c *Command) InheritedFlags() *flag.FlagSet { + c.mergePersistentFlags() + + local := flag.NewFlagSet(c.Name(), flag.ContinueOnError) + + var rmerge func(x *Command) + + rmerge = func(x *Command) { + if x.HasPersistentFlags() { + x.PersistentFlags().VisitAll(func(f *flag.Flag) { + if local.Lookup(f.Name) == nil { + local.AddFlag(f) + } + }) + } + if x.HasParent() { + rmerge(x.parent) + } + } + + if c.HasParent() { + rmerge(c.parent) + } + + return local +} + // Get the Persistent FlagSet specifically set in the current command func (c *Command) PersistentFlags() *flag.FlagSet { if c.pflags == nil {