Skip to content

Commit 23645f3

Browse files
committed
Use slices.Contains
This replaces a few loops with a call to slices.Contains. The slices package was added in Go 1.21. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent a107ee4 commit 23645f3

2 files changed

Lines changed: 10 additions & 18 deletions

File tree

flag.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"regexp"
7+
"slices"
78
"strings"
89
"time"
910
)
@@ -208,13 +209,7 @@ func FlagNames(name string, aliases []string) []string {
208209
}
209210

210211
func hasFlag(flags []Flag, fl Flag) bool {
211-
for _, existing := range flags {
212-
if fl == existing {
213-
return true
214-
}
215-
}
216-
217-
return false
212+
return slices.Contains(flags, fl)
218213
}
219214

220215
func flagSplitMultiValues(val string, sliceSeparator string, disableSliceSeparator bool) []string {

help.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"slices"
89
"strings"
910
"text/tabwriter"
1011
"text/template"
@@ -204,10 +205,8 @@ func cliArgContains(flagName string, args []string) bool {
204205
count = 2
205206
}
206207
flag := fmt.Sprintf("%s%s", strings.Repeat("-", count), name)
207-
for _, a := range args {
208-
if a == flag {
209-
return true
210-
}
208+
if slices.Contains(args, flag) {
209+
return true
211210
}
212211
}
213212
return false
@@ -496,13 +495,11 @@ func checkShellCompleteFlag(c *Command, arguments []string) (bool, []string) {
496495
return false, arguments
497496
}
498497

499-
for _, arg := range arguments {
500-
// If arguments include "--", shell completion is disabled
501-
// because after "--" only positional arguments are accepted.
502-
// https://unix.stackexchange.com/a/11382
503-
if arg == "--" {
504-
return false, arguments[:pos]
505-
}
498+
// If arguments include "--", shell completion is disabled
499+
// because after "--" only positional arguments are accepted.
500+
// https://unix.stackexchange.com/a/11382
501+
if slices.Contains(arguments, "--") {
502+
return false, arguments[:pos]
506503
}
507504

508505
return true, arguments[:pos]

0 commit comments

Comments
 (0)