mirror of
https://github.com/spf13/cobra
synced 2025-04-27 17:17:20 +00:00
Merge af165be453
into 01ffff4eca
This commit is contained in:
commit
e1b5d54f0a
1 changed files with 25 additions and 1 deletions
26
command.go
26
command.go
|
@ -168,6 +168,9 @@ type Command struct {
|
||||||
usageFunc func(*Command) error
|
usageFunc func(*Command) error
|
||||||
// usageTemplate is usage template defined by user.
|
// usageTemplate is usage template defined by user.
|
||||||
usageTemplate string
|
usageTemplate string
|
||||||
|
// flagParseFunc is func defined by user and it's called when the command is
|
||||||
|
// parsing the flags.
|
||||||
|
flagParseFunc func(*Command, []string) error
|
||||||
// flagErrorFunc is func defined by user and it's called when the parsing of
|
// flagErrorFunc is func defined by user and it's called when the parsing of
|
||||||
// flags returns an error.
|
// flags returns an error.
|
||||||
flagErrorFunc func(*Command, error) error
|
flagErrorFunc func(*Command, error) error
|
||||||
|
@ -315,6 +318,11 @@ func (c *Command) SetUsageTemplate(s string) {
|
||||||
c.usageTemplate = s
|
c.usageTemplate = s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetFlagParseFunc sets a function to parse flags.
|
||||||
|
func (c *Command) SetFlagParseFunc(f func(*Command, []string) error) {
|
||||||
|
c.flagParseFunc = f
|
||||||
|
}
|
||||||
|
|
||||||
// SetFlagErrorFunc sets a function to generate an error when flag parsing
|
// SetFlagErrorFunc sets a function to generate an error when flag parsing
|
||||||
// fails.
|
// fails.
|
||||||
func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) {
|
func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) {
|
||||||
|
@ -496,6 +504,22 @@ func (c *Command) UsageString() string {
|
||||||
return bb.String()
|
return bb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FlagParseFunc returns either the function set by SetFlagParseFunc for this
|
||||||
|
// command or a parent, or it returns a function which calls the original
|
||||||
|
// flag parse function.
|
||||||
|
func (c *Command) FlagParseFunc() (f func(*Command, []string) error) {
|
||||||
|
if c.flagParseFunc != nil {
|
||||||
|
return c.flagParseFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.HasParent() {
|
||||||
|
return c.parent.FlagParseFunc()
|
||||||
|
}
|
||||||
|
return func(c *Command, args []string) error {
|
||||||
|
return c.Flags().Parse(args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FlagErrorFunc returns either the function set by SetFlagErrorFunc for this
|
// FlagErrorFunc returns either the function set by SetFlagErrorFunc for this
|
||||||
// command or a parent, or it returns a function which returns the original
|
// command or a parent, or it returns a function which returns the original
|
||||||
// error.
|
// error.
|
||||||
|
@ -1846,7 +1870,7 @@ func (c *Command) ParseFlags(args []string) error {
|
||||||
// do it here after merging all flags and just before parse
|
// do it here after merging all flags and just before parse
|
||||||
c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist)
|
c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist)
|
||||||
|
|
||||||
err := c.Flags().Parse(args)
|
err := c.FlagParseFunc()(c, args)
|
||||||
// Print warnings if they occurred (e.g. deprecated flag messages).
|
// Print warnings if they occurred (e.g. deprecated flag messages).
|
||||||
if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {
|
if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {
|
||||||
c.Print(c.flagErrorBuf.String())
|
c.Print(c.flagErrorBuf.String())
|
||||||
|
|
Loading…
Add table
Reference in a new issue