Skip to content
Open
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
28 changes: 26 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ type Command struct {

// errPrefix is the error message prefix defined by user.
errPrefix string
// errPrefixEmpty tracks if the user explicitly wants an empty errPrefix.
errPrefixEmpty bool

// inReader is a reader defined by the user that replaces stdin
inReader io.Reader
Expand Down Expand Up @@ -373,8 +375,19 @@ func (c *Command) SetVersionTemplate(s string) {
}

// SetErrPrefix sets error message prefix to be used. Application can use it to set custom prefix.
// To unset the error prefix, this function should be called with the empty string.
// To prevent an error prefix from being used, the function SetErrPrefixEmpty(true) should be used.
func (c *Command) SetErrPrefix(s string) {
c.errPrefix = s

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this function is called with an argument "" it should unset the error prefix which should cause ErrPrefix() to check if the parent has an error prefix.

This won't happen if SetErrPrefixEmpty(true) has been called before.
I think we also need to call c.errPrefixEmpty = false in this function to truly unset the error prefix

c.errPrefixEmpty = false
}

// SetErrPrefixEmpty sets whether the error message prefix should be empty.
// If set to true, this suppresses the default "Error: " prefix.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please a comment along the lines of to unset the error prefix (so the parent chain can be checked), please use SetErrPrefix("")

// To unset the error prefix (so the parent chain can be checked), please use SetErrPrefix("").
func (c *Command) SetErrPrefixEmpty(empty bool) {
c.errPrefixEmpty = empty

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same idea that if SetErrPrefix("hello") was called before, this function should undo that, or else the error prefix will not actually show as empty.

So we should set c.errPrefix = "" here I think

c.errPrefix = ""
}

// SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.
Expand Down Expand Up @@ -644,6 +657,9 @@ func (c *Command) ErrPrefix() string {
if c.errPrefix != "" {
return c.errPrefix
}
if c.errPrefixEmpty {
return ""
}

if c.HasParent() {
return c.parent.ErrPrefix()
Expand Down Expand Up @@ -1128,7 +1144,11 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
c = cmd
}
if !c.SilenceErrors {
c.PrintErrln(c.ErrPrefix(), err.Error())
if errPrefix := c.ErrPrefix(); errPrefix != "" {
c.PrintErrln(errPrefix, err.Error())
} else {
c.PrintErrln(err.Error())
}
c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath())
}
return c, err
Expand Down Expand Up @@ -1157,7 +1177,11 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
// If root command has SilenceErrors flagged,
// all subcommands should respect it
if !cmd.SilenceErrors && !c.SilenceErrors {
c.PrintErrln(cmd.ErrPrefix(), err.Error())
if errPrefix := cmd.ErrPrefix(); errPrefix != "" {
c.PrintErrln(errPrefix, err.Error())
} else {
c.PrintErrln(err.Error())
}
}

// If root command has SilenceUsage flagged,
Expand Down
33 changes: 33 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,39 @@ func TestRootAndSubErrPrefix(t *testing.T) {
}
}

func TestRootErrPrefixEmpty(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
rootCmd.SetErrPrefixEmpty(true)

output, err := executeCommand(rootCmd, "--unknown-root-flag")
if err == nil {
t.Errorf("Expected error")
}

checkStringOmits(t, output, "Error: ")
checkStringContains(t, output, "unknown flag: --unknown-root-flag")

// Ensure there is no leading space which used to happen with empty prefix
if strings.HasPrefix(output, " ") {
t.Errorf("Expected no leading space in error output, got %q", output)
}
}

func TestSetErrPrefixEmptyStringRestoresDefault(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
rootCmd.SetErrPrefix("custom error:")

// Setting it to "" should restore the default "Error:" prefix for backward compatibility
rootCmd.SetErrPrefix("")

output, err := executeCommand(rootCmd, "--unknown-root-flag")
if err == nil {
t.Errorf("Expected error")
}

checkStringContains(t, output, "Error: unknown flag: --unknown-root-flag")
}

func TestVersionFlagExecutedOnSubcommand(t *testing.T) {
rootCmd := &Command{Use: "root", Version: "1.0.0"}
rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})
Expand Down
Loading