Set App.ErrWriter in App.Setup()

- Defaults to os.Stderr
- Remove the App.errWriter() function since it is no longer needed
This commit is contained in:
Sebastian Malton 2020-03-31 10:43:36 -04:00
parent 39f9fbd723
commit 22b6dbaad3

19
app.go
View File

@ -163,6 +163,10 @@ func (a *App) Setup() {
a.Writer = os.Stdout a.Writer = os.Stdout
} }
if a.ErrWriter == nil {
a.ErrWriter = os.Stderr
}
var newCommands []*Command var newCommands []*Command
for _, c := range a.Commands { for _, c := range a.Commands {
@ -196,10 +200,6 @@ func (a *App) Setup() {
if a.Metadata == nil { if a.Metadata == nil {
a.Metadata = make(map[string]interface{}) a.Metadata = make(map[string]interface{})
} }
if a.Writer == nil {
a.Writer = os.Stdout
}
} }
func (a *App) newFlagSet() (*flag.FlagSet, error) { func (a *App) newFlagSet() (*flag.FlagSet, error) {
@ -326,7 +326,7 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
// code in the cli.ExitCoder // code in the cli.ExitCoder
func (a *App) RunAndExitOnError() { func (a *App) RunAndExitOnError() {
if err := a.Run(os.Args); err != nil { if err := a.Run(os.Args); err != nil {
_, _ = fmt.Fprintln(a.errWriter(), err) _, _ = fmt.Fprintln(a.ErrWriter, err)
OsExiter(1) OsExiter(1)
} }
} }
@ -480,15 +480,6 @@ func (a *App) VisibleFlags() []Flag {
return visibleFlags(a.Flags) return visibleFlags(a.Flags)
} }
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(fl Flag) { func (a *App) appendFlag(fl Flag) {
if !hasFlag(a.Flags, fl) { if !hasFlag(a.Flags, fl) {
a.Flags = append(a.Flags, fl) a.Flags = append(a.Flags, fl)