Merge pull request #393 from mattfarina/io-Writer

Provide a variable for writing output (the writer) with a default of os.Stderr
main
Dan Buch 8 years ago
commit ade5513337

@ -82,6 +82,8 @@ type App struct {
Email string Email string
// Writer writer to write output to // Writer writer to write output to
Writer io.Writer Writer io.Writer
// ErrWriter writes error output
ErrWriter io.Writer
// Other custom info // Other custom info
Metadata map[string]interface{} Metadata map[string]interface{}
} }
@ -228,11 +230,11 @@ func (a *App) Run(arguments []string) (err error) {
// DEPRECATED: Another entry point to the cli app, takes care of passing arguments and error handling // DEPRECATED: Another entry point to the cli app, takes care of passing arguments and error handling
func (a *App) RunAndExitOnError() { func (a *App) RunAndExitOnError() {
fmt.Fprintf(os.Stderr, fmt.Fprintf(a.errWriter(),
"DEPRECATED cli.App.RunAndExitOnError. %s See %s\n", "DEPRECATED cli.App.RunAndExitOnError. %s See %s\n",
contactSysadmin, runAndExitOnErrorDeprecationURL) contactSysadmin, runAndExitOnErrorDeprecationURL)
if err := a.Run(os.Args); err != nil { if err := a.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(a.errWriter(), err)
OsExiter(1) OsExiter(1)
} }
} }
@ -377,6 +379,16 @@ func (a *App) hasFlag(flag Flag) bool {
return false return false
} }
func (a *App) errWriter() io.Writer {
// When the app ErrWriter is nil use the package level one.
if a.ErrWriter == nil {
return ErrWriter
}
return a.ErrWriter
}
func (a *App) appendFlag(flag Flag) { func (a *App) appendFlag(flag Flag) {
if !a.hasFlag(flag) { if !a.hasFlag(flag) {
a.Flags = append(a.Flags, flag) a.Flags = append(a.Flags, flag)
@ -422,7 +434,7 @@ func HandleAction(action interface{}, context *Context) (err error) {
vals := reflect.ValueOf(action).Call([]reflect.Value{reflect.ValueOf(context)}) vals := reflect.ValueOf(action).Call([]reflect.Value{reflect.ValueOf(context)})
if len(vals) == 0 { if len(vals) == 0 {
fmt.Fprintf(os.Stderr, fmt.Fprintf(ErrWriter,
"DEPRECATED Action signature. Must be `cli.ActionFunc`. %s See %s\n", "DEPRECATED Action signature. Must be `cli.ActionFunc`. %s See %s\n",
contactSysadmin, appActionDeprecationURL) contactSysadmin, appActionDeprecationURL)
return nil return nil

@ -2,12 +2,17 @@ package cli
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
) )
var OsExiter = os.Exit var OsExiter = os.Exit
// ErrWriter is used to write errors to the user. This can be anything
// implementing the io.Writer interface and defaults to os.Stderr.
var ErrWriter io.Writer = os.Stderr
type MultiError struct { type MultiError struct {
Errors []error Errors []error
} }
@ -69,7 +74,7 @@ func HandleExitCoder(err error) {
if exitErr, ok := err.(ExitCoder); ok { if exitErr, ok := err.(ExitCoder); ok {
if err.Error() != "" { if err.Error() != "" {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(ErrWriter, err)
} }
OsExiter(exitErr.ExitCode()) OsExiter(exitErr.ExitCode())
return return

@ -220,7 +220,7 @@ func (f IntSliceFlag) Apply(set *flag.FlagSet) {
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
err := newVal.Set(s) err := newVal.Set(s)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(ErrWriter, err.Error())
} }
} }
f.Value = newVal f.Value = newVal

@ -188,7 +188,7 @@ func printHelp(out io.Writer, templ string, data interface{}) {
err := t.Execute(w, data) err := t.Execute(w, data)
if err != nil { if err != nil {
// If the writer is closed, t.Execute will fail, and there's nothing // If the writer is closed, t.Execute will fail, and there's nothing
// we can do to recover. We could send this to os.Stderr if we need. // we can do to recover. We could send this to ErrWriter if we need.
return return
} }
w.Flush() w.Flush()

Loading…
Cancel
Save