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

Customizable error message prefix #2023

Merged
merged 9 commits into from
Sep 8, 2023
16 changes: 14 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,18 @@ func (c *Command) VersionTemplate() string {
`
}

// ErrPrefix return error message prefix for the command
func (c *Command) ErrPrefix() string {
if c.errPrefix != "" {
return c.errPrefix
}

if c.HasParent() {
return c.parent.ErrPrefix()
}
return "Error:"
}

func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
flag := fs.Lookup(name)
if flag == nil {
Expand Down Expand Up @@ -1058,7 +1070,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
c = cmd
}
if !c.SilenceErrors {
c.PrintErrln(c.errPrefix, err.Error())
c.PrintErrln(c.ErrPrefix(), err.Error())
c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath())
}
return c, err
Expand Down Expand Up @@ -1087,7 +1099,7 @@ 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(c.errPrefix, err.Error())
c.PrintErrln(c.ErrPrefix(), err.Error())
5ouma marked this conversation as resolved.
Show resolved Hide resolved
}

// If root command has SilenceUsage flagged,
Expand Down
12 changes: 12 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,18 @@ func TestShorthandVersionTemplate(t *testing.T) {
checkStringContains(t, output, "customized version: 1.0.0")
}

func TestErrPrefix(t *testing.T) {
5ouma marked this conversation as resolved.
Show resolved Hide resolved
rootCmd := &Command{Use: "root", SilenceUsage: true, Run: emptyRun}
rootCmd.SetErrPrefix("customized error prefix:")

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

checkStringContains(t, output, "customized error prefix: unknown flag: --unknown")
}

func TestVersionFlagExecutedOnSubcommand(t *testing.T) {
rootCmd := &Command{Use: "root", Version: "1.0.0"}
rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})
Expand Down
6 changes: 6 additions & 0 deletions site/content/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,12 @@ Running an application with the '--version' flag will print the version to stdou
the version template. The template can be customized using the
`cmd.SetVersionTemplate(s string)` function.

## Error Message Prefix

Cobra prints an error message when being returned a non-nil error value.
5ouma marked this conversation as resolved.
Show resolved Hide resolved
The default error message is `Error: error contents`.
5ouma marked this conversation as resolved.
Show resolved Hide resolved
The Prefix, `Error:` can be customized using the `cmd.SetErrPrefix(s string)` function.

## PreRun and PostRun Hooks

It is possible to run functions before or after the main `Run` function of your command. The `PersistentPreRun` and `PreRun` functions will be executed before `Run`. `PersistentPostRun` and `PostRun` will be executed after `Run`. The `Persistent*Run` functions will be inherited by children if they do not declare their own. These functions are run in the following order:
Expand Down
Loading