-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: permit empty string for ErrPrefix #2392
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 | ||
| c.errPrefixEmpty = false | ||
| } | ||
|
|
||
| // SetErrPrefixEmpty sets whether the error message prefix should be empty. | ||
| // If set to true, this suppresses the default "Error: " prefix. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(""). | ||
| func (c *Command) SetErrPrefixEmpty(empty bool) { | ||
| c.errPrefixEmpty = empty | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same idea that if So we should set |
||
| c.errPrefix = "" | ||
| } | ||
|
|
||
| // SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands. | ||
|
|
@@ -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() | ||
|
|
@@ -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 | ||
|
|
@@ -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, | ||
|
|
||
There was a problem hiding this comment.
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 causeErrPrefix()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 = falsein this function to truly unset the error prefix