Skip to content
Myr

Help command must use Stdout instead of Stderr

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

authored by Fabiano Franz · committed by Fabiano Franz · parent 6a8bd97 · 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

2 files+53 added−51 removed

modified:cobra_test.go+1 added-1 removed
644 unmodified lines
645646 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 lines
modified:command.go+52 added-50 removed
132 unmodified lines
133func (c *Command) SetArgs(a []string) {134 c.args = a135}136137func (c *Command) getOut(def io.Writer) io.Writer {138 if c.output != nil {139 return *c.output140 }141142 if c.HasParent() {143 return c.parent.Out()144 }145 return def146}147148func (c *Command) Out() io.Writer {149 return c.getOut(os.Stderr)150}151152func (c *Command) getOutOrStdout() io.Writer {153 return c.getOut(os.Stdout)154}155137// SetOutput sets the destination for usage and error messages.138// If output is nil, os.Stderr is used.139func (c *Command) SetOutput(output io.Writer) {140 c.output = &output
34 unmodified lines
175 command.SetGlobalNormalizationFunc(n)176 }177}178179func (c *Command) getOutOrStdout() io.Writer {180 return c.getOut(os.Stdout)181}182183func (c *Command) getOutOrStderr() io.Writer {184 return c.getOut(os.Stderr)185}186187func (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}196197// UsageFunc returns either the function set by SetUsageFunc for this command198// or a parent, or it returns a default usage function199func (c *Command) UsageFunc() (f func(*Command) error) {200 if c.usageFunc != nil {201 return c.usageFunc202 }
1 unmodified line
204 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 lines
230 }231 }232}233234// Output the usage for the command235// Used when a user provides invalid input236// Can be defined by user by overriding UsageFunc237func (c *Command) Usage() error {238 c.mergePersistentFlags()239 err := tmpl(c.getOutOrStderr(), c.UsageTemplate(), c)240 return err241}242243// Output the help for the command244// Used when a user calls help [command]245// by the default HelpFunc in the commander246func (c *Command) Help() error {247 c.mergePersistentFlags()248 err := tmpl(c.getOutOrStdout(), c.HelpTemplate(), c)249 return err250}251252func (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}260261var minUsagePadding = 25262263func (c *Command) UsagePadding() int {264 if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {
299 unmodified lines
564 // flag in initHelpFlag()565 c.Println("\"help\" flag declared as non-bool. Please correct your code")566 return err567 }568569 if helpVal || !c.Runnable() {570 return flag.ErrHelp571 }572
189 unmodified lines
762763// Commands returns a sorted slice of child commands.764func (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.commands
63 unmodified lines
834}835836// Print is a convenience method to Print to the defined output837func (c *Command) Print(i ...interface{}) {809 fmt.Fprint(c.Out(), i...)838 fmt.Fprint(c.getOutOrStderr(), i...)839}840841// Println is a convenience method to Println to the defined output842func (c *Command) Println(i ...interface{}) {
6 unmodified lines
849 str := fmt.Sprintf(format, i...)850 c.Print(str)851}852824// Output the usage for the command825// Used when a user provides invalid input826// Can be defined by user by overriding UsageFunc827func (c *Command) Usage() error {828 c.mergePersistentFlags()829 err := c.UsageFunc()(c)830 return err831}832833// Output the help for the command834// Used when a user calls help [command]835// by the default HelpFunc in the commander836func (c *Command) Help() error {837 c.mergePersistentFlags()838 err := tmpl(c.getOutOrStdout(), c.HelpTemplate(), c)839 return err840}841842func (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}850853// CommandPath returns the full path to this command.854func (c *Command) CommandPath() string {855 str := c.Name()856 x := c
402 unmodified lines