From 755d026045bf85e0048d56e05e920d3d2fa0d455 Mon Sep 17 00:00:00 2001 From: Yoshiki Fujikane <40124947+ffjlabo@users.noreply.github.com> Date: Wed, 5 Jul 2023 22:46:18 +0900 Subject: [PATCH] Output usage only when flag parsing fails #4381 (#4464) Signed-off-by: Yoshiki Fujikane Signed-off-by: Kenta Kozuka --- pkg/cli/app.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/cli/app.go b/pkg/cli/app.go index 7afb18bf65..8d578d4a30 100644 --- a/pkg/cli/app.go +++ b/pkg/cli/app.go @@ -15,6 +15,7 @@ package cli import ( + "errors" "fmt" "github.com/spf13/cobra" @@ -29,15 +30,23 @@ type App struct { telemetryFlags TelemetryFlags } +var FlagParseErr = errors.New("FlagParseErr") + func NewApp(name, desc string) *App { a := &App{ rootCmd: &cobra.Command{ Use: name, Short: desc, SilenceErrors: true, + SilenceUsage: true, }, telemetryFlags: defaultTelemetryFlags, } + a.rootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { + cmd.Println(err) + cmd.Println(cmd.UsageString()) + return FlagParseErr + }) versionCmd := &cobra.Command{ Use: "version", Short: "Print the information of current binary.",