From 871b0edae228a6714c4e3dbc71071464b564dfba Mon Sep 17 00:00:00 2001 From: James Bowes Date: Sat, 31 Oct 2015 10:22:19 -0300 Subject: [PATCH] Ensure the usage command is printed only once Reverse a swap in logic introduced in #169 that would cause the usage output to be printed twice. Fixes #171 --- cobra_test.go | 11 +++++++++++ command.go | 11 ++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cobra_test.go b/cobra_test.go index c26633ed..17361c32 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -1075,3 +1075,14 @@ func TestAddTemplateFunctions(t *testing.T) { t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us) } } + +func TestUsageIsNotPrintedTwice(t *testing.T) { + var cmd = &Command{Use: "root"} + var sub = &Command{Use: "sub"} + cmd.AddCommand(sub) + + r := simpleTester(cmd, "") + if strings.Count(r.Output, "Usage:") != 1 { + t.Error("Usage output is not printed exactly once") + } +} diff --git a/command.go b/command.go index f0587f9b..c1873ec7 100644 --- a/command.go +++ b/command.go @@ -639,11 +639,6 @@ func (c *Command) Execute() (err error) { err = cmd.execute(flags) if err != nil { - // If root command has SilentUsage flagged, - // all subcommands should respect it - if !cmd.SilenceUsage && !c.SilenceUsage { - c.Println(cmd.UsageString()) - } // If root command has SilentErrors flagged, // all subcommands should respect it if !cmd.SilenceErrors && !c.SilenceErrors { @@ -653,6 +648,12 @@ func (c *Command) Execute() (err error) { } c.Println("Error:", err.Error()) } + + // If root command has SilentUsage flagged, + // all subcommands should respect it + if !cmd.SilenceUsage && !c.SilenceUsage { + c.Println(cmd.UsageString()) + } return err } return