Skip to content
Myr

Default usage output to stdout

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

If the command has not set an output explicitly everything will go to stderr. This makes a lot of sense, but is a huge PITA for people who want to be able to grep the help output. It is very common for users to want to do command --help | grep flag This patch fixes that by default help output (but not error output like an invalid command) to stdout instead of defaulting to stderr.

authored by Eric Paris · committed by bep · parent 6264dc6 · Browse files · 1 file+11 added−3 removed
This commit

State

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

Diff stats

1 file+11 added−3 removed

  • other:+11 added−3 removed

1 file+11 added−3 removed

modified:command.go+11 added-3 removed
77 unmodified lines
78func (c *Command) SetArgs(a []string) {79 c.args = a80}8182func (c *Command) Out() io.Writer {82func (c *Command) getOut(def io.Writer) io.Writer {83 if c.output != nil {84 return *c.output85 }8687 if c.HasParent() {88 return c.parent.Out()89 } else {90 return os.Stderr90 return def91 }92}9394func (c *Command) Out() io.Writer {95 return c.getOut(os.Stderr)96}9798func (c *Command) getOutOrStdout() io.Writer {99 return c.getOut(os.Stdout)100}101102// SetOutput sets the destination for usage and error messages.103// If output is nil, os.Stderr is used.104func (c *Command) SetOutput(output io.Writer) {105 c.output = &output
559 unmodified lines
665// Used when a user calls help [command]666// by the default HelpFunc in the commander667func (c *Command) Help() error {668 c.mergePersistentFlags()661 err := tmpl(c.Out(), c.HelpTemplate(), c)669 err := tmpl(c.getOutOrStdout(), c.HelpTemplate(), c)670 return err671}672673func (c *Command) UsageString() string {
283 unmodified lines