Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1623) Send error to stderr instead of stdout #1629

Merged
merged 3 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ type App struct {
didSetup bool

rootCommand *Command

// if the app is in error mode
isInError bool
}

// Setup runs initialization code to ensure all data structures are ready for
Expand Down Expand Up @@ -420,6 +423,17 @@ func runFlagActions(c *Context, fs []Flag) error {
return nil
}

func (a *App) writer() io.Writer {
if a.isInError {
// this can happen in test but not in normal usage
if a.ErrWriter == nil {
return os.Stderr
}
return a.ErrWriter
}
return a.Writer
}

func checkStringSliceIncludes(want string, sSlice []string) bool {
found := false
for _, s := range sSlice {
Expand Down
6 changes: 4 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,16 @@ func (c *Command) Run(cCtx *Context, arguments ...string) (err error) {
}

if err != nil {
cCtx.App.isInError = true
if c.OnUsageError != nil {
err = c.OnUsageError(cCtx, err, !c.isRoot)
cCtx.App.handleExitCoder(cCtx, err)
return err
}
_, _ = fmt.Fprintf(cCtx.App.Writer, "%s %s\n\n", "Incorrect Usage:", err.Error())
_, _ = fmt.Fprintf(cCtx.App.writer(), "%s %s\n\n", "Incorrect Usage:", err.Error())
if cCtx.App.Suggest {
if suggestion, err := c.suggestFlagFromError(err, ""); err == nil {
fmt.Fprintf(cCtx.App.Writer, "%s", suggestion)
fmt.Fprintf(cCtx.App.writer(), "%s", suggestion)
}
}
if !c.HideHelp {
Expand Down Expand Up @@ -203,6 +204,7 @@ func (c *Command) Run(cCtx *Context, arguments ...string) (err error) {

cerr := cCtx.checkRequiredFlags(c.Flags)
if cerr != nil {
cCtx.App.isInError = true
_ = ShowSubcommandHelp(cCtx)
return cerr
}
Expand Down
20 changes: 10 additions & 10 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var helpCommand = &Command{
if templ == "" {
templ = CommandHelpTemplate
}
HelpPrinter(cCtx.App.Writer, templ, cCtx.Command)
HelpPrinter(cCtx.App.writer(), templ, cCtx.Command)
return nil
}
return ShowSubcommandHelp(cCtx)
Expand Down Expand Up @@ -125,7 +125,7 @@ func ShowAppHelp(cCtx *Context) error {
}

if cCtx.App.ExtraInfo == nil {
HelpPrinter(cCtx.App.Writer, tpl, cCtx.App)
HelpPrinter(cCtx.App.writer(), tpl, cCtx.App)
return nil
}

Expand All @@ -134,7 +134,7 @@ func ShowAppHelp(cCtx *Context) error {
"ExtraInfo": cCtx.App.ExtraInfo,
}
}
HelpPrinterCustom(cCtx.App.Writer, tpl, cCtx.App, customAppData())
HelpPrinterCustom(cCtx.App.writer(), tpl, cCtx.App, customAppData())

return nil
}
Expand Down Expand Up @@ -213,23 +213,23 @@ func DefaultCompleteWithFlags(cmd *Command) func(cCtx *Context) {

if strings.HasPrefix(lastArg, "-") {
if cmd != nil {
printFlagSuggestions(lastArg, cmd.Flags, cCtx.App.Writer)
printFlagSuggestions(lastArg, cmd.Flags, cCtx.App.writer())

return
}

printFlagSuggestions(lastArg, cCtx.App.Flags, cCtx.App.Writer)
printFlagSuggestions(lastArg, cCtx.App.Flags, cCtx.App.writer())

return
}
}

if cmd != nil {
printCommandSuggestions(cmd.Commands, cCtx.App.Writer)
printCommandSuggestions(cmd.Commands, cCtx.App.writer())
return
}

printCommandSuggestions(cCtx.App.Commands, cCtx.App.Writer)
printCommandSuggestions(cCtx.App.Commands, cCtx.App.writer())
}
}

Expand Down Expand Up @@ -264,7 +264,7 @@ func ShowCommandHelp(ctx *Context, command string) error {
}
}

HelpPrinter(ctx.App.Writer, templ, c)
HelpPrinter(ctx.App.writer(), templ, c)

return nil
}
Expand Down Expand Up @@ -296,7 +296,7 @@ func ShowSubcommandHelp(cCtx *Context) error {
return nil
}

HelpPrinter(cCtx.App.Writer, SubcommandHelpTemplate, cCtx.Command)
HelpPrinter(cCtx.App.writer(), SubcommandHelpTemplate, cCtx.Command)
return nil
}

Expand All @@ -306,7 +306,7 @@ func ShowVersion(cCtx *Context) {
}

func printVersion(cCtx *Context) {
_, _ = fmt.Fprintf(cCtx.App.Writer, "%v version %v\n", cCtx.App.Name, cCtx.App.Version)
_, _ = fmt.Fprintf(cCtx.App.writer(), "%v version %v\n", cCtx.App.Name, cCtx.App.Version)
}

// ShowCompletions prints the lists of commands within a given context
Expand Down
3 changes: 3 additions & 0 deletions suggestions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"errors"
"fmt"
"os"
"testing"
)

Expand Down Expand Up @@ -123,6 +124,7 @@ func TestSuggestCommand(t *testing.T) {
func ExampleApp_Suggest() {
app := &App{
Name: "greet",
ErrWriter: os.Stdout,
Suggest: true,
HideHelp: false,
HideHelpCommand: true,
Expand All @@ -148,6 +150,7 @@ func ExampleApp_Suggest() {
func ExampleApp_Suggest_command() {
app := &App{
Name: "greet",
ErrWriter: os.Stdout,
Suggest: true,
HideHelpCommand: true,
CustomAppHelpTemplate: "(this space intentionally left blank)\n",
Expand Down