[add] The error message prefix

The user can change it by specifying `errPrefix` by `SetErrPrefix`.
This commit is contained in:
Souma 2023-08-31 17:33:06 +09:00 committed by GitHub
parent 285460dca6
commit 48dabba4d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -181,6 +181,9 @@ type Command struct {
// versionTemplate is the version template defined by user. // versionTemplate is the version template defined by user.
versionTemplate string versionTemplate string
// errPrefix is the error message prefix defined by user.
errPrefix string
// inReader is a reader defined by the user that replaces stdin // inReader is a reader defined by the user that replaces stdin
inReader io.Reader inReader io.Reader
// outWriter is a writer defined by the user that replaces stdout // outWriter is a writer defined by the user that replaces stdout
@ -346,6 +349,11 @@ func (c *Command) SetVersionTemplate(s string) {
c.versionTemplate = s c.versionTemplate = s
} }
// SetErrPrefix sets error message prefix to be used. Application can use it to set custom prefix.
func (c *Command) SetErrPrefix(s string) {
c.errPrefix = s
}
// SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands. // SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.
// The user should not have a cyclic dependency on commands. // The user should not have a cyclic dependency on commands.
func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) { func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
@ -1050,7 +1058,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
c = cmd c = cmd
} }
if !c.SilenceErrors { if !c.SilenceErrors {
c.PrintErrln("Error:", err.Error()) c.PrintErrln(c.errPrefix, err.Error())
c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath()) c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath())
} }
return c, err return c, err
@ -1079,7 +1087,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
// If root command has SilenceErrors flagged, // If root command has SilenceErrors flagged,
// all subcommands should respect it // all subcommands should respect it
if !cmd.SilenceErrors && !c.SilenceErrors { if !cmd.SilenceErrors && !c.SilenceErrors {
c.PrintErrln("Error:", err.Error()) c.PrintErrln(c.errPrefix, err.Error())
} }
// If root command has SilenceUsage flagged, // If root command has SilenceUsage flagged,