Subcommands flag parsing errors print subcommand usage and not root's command usage

Conflicts:
	command.go
This commit is contained in:
Maciej Szulik 2014-12-09 14:42:53 +01:00 committed by spf13
parent 1cb31604a1
commit 6e6b6a9c19

View file

@ -357,14 +357,16 @@ func (c *Command) execute(a []string) (err error) {
if err != nil { if err != nil {
// We're writing subcommand usage to root command's error buffer to have it displayed to the user // We're writing subcommand usage to root command's error buffer to have it displayed to the user
if c.Root().cmdErrorBuf == nil { r := c.Root()
c.Root().cmdErrorBuf = new(bytes.Buffer) if r.cmdErrorBuf == nil {
r.cmdErrorBuf = new(bytes.Buffer)
} }
// for writing the usage to the buffer we need to switch the output temporarily // for writing the usage to the buffer we need to switch the output temporarily
out := c.Out() // since Out() returns root output, you also need to revert that on root
c.SetOutput(c.Root().cmdErrorBuf) out := r.Out()
r.SetOutput(r.cmdErrorBuf)
c.Usage() c.Usage()
c.SetOutput(out) r.SetOutput(out)
return err return err
} else { } else {
// If help is called, regardless of other flags, we print that // If help is called, regardless of other flags, we print that
@ -499,6 +501,8 @@ func (c *Command) initHelp() {
func (c *Command) ResetCommands() { func (c *Command) ResetCommands() {
c.commands = nil c.commands = nil
c.helpCommand = nil c.helpCommand = nil
c.cmdErrorBuf = new(bytes.Buffer)
c.cmdErrorBuf.Reset()
} }
func (c *Command) Commands() []*Command { func (c *Command) Commands() []*Command {