Skip to content
Myr

Expose OutOrStdout and OutOrStderr and don't make assumptions if output is not set

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 a272c3c · Browse files · 2 files+10 added−10 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+10 added−10 removed

  • other:+10 added−10 removed

2 files+10 added−10 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.getOutOrStdout(), "%v", args)649 fmt.Fprintf(cmd.OutOrStdout(), "%v", args)650 },651 }652 first.AddCommand(second)653
535 unmodified lines
modified:command.go+9 added-9 removed
108 unmodified lines
109110 flagErrorBuf *bytes.Buffer111112 args []string // actual args parsed from flags113 output *io.Writer // nil means stderr; use Out() method instead113 output *io.Writer // out writer if set in SetOutput(w)114 usageFunc func(*Command) error // Usage can be defined by application115 usageTemplate string // Can be defined by Application116 helpTemplate string // Can be defined by Application117 helpFunc func(*Command, []string) // Help can be defined by application
57 unmodified lines
175 command.SetGlobalNormalizationFunc(n)176 }177}178179func (c *Command) getOutOrStdout() io.Writer {179func (c *Command) OutOrStdout() io.Writer {180 return c.getOut(os.Stdout)181}182183func (c *Command) getOutOrStderr() io.Writer {183func (c *Command) OutOrStderr() io.Writer {184 return c.getOut(os.Stderr)185}186187func (c *Command) getOut(def io.Writer) io.Writer {
47 unmodified lines
235// 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)239 err := tmpl(c.OutOrStderr(), 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)248 err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)249 return err250}251252func (c *Command) UsageString() string {
579 unmodified lines
832 }833 }834}835836// Print is a convenience method to Print to the defined output836// Print is a convenience method to Print to the defined output, fallback to Stderr if not set837func (c *Command) Print(i ...interface{}) {838 fmt.Fprint(c.getOutOrStderr(), i...)838 fmt.Fprint(c.OutOrStderr(), i...)839}840841// Println is a convenience method to Println to the defined output841// Println is a convenience method to Println to the defined output, fallback to Stderr if not set842func (c *Command) Println(i ...interface{}) {843 str := fmt.Sprintln(i...)844 c.Print(str)845}846847// Printf is a convenience method to Printf to the defined output847// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set848func (c *Command) Printf(format string, i ...interface{}) {849 str := fmt.Sprintf(format, i...)850 c.Print(str)851}
407 unmodified lines