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{
|
second := &Command{
|
||||||
Use: "second",
|
Use: "second",
|
||||||
Run: func(cmd *Command, args []string) {
|
Run: func(cmd *Command, args []string) {
|
||||||
fmt.Fprintf(cmd.getOutOrStdout(), "%v", args)
|
fmt.Fprintf(cmd.OutOrStdout(), "%v", args)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
first.AddCommand(second)
|
first.AddCommand(second)
|
||||||
|
18
command.go
18
command.go
@ -110,7 +110,7 @@ type Command struct {
|
|||||||
flagErrorBuf *bytes.Buffer
|
flagErrorBuf *bytes.Buffer
|
||||||
|
|
||||||
args []string // actual args parsed from flags
|
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
|
usageFunc func(*Command) error // Usage can be defined by application
|
||||||
usageTemplate string // Can be defined by Application
|
usageTemplate string // Can be defined by Application
|
||||||
helpTemplate 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)
|
return c.getOut(os.Stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) getOutOrStderr() io.Writer {
|
func (c *Command) OutOrStderr() io.Writer {
|
||||||
return c.getOut(os.Stderr)
|
return c.getOut(os.Stderr)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,7 +236,7 @@ func (c *Command) HelpFunc() func(*Command, []string) {
|
|||||||
// Can be defined by user by overriding UsageFunc
|
// Can be defined by user by overriding UsageFunc
|
||||||
func (c *Command) Usage() error {
|
func (c *Command) Usage() error {
|
||||||
c.mergePersistentFlags()
|
c.mergePersistentFlags()
|
||||||
err := tmpl(c.getOutOrStderr(), c.UsageTemplate(), c)
|
err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,7 +245,7 @@ func (c *Command) Usage() error {
|
|||||||
// by the default HelpFunc in the commander
|
// by the default HelpFunc in the commander
|
||||||
func (c *Command) Help() error {
|
func (c *Command) Help() error {
|
||||||
c.mergePersistentFlags()
|
c.mergePersistentFlags()
|
||||||
err := tmpl(c.getOutOrStdout(), c.HelpTemplate(), c)
|
err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
|
||||||
return err
|
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{}) {
|
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{}) {
|
func (c *Command) Println(i ...interface{}) {
|
||||||
str := fmt.Sprintln(i...)
|
str := fmt.Sprintln(i...)
|
||||||
c.Print(str)
|
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{}) {
|
func (c *Command) Printf(format string, i ...interface{}) {
|
||||||
str := fmt.Sprintf(format, i...)
|
str := fmt.Sprintf(format, i...)
|
||||||
c.Print(str)
|
c.Print(str)
|
||||||
|
Loading…
Reference in New Issue
Block a user