Fix stripFlags to be more intelligent about what it parses.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Brendan Burns · committed by spf13 · parent 5c91469 · Browse files · 2 files·+108 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+108 added−2 removed
- other:+108 added−2 removed
Provenance
- Commit
bbdea35- Tree
eeb72c0- Author
Brendan Burns <bburns@google.com>- Committer
spf13 <steve.francia@gmail.com>- Parent
5c91469
2 files changed+108 added−2 removed
added:command_test.go+81 added
1+package cobra2+3+import (4+ "reflect"5+ "testing"6+)7+8+func TestStripFlags(t *testing.T) {9+ tests := []struct {10+ input []string11+ output []string12+ }{13+ {14+ []string{"foo", "bar"},15+ []string{"foo", "bar"},16+ },17+ {18+ []string{"foo", "--bar", "-b"},19+ []string{"foo"},20+ },21+ {22+ []string{"-b", "foo", "--bar", "bar"},23+ []string{},24+ },25+ {26+ []string{"-i10", "echo"},27+ []string{"echo"},28+ },29+ {30+ []string{"-i=10", "echo"},31+ []string{"echo"},32+ },33+ {34+ []string{"--int=100", "echo"},35+ []string{"echo"},36+ },37+ {38+ []string{"-ib", "echo", "-bfoo", "baz"},39+ []string{"echo", "baz"},40+ },41+ {42+ []string{"-i=baz", "bar", "-i", "foo", "blah"},43+ []string{"bar", "blah"},44+ },45+ {46+ []string{"--int=baz", "-bbar", "-i", "foo", "blah"},47+ []string{"blah"},48+ },49+ {50+ []string{"--cat", "bar", "-i", "foo", "blah"},51+ []string{"bar", "blah"},52+ },53+ {54+ []string{"-c", "bar", "-i", "foo", "blah"},55+ []string{"bar", "blah"},56+ },57+ }58+59+ cmdPrint := &Command{60+ Use: "print [string to print]",61+ Short: "Print anything to the screen",62+ Long: `an utterly useless command for testing.`,63+ Run: func(cmd *Command, args []string) {64+ tp = args65+ },66+ }67+68+ var flagi int69+ var flagstr string70+ var flagbool bool71+ cmdPrint.Flags().IntVarP(&flagi, "int", "i", 345, "help message for flag int")72+ cmdPrint.Flags().StringVarP(&flagstr, "bar", "b", "bar", "help message for flag string")73+ cmdPrint.Flags().BoolVarP(&flagbool, "cat", "c", false, "help message for flag bool")74+75+ for _, test := range tests {76+ output := stripFlags(test.input, cmdPrint)77+ if !reflect.DeepEqual(test.output, output) {78+ t.Errorf("expected: %v, got: %v", test.output, output)79+ }80+ }81+}modified:command.go+27 added-2 removed
249 unmodified lines250 x.parent = c251 }252 }253 254+// Test if the named flag is a boolean flag.255+func isBooleanFlag(name string, f *flag.FlagSet) bool {256+ flag := f.Lookup(name)257+ if flag == nil {258+ return false259+ }260+ return flag.Value.Type() == "bool"261+}262+263+// Test if the named flag is a boolean flag.264+func isBooleanShortFlag(name string, f *flag.FlagSet) bool {265+ result := false266+ f.VisitAll(func (f *flag.Flag) { if f.Shorthand == name && f.Value.Type() == "bool" { result = true } })267+ return result268+}269+254-func stripFlags(args []string) []string {270+func stripFlags(args []string, c *Command) []string {271 if len(args) < 1 {272 return args273 }274 275 commands := []string{}276 277 inQuote := false278+ inFlag := false279 for _, y := range args {280 if !inQuote {281 switch {282 case strings.HasPrefix(y, "\""):283 inQuote = true284 case strings.Contains(y, "=\""):285 inQuote = true286+ case strings.HasPrefix(y, "--") && !strings.Contains(y, "="):287+ // TODO: this isn't quite right, we should really check ahead for 'true' or 'false'288+ inFlag = !isBooleanFlag(y[2:], c.Flags())289+ case strings.HasPrefix(y, "-") && !strings.Contains(y, "=") && len(y) == 2 && !isBooleanShortFlag(y[1:], c.Flags()):290+ inFlag = true291+ case inFlag:292+ inFlag = false293 case !strings.HasPrefix(y, "-"):294 commands = append(commands, y)295+ inFlag = false296 }297 }298 299 if strings.HasSuffix(y, "\"") && !strings.HasSuffix(y, "\\\"") {29 unmodified lines329 var innerfind func(*Command, []string) (*Command, []string)330 331 innerfind = func(c *Command, args []string) (*Command, []string) {332 if len(args) > 0 && c.HasSubCommands() {308- argsWOflags := stripFlags(args)333+ argsWOflags := stripFlags(args, c)334 if len(argsWOflags) > 0 {335 matches := make([]*Command, 0)336 for _, cmd := range c.commands {337 if cmd.Name() == argsWOflags[0] || cmd.HasAlias(argsWOflags[0]) { // exact name or alias match573 unmodified lines