Skip to content

Commit

Permalink
Merge pull request spf13#174 from jbowes/fix-duplicate-usage
Browse files Browse the repository at this point in the history
Ensure the usage command is printed only once
  • Loading branch information
eparis committed Nov 2, 2015
2 parents 4f44cb7 + 871b0ed commit a082ce4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 11 additions & 0 deletions cobra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
11 changes: 6 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit a082ce4

Please sign in to comment.