From 3d7bff8a321b0a0f88b115558bb7e21bec9124ab Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Thu, 27 Apr 2017 14:55:09 +0200 Subject: [PATCH] Revert "Get rid of flagErrorBuf (#417)" This reverts commit b655df6ce84126fe9272a3cd4d70555a3a4cfed4. Reason for revert: duplicate errors from flag. More in #426 Fix #426 --- command.go | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/command.go b/command.go index 79956f3..bb26d20 100644 --- a/command.go +++ b/command.go @@ -114,6 +114,9 @@ type Command struct { // is commands slice are sorted or not commandsAreSorted bool + // flagErrorBuf contains all error messages from pflag. + flagErrorBuf *bytes.Buffer + args []string // actual args parsed from flags output io.Writer // out writer if set in SetOutput(w) usageFunc func(*Command) error // Usage can be defined by application @@ -915,6 +918,7 @@ func (c *Command) DebugFlags() { } }) } + c.Println(x.flagErrorBuf) if x.HasSubCommands() { for _, y := range x.commands { debugflags(y) @@ -1055,7 +1059,10 @@ func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) f func (c *Command) Flags() *flag.FlagSet { if c.flags == nil { c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) - c.flags.SetOutput(c.OutOrStderr()) + if c.flagErrorBuf == nil { + c.flagErrorBuf = new(bytes.Buffer) + } + c.flags.SetOutput(c.flagErrorBuf) } return c.flags @@ -1080,7 +1087,10 @@ func (c *Command) LocalFlags() *flag.FlagSet { if c.lflags == nil { c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) - c.lflags.SetOutput(c.OutOrStderr()) + if c.flagErrorBuf == nil { + c.flagErrorBuf = new(bytes.Buffer) + } + c.lflags.SetOutput(c.flagErrorBuf) } c.lflags.SortFlags = c.Flags().SortFlags @@ -1100,6 +1110,10 @@ func (c *Command) InheritedFlags() *flag.FlagSet { if c.iflags == nil { c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) + if c.flagErrorBuf == nil { + c.flagErrorBuf = new(bytes.Buffer) + } + c.iflags.SetOutput(c.flagErrorBuf) } local := c.LocalFlags() @@ -1120,17 +1134,22 @@ func (c *Command) NonInheritedFlags() *flag.FlagSet { func (c *Command) PersistentFlags() *flag.FlagSet { if c.pflags == nil { c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) - c.pflags.SetOutput(c.OutOrStderr()) + if c.flagErrorBuf == nil { + c.flagErrorBuf = new(bytes.Buffer) + } + c.pflags.SetOutput(c.flagErrorBuf) } return c.pflags } // ResetFlags is used in testing. func (c *Command) ResetFlags() { + c.flagErrorBuf = new(bytes.Buffer) + c.flagErrorBuf.Reset() c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) - c.flags.SetOutput(c.OutOrStderr()) + c.flags.SetOutput(c.flagErrorBuf) c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) - c.pflags.SetOutput(c.OutOrStderr()) + c.pflags.SetOutput(c.flagErrorBuf) } // HasFlags checks if the command contains any flags (local plus persistent from the entire structure). @@ -1229,7 +1248,7 @@ func (c *Command) mergePersistentFlags() { func (c *Command) updateParentsPflags() { if c.parentsPflags == nil { c.parentsPflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) - c.parentsPflags.SetOutput(c.OutOrStderr()) + c.parentsPflags.SetOutput(c.flagErrorBuf) c.parentsPflags.SortFlags = false }