Skip to content

Commit

Permalink
Output usage only when flag parsing fails #4381 (#4464)
Browse files Browse the repository at this point in the history
Signed-off-by: Yoshiki Fujikane <[email protected]>
  • Loading branch information
ffjlabo authored Jul 5, 2023
1 parent 36e2948 commit 0e38b5a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cli

import (
"errors"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -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.",
Expand Down

0 comments on commit 0e38b5a

Please sign in to comment.