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
Provenance
- Commit
b620a75- Tree
d4d0c4b- Author
Eric Paris <eparis@redhat.com>- Committer
GitHub <noreply@github.com>- Parent 1
9c28e4b- Parent 2
67feb81
3 files changed+55 added−7 removed
modified:doc/md_docs.md-1 removed
99 unmodified lines10010110210310499 unmodified lines base := strings.TrimSuffix(name, path.Ext(name)) return "/commands/" + strings.ToLower(base) + "/"}```
modified:command_test.go+26 added
1 package cobra2 3 import (4+ "bytes"5+ "fmt"6 "os"7 "reflect"8 "testing"9 )165 unmodified lines175 }176 177 EnableCommandSorting = true178 }179+180+func TestFlagErrorFunc(t *testing.T) {181+182+ cmd := &Command{183+ Use: "print",184+ RunE: func(cmd *Command, args []string) error {185+ return nil186+ },187+ }188+ expectedFmt := "This is expected: %s"189+190+ 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))195+196+ err := cmd.Execute()197+198+ 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 lines108 commandsAreSorted bool109 110 flagErrorBuf *bytes.Buffer111 112- 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 commands29 unmodified lines150 func (c *Command) SetUsageTemplate(s string) {151 c.usageTemplate = s152 }153 154+// SetFlagErrorFunc sets a function to generate an error when flag parsing155+// fails156+func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) {157+ c.flagErrorFunc = f158+}159+153-// Can be defined by Application.160+// Can be defined by Application161 func (c *Command) SetHelpFunc(f func(*Command, []string)) {162 c.helpFunc = f163 }164 98 unmodified lines263 c.output = tmpOutput264 return bb.String()265 }266 267+// FlagErrorFunc returns either the function set by SetFlagErrorFunc for this268+// command or a parent, or it returns a function which returns the original269+// error.270+func (c *Command) FlagErrorFunc() (f func(*Command, error) error) {271+ if c.flagErrorFunc != nil {272+ return c.flagErrorFunc273+ }274+275+ if c.HasParent() {276+ return c.parent.FlagErrorFunc()277+ }278+ return func(c *Command, err error) error {279+ return err280+ }281+}282+283 var minUsagePadding = 25284 285 func (c *Command) UsagePadding() int {286 if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {288 unmodified lines575 c.initHelpFlag()576 577 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