From 20217d8f5e2a9f356ef9079c7fa79e71fadbeac1 Mon Sep 17 00:00:00 2001 From: Fabiano Franz Date: Fri, 15 Jul 2016 17:12:07 -0300 Subject: [PATCH] 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 15b88930..881a621d 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 75b48cb2..d2c30852 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)