Fix stderr printing functions (#894)
* Fix stderr printing functions Follow-up of #822 * Errors go to stderr as per POSIX * use PrintErrf() instead of extra call to Sprintf() * Error messages should always be printed to os.Stderr. * add test case for Print* redirection Thanks: @bukowa for the patch.
This commit is contained in:
14
command.go
14
command.go
@ -367,7 +367,7 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
|
||||
c.mergePersistentFlags()
|
||||
err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)
|
||||
if err != nil {
|
||||
c.Println(err)
|
||||
c.PrintErrln(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@ -395,7 +395,7 @@ func (c *Command) HelpFunc() func(*Command, []string) {
|
||||
// See https://github.com/spf13/cobra/issues/1002
|
||||
err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
|
||||
if err != nil {
|
||||
c.Println(err)
|
||||
c.PrintErrln(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -938,8 +938,8 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||
c = cmd
|
||||
}
|
||||
if !c.SilenceErrors {
|
||||
c.Println("Error:", err.Error())
|
||||
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
||||
c.PrintErrln("Error:", err.Error())
|
||||
c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath())
|
||||
}
|
||||
return c, err
|
||||
}
|
||||
@ -967,7 +967,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||
// If root command has SilentErrors flagged,
|
||||
// all subcommands should respect it
|
||||
if !cmd.SilenceErrors && !c.SilenceErrors {
|
||||
c.Println("Error:", err.Error())
|
||||
c.PrintErrln("Error:", err.Error())
|
||||
}
|
||||
|
||||
// If root command has SilentUsage flagged,
|
||||
@ -1209,12 +1209,12 @@ func (c *Command) PrintErr(i ...interface{}) {
|
||||
|
||||
// PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set.
|
||||
func (c *Command) PrintErrln(i ...interface{}) {
|
||||
c.Print(fmt.Sprintln(i...))
|
||||
c.PrintErr(fmt.Sprintln(i...))
|
||||
}
|
||||
|
||||
// PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set.
|
||||
func (c *Command) PrintErrf(format string, i ...interface{}) {
|
||||
c.Print(fmt.Sprintf(format, i...))
|
||||
c.PrintErr(fmt.Sprintf(format, i...))
|
||||
}
|
||||
|
||||
// CommandPath returns the full path to this command.
|
||||
|
Reference in New Issue
Block a user