mirror of
https://github.com/spf13/cobra.git
synced 2024-11-15 09:48:19 -07:00
Merge pull request #174 from jbowes/fix-duplicate-usage
Ensure the usage command is printed only once
This commit is contained in:
commit
a082ce41f3
@ -1075,3 +1075,14 @@ func TestAddTemplateFunctions(t *testing.T) {
|
|||||||
t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us)
|
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
command.go
11
command.go
@ -639,11 +639,6 @@ func (c *Command) Execute() (err error) {
|
|||||||
|
|
||||||
err = cmd.execute(flags)
|
err = cmd.execute(flags)
|
||||||
if err != nil {
|
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,
|
// If root command has SilentErrors flagged,
|
||||||
// all subcommands should respect it
|
// all subcommands should respect it
|
||||||
if !cmd.SilenceErrors && !c.SilenceErrors {
|
if !cmd.SilenceErrors && !c.SilenceErrors {
|
||||||
@ -653,6 +648,12 @@ func (c *Command) Execute() (err error) {
|
|||||||
}
|
}
|
||||||
c.Println("Error:", 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 err
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user