forked from mirror/cobra
Expose OutOrStdout and OutOrStderr and don't make assumptions if output is not set
This commit is contained in:
parent
a272c3cbd5
commit
20217d8f5e
|
@ -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)
|
||||
|
|
18
command.go
18
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)
|
||||
|
|
Loading…
Reference in New Issue