Skip to content
Myr

Merge pull request #305 from fabianofranz/expose_out

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

Fixes how we expose help and usage and expose output without making assumptions

authored by Eric Paris · committed by GitHub · parents a272c3c, d6bf4ef · Browse files · 2 files+16 added−33 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+16 added−33 removed

  • other:+16 added−33 removed

2 files+16 added−33 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+15 added-32 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 {
16 unmodified lines
204 if c.HasParent() {205 return c.parent.UsageFunc()206 }207 return func(c *Command) error {208 err := c.Usage()208 c.mergePersistentFlags()209 err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)210 if err != nil {211 c.Println(err)212 }213 return err214 }215}216217// HelpFunc returns either the function set by SetHelpFunc for this command217// or a parent, or it returns a function which calls c.Help()218// or a parent, or it returns a function with default help behavior219func (c *Command) HelpFunc() func(*Command, []string) {220 cmd := c221 for cmd != nil {222 if cmd.helpFunc != nil {
1 unmodified line
224 }225 cmd = cmd.parent226 }227 return func(*Command, []string) {227 err := c.Help()228 c.mergePersistentFlags()229 err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)230 if err != nil {231 c.Println(err)232 }233 }234}235234// 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}251236func (c *Command) UsageString() string {237 tmpOutput := c.output238 bb := new(bytes.Buffer)239 c.SetOutput(bb)256 c.Usage()240 c.UsageFunc()(c)241 c.output = tmpOutput242 return bb.String()243}244
474 unmodified lines
719 Run: func(c *Command, args []string) {720 cmd, _, e := c.Root().Find(args)721 if cmd == nil || e != nil {722 c.Printf("Unknown help topic %#q.", args)739 c.Root().Usage()723 c.Root().UsageFunc()(cmd)724 } else {741 helpFunc := cmd.HelpFunc()742 helpFunc(cmd, args)725 cmd.HelpFunc()(cmd, args)726 }727 },728 }729 }
85 unmodified lines
815 }816 }817}818836// Print is a convenience method to Print to the defined output819// Print is a convenience method to Print to the defined output, fallback to Stderr if not set820func (c *Command) Print(i ...interface{}) {838 fmt.Fprint(c.getOutOrStderr(), i...)821 fmt.Fprint(c.OutOrStderr(), i...)822}823841// Println is a convenience method to Println to the defined output824// Println is a convenience method to Println to the defined output, fallback to Stderr if not set825func (c *Command) Println(i ...interface{}) {826 str := fmt.Sprintln(i...)827 c.Print(str)828}829847// Printf is a convenience method to Printf to the defined output830// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set831func (c *Command) Printf(format string, i ...interface{}) {832 str := fmt.Sprintf(format, i...)833 c.Print(str)834}
407 unmodified lines