Skip to content
Myr

Merge pull request #283 from dnephin/add_flag_usage_func

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add support for setting a function to handle flag parsing errors

authored by Eric Paris · committed by GitHub · parents 9c28e4b, 67feb81 · Browse files · 3 files+55 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+55 added−7 removed

  • other:+55 added−7 removed

3 files+55 added−7 removed

modified:doc/md_docs.md-1 removed
modified:command_test.go+26 added
1package cobra23import (4 "bytes"5 "fmt"6 "os"7 "reflect"8 "testing"9)
165 unmodified lines
175 }176177 EnableCommandSorting = true178}179180func TestFlagErrorFunc(t *testing.T) {181182 cmd := &Command{183 Use: "print",184 RunE: func(cmd *Command, args []string) error {185 return nil186 },187 }188 expectedFmt := "This is expected: %s"189190 cmd.SetFlagErrorFunc(func(c *Command, err error) error {191 return fmt.Errorf(expectedFmt, err)192 })193 cmd.SetArgs([]string{"--bogus-flag"})194 cmd.SetOutput(new(bytes.Buffer))195196 err := cmd.Execute()197198 expected := fmt.Sprintf(expectedFmt, "unknown flag: --bogus-flag")199 if err.Error() != expected {200 t.Errorf("expected %v, got %v", expected, err.Error())201 }202}
modified:command.go+29 added-6 removed
107 unmodified lines
108 commandsAreSorted bool109110 flagErrorBuf *bytes.Buffer111112 args []string // actual args parsed from flags113 output *io.Writer // out writer if set in SetOutput(w)114 usageFunc func(*Command) error // Usage can be defined by application115 usageTemplate string // Can be defined by Application112 args []string // actual args parsed from flags113 output *io.Writer // out writer if set in SetOutput(w)114 usageFunc func(*Command) error // Usage can be defined by application115 usageTemplate string // Can be defined by Application116 flagErrorFunc func(*Command, error) error117 helpTemplate string // Can be defined by Application118 helpFunc func(*Command, []string) // Help can be defined by application119 helpCommand *Command // The help command120 // The global normalization function that we can use on every pFlag set and children commands
29 unmodified lines
150func (c *Command) SetUsageTemplate(s string) {151 c.usageTemplate = s152}153154// SetFlagErrorFunc sets a function to generate an error when flag parsing155// fails156func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) {157 c.flagErrorFunc = f158}159153// Can be defined by Application.160// Can be defined by Application161func (c *Command) SetHelpFunc(f func(*Command, []string)) {162 c.helpFunc = f163}164
98 unmodified lines
263 c.output = tmpOutput264 return bb.String()265}266267// FlagErrorFunc returns either the function set by SetFlagErrorFunc for this268// command or a parent, or it returns a function which returns the original269// error.270func (c *Command) FlagErrorFunc() (f func(*Command, error) error) {271 if c.flagErrorFunc != nil {272 return c.flagErrorFunc273 }274275 if c.HasParent() {276 return c.parent.FlagErrorFunc()277 }278 return func(c *Command, err error) error {279 return err280 }281}282283var minUsagePadding = 25284285func (c *Command) UsagePadding() int {286 if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {
288 unmodified lines
575 c.initHelpFlag()576577 err = c.ParseFlags(a)578 if err != nil {556 return err579 return c.FlagErrorFunc()(c, err)580 }581 // If help is called, regardless of other flags, return we want help582 // Also say we need help if the command isn't runnable.583 helpVal, err := c.Flags().GetBool("help")
697 unmodified lines