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
1 changed files with 9 additions and 5 deletions

View File

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