Skip to content

Commit

Permalink
Output usage only when flag parsing fails pipe-cd#4381
Browse files Browse the repository at this point in the history
  • Loading branch information
ffjlabo committed Jun 8, 2023
1 parent 7366b31 commit d395ee3
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 d395ee3

Please sign in to comment.