Skip to content
Myr

Ensure the usage command is printed only once

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Reverse a swap in logic introduced in #169 that would cause the usage output to be printed twice. Fixes #171

authored by James Bowes · committed by James Bowes · parent 2456266 · Browse files · 2 files+17 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+17 added−5 removed

  • other:+17 added−5 removed

2 files+17 added−5 removed

modified:cobra_test.go+11 added
1073 unmodified lines
1074 if us := c.UsageString(); us != usage {1075 t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us)1076 }1077}10781079func TestUsageIsNotPrintedTwice(t *testing.T) {1080 var cmd = &Command{Use: "root"}1081 var sub = &Command{Use: "sub"}1082 cmd.AddCommand(sub)10831084 r := simpleTester(cmd, "")1085 if strings.Count(r.Output, "Usage:") != 1 {1086 t.Error("Usage output is not printed exactly once")1087 }1088}
modified:command.go+6 added-5 removed
637 unmodified lines
638 }639640 err = cmd.execute(flags)641 if err != nil {642 // If root command has SilentUsage flagged,643 // all subcommands should respect it644 if !cmd.SilenceUsage && !c.SilenceUsage {645 c.Println(cmd.UsageString())646 }642 // If root command has SilentErrors flagged,643 // all subcommands should respect it644 if !cmd.SilenceErrors && !c.SilenceErrors {645 if err == flag.ErrHelp {
1 unmodified line
647 return nil648 }649 c.Println("Error:", err.Error())650 }651652 // If root command has SilentUsage flagged,653 // all subcommands should respect it654 if !cmd.SilenceUsage && !c.SilenceUsage {655 c.Println(cmd.UsageString())656 }657 return err658 }659 return660}
518 unmodified lines