From 6e6b6a9c1918b0e3626f5d6d0ca8ceb517e507ee Mon Sep 17 00:00:00 2001 From: Maciej Szulik Date: Tue, 9 Dec 2014 14:42:53 +0100 Subject: [PATCH] Subcommands flag parsing errors print subcommand usage and not root's command usage Conflicts: command.go --- command.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/command.go b/command.go index e39d857..7b2cf7d 100644 --- a/command.go +++ b/command.go @@ -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 {