From 20217d8f5e2a9f356ef9079c7fa79e71fadbeac1 Mon Sep 17 00:00:00 2001 From: Fabiano Franz Date: Fri, 15 Jul 2016 17:12:07 -0300 Subject: [PATCH 1/2] Expose OutOrStdout and OutOrStderr and don't make assumptions if output is not set --- cobra_test.go | 2 +- command.go | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cobra_test.go b/cobra_test.go index 15b8893..881a621 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -646,7 +646,7 @@ func TestSubcommandArgEvaluation(t *testing.T) { second := &Command{ Use: "second", Run: func(cmd *Command, args []string) { - fmt.Fprintf(cmd.getOutOrStdout(), "%v", args) + fmt.Fprintf(cmd.OutOrStdout(), "%v", args) }, } first.AddCommand(second) diff --git a/command.go b/command.go index 75b48cb..d2c3085 100644 --- a/command.go +++ b/command.go @@ -110,7 +110,7 @@ type Command struct { flagErrorBuf *bytes.Buffer args []string // actual args parsed from flags - output *io.Writer // nil means stderr; use Out() method instead + output *io.Writer // out writer if set in SetOutput(w) usageFunc func(*Command) error // Usage can be defined by application usageTemplate string // Can be defined by Application helpTemplate string // Can be defined by Application @@ -176,11 +176,11 @@ func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string } } -func (c *Command) getOutOrStdout() io.Writer { +func (c *Command) OutOrStdout() io.Writer { return c.getOut(os.Stdout) } -func (c *Command) getOutOrStderr() io.Writer { +func (c *Command) OutOrStderr() io.Writer { return c.getOut(os.Stderr) } @@ -236,7 +236,7 @@ func (c *Command) HelpFunc() func(*Command, []string) { // Can be defined by user by overriding UsageFunc func (c *Command) Usage() error { c.mergePersistentFlags() - err := tmpl(c.getOutOrStderr(), c.UsageTemplate(), c) + err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c) return err } @@ -245,7 +245,7 @@ func (c *Command) Usage() error { // by the default HelpFunc in the commander func (c *Command) Help() error { c.mergePersistentFlags() - err := tmpl(c.getOutOrStdout(), c.HelpTemplate(), c) + err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c) return err } @@ -833,18 +833,18 @@ main: } } -// Print is a convenience method to Print to the defined output +// Print is a convenience method to Print to the defined output, fallback to Stderr if not set func (c *Command) Print(i ...interface{}) { - fmt.Fprint(c.getOutOrStderr(), i...) + fmt.Fprint(c.OutOrStderr(), i...) } -// Println is a convenience method to Println to the defined output +// Println is a convenience method to Println to the defined output, fallback to Stderr if not set func (c *Command) Println(i ...interface{}) { str := fmt.Sprintln(i...) c.Print(str) } -// Printf is a convenience method to Printf to the defined output +// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set func (c *Command) Printf(format string, i ...interface{}) { str := fmt.Sprintf(format, i...) c.Print(str) From d6bf4ef243b7f3b96d8a4249e63af711ef33d53e Mon Sep 17 00:00:00 2001 From: Fabiano Franz Date: Fri, 15 Jul 2016 17:17:29 -0300 Subject: [PATCH 2/2] Don't expose Usage() and Help() - our interfaces are UsageFunc, HelpFunc and UsageString --- command.go | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/command.go b/command.go index d2c3085..5b64187 100644 --- a/command.go +++ b/command.go @@ -205,7 +205,8 @@ func (c *Command) UsageFunc() (f func(*Command) error) { return c.parent.UsageFunc() } return func(c *Command) error { - err := c.Usage() + c.mergePersistentFlags() + err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c) if err != nil { c.Println(err) } @@ -214,7 +215,7 @@ func (c *Command) UsageFunc() (f func(*Command) error) { } // HelpFunc returns either the function set by SetHelpFunc for this command -// or a parent, or it returns a function which calls c.Help() +// or a parent, or it returns a function with default help behavior func (c *Command) HelpFunc() func(*Command, []string) { cmd := c for cmd != nil { @@ -224,36 +225,19 @@ func (c *Command) HelpFunc() func(*Command, []string) { cmd = cmd.parent } return func(*Command, []string) { - err := c.Help() + c.mergePersistentFlags() + err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c) if err != nil { c.Println(err) } } } -// Output the usage for the command -// Used when a user provides invalid input -// Can be defined by user by overriding UsageFunc -func (c *Command) Usage() error { - c.mergePersistentFlags() - err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c) - return err -} - -// Output the help for the command -// Used when a user calls help [command] -// by the default HelpFunc in the commander -func (c *Command) Help() error { - c.mergePersistentFlags() - err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c) - return err -} - func (c *Command) UsageString() string { tmpOutput := c.output bb := new(bytes.Buffer) c.SetOutput(bb) - c.Usage() + c.UsageFunc()(c) c.output = tmpOutput return bb.String() } @@ -736,10 +720,9 @@ func (c *Command) initHelpCmd() { cmd, _, e := c.Root().Find(args) if cmd == nil || e != nil { c.Printf("Unknown help topic %#q.", args) - c.Root().Usage() + c.Root().UsageFunc()(cmd) } else { - helpFunc := cmd.HelpFunc() - helpFunc(cmd, args) + cmd.HelpFunc()(cmd, args) } }, }