Get rid of flagErrorBuf (#417)

Fix #27
This commit is contained in:
Albert Nigmatzianov 2017-04-19 15:23:43 +02:00 committed by Eric Paris
parent f95d58bdf3
commit b655df6ce8

View File

@ -110,8 +110,6 @@ type Command struct {
// is commands slice are sorted or not // is commands slice are sorted or not
commandsAreSorted bool commandsAreSorted bool
flagErrorBuf *bytes.Buffer
args []string // actual args parsed from flags args []string // actual args parsed from flags
output io.Writer // out writer if set in SetOutput(w) output io.Writer // out writer if set in SetOutput(w)
usageFunc func(*Command) error // Usage can be defined by application usageFunc func(*Command) error // Usage can be defined by application
@ -674,17 +672,6 @@ func (c *Command) preRun() {
} }
} }
func (c *Command) errorMsgFromParse() string {
s := c.flagErrorBuf.String()
x := strings.Split(s, "\n")
if len(x) > 0 {
return x[0]
}
return ""
}
// Execute Call execute to use the args (os.Args[1:] by default) // Execute Call execute to use the args (os.Args[1:] by default)
// and run through the command tree finding appropriate matches // and run through the command tree finding appropriate matches
// for commands and then corresponding flags. // for commands and then corresponding flags.
@ -948,7 +935,6 @@ func (c *Command) DebugFlags() {
} }
}) })
} }
c.Println(x.flagErrorBuf)
if x.HasSubCommands() { if x.HasSubCommands() {
for _, y := range x.commands { for _, y := range x.commands {
debugflags(y) debugflags(y)
@ -1089,10 +1075,7 @@ func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) f
func (c *Command) Flags() *flag.FlagSet { func (c *Command) Flags() *flag.FlagSet {
if c.flags == nil { if c.flags == nil {
c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
if c.flagErrorBuf == nil { c.flags.SetOutput(c.OutOrStderr())
c.flagErrorBuf = new(bytes.Buffer)
}
c.flags.SetOutput(c.flagErrorBuf)
} }
return c.flags return c.flags
} }
@ -1166,22 +1149,17 @@ func (c *Command) NonInheritedFlags() *flag.FlagSet {
func (c *Command) PersistentFlags() *flag.FlagSet { func (c *Command) PersistentFlags() *flag.FlagSet {
if c.pflags == nil { if c.pflags == nil {
c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
if c.flagErrorBuf == nil { c.pflags.SetOutput(c.OutOrStderr())
c.flagErrorBuf = new(bytes.Buffer)
}
c.pflags.SetOutput(c.flagErrorBuf)
} }
return c.pflags return c.pflags
} }
// ResetFlags is used in testing. // ResetFlags is used in testing.
func (c *Command) ResetFlags() { func (c *Command) ResetFlags() {
c.flagErrorBuf = new(bytes.Buffer)
c.flagErrorBuf.Reset()
c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.flags.SetOutput(c.flagErrorBuf) c.flags.SetOutput(c.OutOrStderr())
c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.pflags.SetOutput(c.flagErrorBuf) c.pflags.SetOutput(c.OutOrStderr())
} }
// HasFlags checks if the command contains any flags (local plus persistent from the entire structure). // HasFlags checks if the command contains any flags (local plus persistent from the entire structure).
@ -1271,10 +1249,7 @@ func (c *Command) mergePersistentFlags() {
// Save the set of local flags // Save the set of local flags
if c.lflags == nil { if c.lflags == nil {
c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
if c.flagErrorBuf == nil { c.lflags.SetOutput(c.OutOrStderr())
c.flagErrorBuf = new(bytes.Buffer)
}
c.lflags.SetOutput(c.flagErrorBuf)
addtolocal := func(f *flag.Flag) { addtolocal := func(f *flag.Flag) {
c.lflags.AddFlag(f) c.lflags.AddFlag(f)
} }