Skip to content
Myr

Customizable error message prefix (#2023)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Souma · committed by GitHub · parent c5dacb3 · Browse files · 3 files+61 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+61 added−2 removed

  • other:+61 added−2 removed

3 files+61 added−2 removed

modified:site/content/user_guide.md+6 added
594 unmodified lines
595Running an application with the '--version' flag will print the version to stdout using596the version template. The template can be customized using the597`cmd.SetVersionTemplate(s string)` function.598599## Error Message Prefix600601Cobra prints an error message when receiving a non-nil error value.602The default error message is `Error: <error contents>`.603The Prefix, `Error:` can be customized using the `cmd.SetErrPrefix(s string)` function.604605## PreRun and PostRun Hooks606607It 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:608
138 unmodified lines
modified:command_test.go+33 added
1097 unmodified lines
10981099 checkStringContains(t, output, "customized version: 1.0.0")1100}11011102func TestRootErrPrefixExecutedOnSubcommand(t *testing.T) {1103 rootCmd := &Command{Use: "root", Run: emptyRun}1104 rootCmd.SetErrPrefix("root error prefix:")1105 rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})11061107 output, err := executeCommand(rootCmd, "sub", "--unknown-flag")1108 if err == nil {1109 t.Errorf("Expected error")1110 }11111112 checkStringContains(t, output, "root error prefix: unknown flag: --unknown-flag")1113}11141115func TestRootAndSubErrPrefix(t *testing.T) {1116 rootCmd := &Command{Use: "root", Run: emptyRun}1117 subCmd := &Command{Use: "sub", Run: emptyRun}1118 rootCmd.AddCommand(subCmd)1119 rootCmd.SetErrPrefix("root error prefix:")1120 subCmd.SetErrPrefix("sub error prefix:")11211122 if output, err := executeCommand(rootCmd, "--unknown-root-flag"); err == nil {1123 t.Errorf("Expected error")1124 } else {1125 checkStringContains(t, output, "root error prefix: unknown flag: --unknown-root-flag")1126 }11271128 if output, err := executeCommand(rootCmd, "sub", "--unknown-sub-flag"); err == nil {1129 t.Errorf("Expected error")1130 } else {1131 checkStringContains(t, output, "sub error prefix: unknown flag: --unknown-sub-flag")1132 }1133}11341135func TestVersionFlagExecutedOnSubcommand(t *testing.T) {1136 rootCmd := &Command{Use: "root", Version: "1.0.0"}1137 rootCmd.AddCommand(&Command{Use: "sub", Run: emptyRun})1138
1632 unmodified lines
modified:command.go+22 added-2 removed
179 unmodified lines
180181 // versionTemplate is the version template defined by user.182 versionTemplate string183184 // errPrefix is the error message prefix defined by user.185 errPrefix string186187 // inReader is a reader defined by the user that replaces stdin188 inReader io.Reader189 // outWriter is a writer defined by the user that replaces stdout190 outWriter io.Writer
157 unmodified lines
348func (c *Command) SetVersionTemplate(s string) {349 c.versionTemplate = s350}351352// SetErrPrefix sets error message prefix to be used. Application can use it to set custom prefix.353func (c *Command) SetErrPrefix(s string) {354 c.errPrefix = s355}356357// SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.358// The user should not have a cyclic dependency on commands.359func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {360 c.Flags().SetNormalizeFunc(n)
241 unmodified lines
602 return `{{with .Name}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}603`604}605606// ErrPrefix return error message prefix for the command607func (c *Command) ErrPrefix() string {608 if c.errPrefix != "" {609 return c.errPrefix610 }611612 if c.HasParent() {613 return c.parent.ErrPrefix()614 }615 return "Error:"616}617618func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {619 flag := fs.Lookup(name)620 if flag == nil {621 return false
447 unmodified lines
1069 if cmd != nil {1070 c = cmd1071 }1072 if !c.SilenceErrors {1053 c.PrintErrln("Error:", err.Error())1073 c.PrintErrln(c.ErrPrefix(), err.Error())1074 c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath())1075 }1076 return c, err1077 }
20 unmodified lines
10981099 // If root command has SilenceErrors flagged,1100 // all subcommands should respect it1101 if !cmd.SilenceErrors && !c.SilenceErrors {1082 c.PrintErrln("Error:", err.Error())1102 c.PrintErrln(cmd.ErrPrefix(), err.Error())1103 }11041105 // If root command has SilenceUsage flagged,1106 // all subcommands should respect it
750 unmodified lines