From f75985f797bb3dbcca37926a158d2a9657cb45af Mon Sep 17 00:00:00 2001 From: yanghao05 Date: Thu, 6 Jul 2023 11:29:26 +0800 Subject: [PATCH] add silent call --- cmd_root.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd_root.go b/cmd_root.go index 0ee7bf3..05c94a2 100644 --- a/cmd_root.go +++ b/cmd_root.go @@ -34,6 +34,14 @@ func Serve(providers container.Providers, opts ...Option) error { opt(rootCmd) } + rootCmd.SilenceErrors = true + rootCmd.SilenceUsage = true + rootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { + cmd.Println(err) + cmd.Println(cmd.UsageString()) + return err + }) + defaultCfgFile := fmt.Sprintf(".%s.toml", rootCmd.Use) rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file path, lookup in dir: $HOME, $PWD, /etc, /usr/local/etc, filename: "+defaultCfgFile)