From f8e1ec56bdd7494d309c69681267859a6bfb7549 Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Fri, 13 Feb 2015 19:45:00 -0500 Subject: [PATCH] new NonInheritedFlags() which give all flags which were not persisted from a parent command --- command.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/command.go b/command.go index 3cf7db28..6ac3ea70 100644 --- a/command.go +++ b/command.go @@ -765,6 +765,22 @@ func (c *Command) InheritedFlags() *flag.FlagSet { return local } +// All Flags which were not inherited from parent commands +func (c *Command) NonInheritedFlags() *flag.FlagSet { + c.mergePersistentFlags() + + local := flag.NewFlagSet(c.Name(), flag.ContinueOnError) + inheritedFlags := c.InheritedFlags() + + c.Flags().VisitAll(func(f *flag.Flag) { + if inheritedFlags.Lookup(f.Name) == nil { + local.AddFlag(f) + } + }) + + return local +} + // Get the Persistent FlagSet specifically set in the current command func (c *Command) PersistentFlags() *flag.FlagSet { if c.pflags == nil {