Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an option to set custom FlagErrorHandling #1662

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 23 additions & 8 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ import (
// FParseErrWhitelist configures Flag parse errors to be ignored
type FParseErrWhitelist flag.ParseErrorsWhitelist

// FErrorHandling defines how to handle flag parsing errors
type FErrorHandling flag.ErrorHandling

const (
// ContinueOnError will return an err from Parse() if an error is found
ContinueOnError FErrorHandling = iota
// ExitOnError will call os.Exit(2) if an error is found when parsing
ExitOnError
// PanicOnError will panic() if an error is found when parsing flags
PanicOnError
)

// Command is just that, a command for your application.
// E.g. 'go run ...' - 'run' is the command. Cobra requires
// you to define the usage and description as part of your command
Expand Down Expand Up @@ -222,6 +234,9 @@ type Command struct {
// SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions.
// Must be > 0.
SuggestionsMinimumDistance int

// FlagErrorHandling defines how to handle flag parsing errors. Defaults to flag.ContinueOnError.
FlagErrorHandling FErrorHandling
}

// Context returns underlying command context. If command was executed
Expand Down Expand Up @@ -1460,7 +1475,7 @@ func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) f
// to this command (local and persistent declared here and by all parents).
func (c *Command) Flags() *flag.FlagSet {
if c.flags == nil {
c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.flags = flag.NewFlagSet(c.Name(), (flag.ErrorHandling)(c.FlagErrorHandling))
if c.flagErrorBuf == nil {
c.flagErrorBuf = new(bytes.Buffer)
}
Expand All @@ -1474,7 +1489,7 @@ func (c *Command) Flags() *flag.FlagSet {
func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
persistentFlags := c.PersistentFlags()

out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
out := flag.NewFlagSet(c.Name(), (flag.ErrorHandling)(c.FlagErrorHandling))
c.LocalFlags().VisitAll(func(f *flag.Flag) {
if persistentFlags.Lookup(f.Name) == nil {
out.AddFlag(f)
Expand All @@ -1488,7 +1503,7 @@ func (c *Command) LocalFlags() *flag.FlagSet {
c.mergePersistentFlags()

if c.lflags == nil {
c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.lflags = flag.NewFlagSet(c.Name(), (flag.ErrorHandling)(c.FlagErrorHandling))
if c.flagErrorBuf == nil {
c.flagErrorBuf = new(bytes.Buffer)
}
Expand All @@ -1514,7 +1529,7 @@ func (c *Command) InheritedFlags() *flag.FlagSet {
c.mergePersistentFlags()

if c.iflags == nil {
c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.iflags = flag.NewFlagSet(c.Name(), (flag.ErrorHandling)(c.FlagErrorHandling))
if c.flagErrorBuf == nil {
c.flagErrorBuf = new(bytes.Buffer)
}
Expand Down Expand Up @@ -1542,7 +1557,7 @@ func (c *Command) NonInheritedFlags() *flag.FlagSet {
// PersistentFlags returns the persistent FlagSet specifically set in the current command.
func (c *Command) PersistentFlags() *flag.FlagSet {
if c.pflags == nil {
c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.pflags = flag.NewFlagSet(c.Name(), (flag.ErrorHandling)(c.FlagErrorHandling))
if c.flagErrorBuf == nil {
c.flagErrorBuf = new(bytes.Buffer)
}
Expand All @@ -1555,9 +1570,9 @@ func (c *Command) PersistentFlags() *flag.FlagSet {
func (c *Command) ResetFlags() {
c.flagErrorBuf = new(bytes.Buffer)
c.flagErrorBuf.Reset()
c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.flags = flag.NewFlagSet(c.Name(), (flag.ErrorHandling)(c.FlagErrorHandling))
c.flags.SetOutput(c.flagErrorBuf)
c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.pflags = flag.NewFlagSet(c.Name(), (flag.ErrorHandling)(c.FlagErrorHandling))
c.pflags.SetOutput(c.flagErrorBuf)

c.lflags = nil
Expand Down Expand Up @@ -1674,7 +1689,7 @@ func (c *Command) mergePersistentFlags() {
// If c.parentsPflags == nil, it makes new.
func (c *Command) updateParentsPflags() {
if c.parentsPflags == nil {
c.parentsPflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.parentsPflags = flag.NewFlagSet(c.Name(), (flag.ErrorHandling)(c.FlagErrorHandling))
c.parentsPflags.SetOutput(c.flagErrorBuf)
c.parentsPflags.SortFlags = false
}
Expand Down