Skip to content

Commit

Permalink
style: replace stringInSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed Mar 20, 2019
1 parent 1d7d1b3 commit 178cb83
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
9 changes: 0 additions & 9 deletions cobra.go
Expand Up @@ -189,12 +189,3 @@ func ld(s, t string, ignoreCase bool) int {
}
return d[len(s)][len(t)]
}

func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
12 changes: 7 additions & 5 deletions command.go
Expand Up @@ -732,7 +732,7 @@ func (c *Command) execute(a []string) (err error) {
argWoFlags = a
}

if err := c.ValidateArgs(argWoFlags); err != nil {
if err := c.validateArgs(argWoFlags); err != nil {
return err
}

Expand Down Expand Up @@ -871,14 +871,16 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
return cmd, err
}

func (c *Command) ValidateArgs(args []string) error {
func (c *Command) validateArgs(args []string) error {
if c.Args == nil {
return nil
}
if len(c.ValidArgs) > 0 {
for _, v := range args {
if !stringInSlice(v, c.ValidArgs) {
return fmt.Errorf("invalid argument %q for %q%s", v, c.CommandPath(), c.findSuggestions(args[0]))
const p = "$"
s := p + strings.Join(c.ValidArgs, p) + p
for _, a := range args {
if !strings.Contains(s, p+a+p) {
return fmt.Errorf("invalid argument %q for %q%s", a, c.CommandPath(), c.findSuggestions(args[0]))
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions command_test.go
Expand Up @@ -1414,11 +1414,13 @@ func TestSortedFlags(t *testing.T) {
if i == len(names) {
return
}
if stringInSlice(f.Name, names) {
if names[i] != f.Name {
t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)
for _, name := range names {
if name == f.Name {
if names[i] != f.Name {
t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)
}
i++
}
i++
}
})
}
Expand Down

0 comments on commit 178cb83

Please sign in to comment.