From 36e08ffd5b980ff06861ad40dfb3113b10642bdd Mon Sep 17 00:00:00 2001 From: spf13 Date: Wed, 4 Sep 2013 00:15:18 -0400 Subject: [PATCH] Parsing persistentFlags properly (mostly) --- cobra.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cobra.go b/cobra.go index 64b47a8..44ef4d0 100644 --- a/cobra.go +++ b/cobra.go @@ -136,7 +136,7 @@ func (c *Command) execute(args []string) (err error) { cmd, a, e := c.Find(args) if e == nil { - cmd.Flags().Parse(a) + err = cmd.ParseFlags(a) argWoFlags := cmd.Flags().Args() cmd.Run(cmd, argWoFlags) return nil @@ -296,16 +296,20 @@ func (c *Command) ParseFlags(args []string) (err error) { // Climbs up the command tree parsing flags from top to bottom func (c *Command) ParsePersistentFlags(args []string) (err error) { - if !c.HasParent() || c.parent.PersistentFlags().Parsed() { - err = c.PersistentFlags().Parse(args) - if err != nil { - return err + if !c.HasParent() || (c.parent.HasPersistentFlags() && c.parent.PersistentFlags().Parsed()) { + if c.HasPersistentFlags() { + err = c.PersistentFlags().Parse(args) + if err != nil { + return err + } } } else { - err = c.parent.ParsePersistentFlags(args) - if err != nil { - return err + if c.HasParent() && c.parent.HasPersistentFlags() { + err = c.parent.ParsePersistentFlags(args) + if err != nil { + return err + } } } - return nil + return }