Merge pull request #303 from fabianofranz/testing_help_exiting_1
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Help command must use Stdout instead of Stderr
authored by Eric Paris · committed by GitHub · parents 6a8bd97, 77530f5 · Browse files · 2 files·+53 added−51 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+53 added−51 removed
- other:+53 added−51 removed
Provenance
- Commit
a272c3c- Tree
82b174f- Author
Eric Paris <eparis@redhat.com>- Committer
GitHub <noreply@github.com>- Parent 1
6a8bd97- Parent 2
77530f5
2 files changed+53 added−51 removed
modified:cobra_test.go+1 added-1 removed
644 unmodified lines645 646 second := &Command{647 Use: "second",648 Run: func(cmd *Command, args []string) {649- fmt.Fprintf(cmd.Out(), "%v", args)649+ fmt.Fprintf(cmd.getOutOrStdout(), "%v", args)650 },651 }652 first.AddCommand(second)653 535 unmodified linesmodified:command.go+52 added-50 removed
132 unmodified lines133 func (c *Command) SetArgs(a []string) {134 c.args = a135 }136 137-func (c *Command) getOut(def io.Writer) io.Writer {138- if c.output != nil {139- return *c.output140- }141-142- if c.HasParent() {143- return c.parent.Out()144- }145- return def146-}147-148-func (c *Command) Out() io.Writer {149- return c.getOut(os.Stderr)150-}151-152-func (c *Command) getOutOrStdout() io.Writer {153- return c.getOut(os.Stdout)154-}155-137 // SetOutput sets the destination for usage and error messages.138 // If output is nil, os.Stderr is used.139 func (c *Command) SetOutput(output io.Writer) {140 c.output = &output34 unmodified lines175 command.SetGlobalNormalizationFunc(n)176 }177 }178 179+func (c *Command) getOutOrStdout() io.Writer {180+ return c.getOut(os.Stdout)181+}182+183+func (c *Command) getOutOrStderr() io.Writer {184+ return c.getOut(os.Stderr)185+}186+187+func (c *Command) getOut(def io.Writer) io.Writer {188+ if c.output != nil {189+ return *c.output190+ }191+ if c.HasParent() {192+ return c.parent.getOut(def)193+ }194+ return def195+}196+197+// UsageFunc returns either the function set by SetUsageFunc for this command198+// or a parent, or it returns a default usage function199 func (c *Command) UsageFunc() (f func(*Command) error) {200 if c.usageFunc != nil {201 return c.usageFunc202 }1 unmodified line204 if c.HasParent() {205 return c.parent.UsageFunc()206 }207 return func(c *Command) error {207- err := tmpl(c.Out(), c.UsageTemplate(), c)208+ err := c.Usage()209 if err != nil {209- fmt.Print(err)210+ c.Println(err)211 }212 return err213 }214 }15 unmodified lines230 }231 }232 }233 234+// Output the usage for the command235+// Used when a user provides invalid input236+// Can be defined by user by overriding UsageFunc237+func (c *Command) Usage() error {238+ c.mergePersistentFlags()239+ err := tmpl(c.getOutOrStderr(), c.UsageTemplate(), c)240+ return err241+}242+243+// Output the help for the command244+// Used when a user calls help [command]245+// by the default HelpFunc in the commander246+func (c *Command) Help() error {247+ c.mergePersistentFlags()248+ err := tmpl(c.getOutOrStdout(), c.HelpTemplate(), c)249+ return err250+}251+252+func (c *Command) UsageString() string {253+ tmpOutput := c.output254+ bb := new(bytes.Buffer)255+ c.SetOutput(bb)256+ c.Usage()257+ c.output = tmpOutput258+ return bb.String()259+}260+261 var minUsagePadding = 25262 263 func (c *Command) UsagePadding() int {264 if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {299 unmodified lines564 // flag in initHelpFlag()565 c.Println("\"help\" flag declared as non-bool. Please correct your code")566 return err567 }568+569 if helpVal || !c.Runnable() {570 return flag.ErrHelp571 }572 189 unmodified lines762 763 // Commands returns a sorted slice of child commands.764 func (c *Command) Commands() []*Command {765 // do not sort commands if it already sorted or sorting was disabled737- if EnableCommandSorting && !c.commandsAreSorted{766+ if EnableCommandSorting && !c.commandsAreSorted {767 sort.Sort(commandSorterByName(c.commands))768 c.commandsAreSorted = true769 }770 return c.commands63 unmodified lines834 }835 836 // Print is a convenience method to Print to the defined output837 func (c *Command) Print(i ...interface{}) {809- fmt.Fprint(c.Out(), i...)838+ fmt.Fprint(c.getOutOrStderr(), i...)839 }840 841 // Println is a convenience method to Println to the defined output842 func (c *Command) Println(i ...interface{}) {6 unmodified lines849 str := fmt.Sprintf(format, i...)850 c.Print(str)851 }852 824-// Output the usage for the command825-// Used when a user provides invalid input826-// Can be defined by user by overriding UsageFunc827-func (c *Command) Usage() error {828- c.mergePersistentFlags()829- err := c.UsageFunc()(c)830- return err831-}832-833-// Output the help for the command834-// Used when a user calls help [command]835-// by the default HelpFunc in the commander836-func (c *Command) Help() error {837- c.mergePersistentFlags()838- err := tmpl(c.getOutOrStdout(), c.HelpTemplate(), c)839- return err840-}841-842-func (c *Command) UsageString() string {843- tmpOutput := c.output844- bb := new(bytes.Buffer)845- c.SetOutput(bb)846- c.Usage()847- c.output = tmpOutput848- return bb.String()849-}850-853 // CommandPath returns the full path to this command.854 func (c *Command) CommandPath() string {855 str := c.Name()856 x := c402 unmodified lines