Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type Command struct {
// default behavior.
ExitErrHandler ExitErrHandlerFunc `json:"-"`
// Other custom info
Metadata map[string]interface{} `json:"metadata"`
Metadata map[string]any `json:"metadata"`
// Carries a function which returns app specific info.
ExtraInfo func() map[string]string `json:"-"`
// CustomRootCommandHelpTemplate the text template for app help topic.
Expand Down Expand Up @@ -559,7 +559,7 @@ func (cmd *Command) Count(name string) int {
}

// Value returns the value of the flag corresponding to `name`
func (cmd *Command) Value(name string) interface{} {
func (cmd *Command) Value(name string) any {
if fs := cmd.lookupFlag(name); fs != nil {
tracef("value found for name %[1]q (cmd=%[2]q)", name, cmd.Name)
return fs.Get()
Expand Down
4 changes: 2 additions & 2 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,7 @@ func TestCommandHelpPrinter(t *testing.T) {
}()

wasCalled := false
HelpPrinter = func(io.Writer, string, interface{}) {
HelpPrinter = func(io.Writer, string, any) {
wasCalled = true
}

Expand Down Expand Up @@ -5420,7 +5420,7 @@ func TestCommand_ExclusiveFlagsWithAfter(t *testing.T) {
func TestCommand_ParallelRun(t *testing.T) {
t.Parallel()

for i := 0; i < 10; i++ {
for i := range 10 {
t.Run(fmt.Sprintf("run_%d", i), func(t *testing.T) {
t.Parallel()

Expand Down
16 changes: 8 additions & 8 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ func TestCompletionSubcommand(t *testing.T) {
args []string
contains string
msg string
msgArgs []interface{}
msgArgs []any
notContains bool
}{
{
name: "subcommand general completion",
args: []string{"foo", "bar", completionFlag},
contains: "xyz",
msg: "Expected output to contain shell name %[1]q",
msgArgs: []interface{}{
msgArgs: []any{
"xyz",
},
},
Expand All @@ -86,7 +86,7 @@ func TestCompletionSubcommand(t *testing.T) {
args: []string{"foo", "bar", "-", completionFlag},
contains: "l1",
msg: "Expected output to contain shell name %[1]q",
msgArgs: []interface{}{
msgArgs: []any{
"l1",
},
},
Expand All @@ -95,7 +95,7 @@ func TestCompletionSubcommand(t *testing.T) {
args: []string{"foo", "bar", "--", completionFlag},
contains: "l1",
msg: "Expected output to contain shell name %[1]q",
msgArgs: []interface{}{
msgArgs: []any{
"l1",
},
notContains: true,
Expand All @@ -105,7 +105,7 @@ func TestCompletionSubcommand(t *testing.T) {
args: []string{"foo", "bar", "xyz", completionFlag},
contains: "-g",
msg: "Expected output to contain flag %[1]q",
msgArgs: []interface{}{
msgArgs: []any{
"-g",
},
notContains: true,
Expand All @@ -115,7 +115,7 @@ func TestCompletionSubcommand(t *testing.T) {
args: []string{"foo", "bar", "xyz", "-", completionFlag},
contains: "-g",
msg: "Expected output to contain flag %[1]q",
msgArgs: []interface{}{
msgArgs: []any{
"-g",
},
},
Expand All @@ -124,7 +124,7 @@ func TestCompletionSubcommand(t *testing.T) {
args: []string{"foo", "bar", "xyz", "--", completionFlag},
contains: "-g",
msg: "Expected output to contain flag %[1]q",
msgArgs: []interface{}{
msgArgs: []any{
"-g",
},
notContains: true,
Expand All @@ -134,7 +134,7 @@ func TestCompletionSubcommand(t *testing.T) {
args: []string{"foo", "bar", "xyz", "--", "sargs", completionFlag},
contains: "-g",
msg: "Expected output to contain flag %[1]q",
msgArgs: []interface{}{
msgArgs: []any{
"-g",
},
notContains: true,
Expand Down
10 changes: 5 additions & 5 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ func unquoteUsage(usage string) (string, string) {
}

func prefixedNames(names []string, placeholder string) string {
var prefixed string
var prefixed strings.Builder
for i, name := range names {
if name == "" {
continue
}

prefixed += prefixFor(name) + name
prefixed.WriteString(prefixFor(name) + name)
if placeholder != "" {
prefixed += " " + placeholder
prefixed.WriteString(" " + placeholder)
}
if i < len(names)-1 {
prefixed += ", "
prefixed.WriteString(", ")
}
}
return prefixed
return prefixed.String()
}

func envFormat(envVars []string, prefix, sep, suffix string) string {
Expand Down
9 changes: 2 additions & 7 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"regexp"
"slices"
"strings"
"time"
)
Expand Down Expand Up @@ -208,13 +209,7 @@ func FlagNames(name string, aliases []string) []string {
}

func hasFlag(flags []Flag, fl Flag) bool {
for _, existing := range flags {
if fl == existing {
return true
}
}

return false
return slices.Contains(flags, fl)
}

func flagSplitMultiValues(val string, sliceSeparator string, disableSliceSeparator bool) []string {
Expand Down
2 changes: 1 addition & 1 deletion flag_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (b *boolValue) Set(s string) error {
return err
}

func (b *boolValue) Get() interface{} { return *b.destination }
func (b *boolValue) Get() any { return *b.destination }

func (b *boolValue) String() string {
return strconv.FormatBool(*b.destination)
Expand Down
2 changes: 1 addition & 1 deletion flag_map_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (i *MapBase[T, C, VC]) Value() map[string]T {
}

// Get returns the mapping of values set by this flag
func (i *MapBase[T, C, VC]) Get() interface{} {
func (i *MapBase[T, C, VC]) Get() any {
return *i.dict
}

Expand Down
2 changes: 1 addition & 1 deletion flag_slice_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (i *SliceBase[T, C, VC]) Value() []T {
}

// Get returns the slice of values set by this flag
func (i *SliceBase[T, C, VC]) Get() interface{} {
func (i *SliceBase[T, C, VC]) Get() any {
return *i.slice
}

Expand Down
2 changes: 1 addition & 1 deletion flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (p *Parser) String() string {
return fmt.Sprintf("%s,%s", p[0], p[1])
}

func (p *Parser) Get() interface{} {
func (p *Parser) Get() any {
return p
}

Expand Down
8 changes: 4 additions & 4 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ type Command struct {
// default behavior.
ExitErrHandler ExitErrHandlerFunc `json:"-"`
// Other custom info
Metadata map[string]interface{} `json:"metadata"`
Metadata map[string]any `json:"metadata"`
// Carries a function which returns app specific info.
ExtraInfo func() map[string]string `json:"-"`
// CustomRootCommandHelpTemplate the text template for app help topic.
Expand Down Expand Up @@ -777,7 +777,7 @@ func (cmd *Command) UintSlice(name string) []uint
UintSlice looks up the value of a local UintSliceFlag, returns nil if not
found

func (cmd *Command) Value(name string) interface{}
func (cmd *Command) Value(name string) any
Value returns the value of the flag corresponding to `name`

func (cmd *Command) VisibleCategories() []CommandCategory
Expand Down Expand Up @@ -1225,7 +1225,7 @@ func NewMapBase[T any, C any, VC ValueCreator[T, C]](defaults map[string]T) *Map

func (i MapBase[T, C, VC]) Create(val map[string]T, p *map[string]T, c C) Value

func (i *MapBase[T, C, VC]) Get() interface{}
func (i *MapBase[T, C, VC]) Get() any
Get returns the mapping of values set by this flag

func (i *MapBase[T, C, VC]) Serialize() string
Expand Down Expand Up @@ -1312,7 +1312,7 @@ func NewSliceBase[T any, C any, VC ValueCreator[T, C]](defaults ...T) *SliceBase

func (i SliceBase[T, C, VC]) Create(val []T, p *[]T, c C) Value

func (i *SliceBase[T, C, VC]) Get() interface{}
func (i *SliceBase[T, C, VC]) Get() any
Get returns the slice of values set by this flag

func (i *SliceBase[T, C, VC]) Serialize() string
Expand Down
19 changes: 8 additions & 11 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"slices"
"strings"
"text/tabwriter"
"text/template"
Expand Down Expand Up @@ -204,10 +205,8 @@ func cliArgContains(flagName string, args []string) bool {
count = 2
}
flag := fmt.Sprintf("%s%s", strings.Repeat("-", count), name)
for _, a := range args {
if a == flag {
return true
}
if slices.Contains(args, flag) {
return true
}
}
return false
Expand Down Expand Up @@ -496,13 +495,11 @@ func checkShellCompleteFlag(c *Command, arguments []string) (bool, []string) {
return false, arguments
}

for _, arg := range arguments {
// If arguments include "--", shell completion is disabled
// because after "--" only positional arguments are accepted.
// https://unix.stackexchange.com/a/11382
if arg == "--" {
return false, arguments[:pos]
}
// If arguments include "--", shell completion is disabled
// because after "--" only positional arguments are accepted.
// https://unix.stackexchange.com/a/11382
if slices.Contains(arguments, "--") {
return false, arguments[:pos]
}

return true, arguments[:pos]
Expand Down
36 changes: 18 additions & 18 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func TestShowCommandHelp_HelpPrinter(t *testing.T) {
{
name: "no-command",
template: "",
printer: func(w io.Writer, _ string, _ interface{}) {
printer: func(w io.Writer, _ string, _ any) {
fmt.Fprint(w, "yo")
},
command: "",
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestShowCommandHelp_HelpPrinter(t *testing.T) {
defer func(old HelpPrinterFunc) {
HelpPrinter = old
}(HelpPrinter)
HelpPrinter = func(w io.Writer, templ string, data interface{}) {
HelpPrinter = func(w io.Writer, templ string, data any) {
assert.Equal(t, tt.wantTemplate, templ, "template mismatch")
tt.printer(w, templ, data)
}
Expand Down Expand Up @@ -947,7 +947,7 @@ func TestShowRootCommandHelp_HelpPrinter(t *testing.T) {
{
name: "standard-command",
template: "",
printer: func(w io.Writer, _ string, _ interface{}) {
printer: func(w io.Writer, _ string, _ any) {
fmt.Fprint(w, "yo")
},
wantTemplate: RootCommandHelpTemplate,
Expand All @@ -956,7 +956,7 @@ func TestShowRootCommandHelp_HelpPrinter(t *testing.T) {
{
name: "custom-template-command",
template: "{{doublecho .Name}}",
printer: func(w io.Writer, templ string, data interface{}) {
printer: func(w io.Writer, templ string, data any) {
// Pass a custom function to ensure it gets used
fm := map[string]any{"doublecho": doublecho}
DefaultPrintHelpCustom(w, templ, data, fm)
Expand All @@ -971,7 +971,7 @@ func TestShowRootCommandHelp_HelpPrinter(t *testing.T) {
defer func(old HelpPrinterFunc) {
HelpPrinter = old
}(HelpPrinter)
HelpPrinter = func(w io.Writer, templ string, data interface{}) {
HelpPrinter = func(w io.Writer, templ string, data any) {
assert.Equal(t, tt.wantTemplate, templ, "unexpected template")
tt.printer(w, templ, data)
}
Expand Down Expand Up @@ -1006,7 +1006,7 @@ func TestShowRootCommandHelp_HelpPrinterCustom(t *testing.T) {
{
name: "standard-command",
template: "",
printer: func(w io.Writer, _ string, _ interface{}, _ map[string]interface{}) {
printer: func(w io.Writer, _ string, _ any, _ map[string]any) {
fmt.Fprint(w, "yo")
},
wantTemplate: RootCommandHelpTemplate,
Expand All @@ -1015,7 +1015,7 @@ func TestShowRootCommandHelp_HelpPrinterCustom(t *testing.T) {
{
name: "custom-template-command",
template: "{{doublecho .Name}}",
printer: func(w io.Writer, templ string, data interface{}, _ map[string]interface{}) {
printer: func(w io.Writer, templ string, data any, _ map[string]any) {
// Pass a custom function to ensure it gets used
fm := map[string]any{"doublecho": doublecho}
DefaultPrintHelpCustom(w, templ, data, fm)
Expand All @@ -1030,7 +1030,7 @@ func TestShowRootCommandHelp_HelpPrinterCustom(t *testing.T) {
defer func(old HelpPrinterCustomFunc) {
HelpPrinterCustom = old
}(HelpPrinterCustom)
HelpPrinterCustom = func(w io.Writer, templ string, data interface{}, fm map[string]interface{}) {
HelpPrinterCustom = func(w io.Writer, templ string, data any, fm map[string]any) {
assert.Nil(t, fm, "unexpected function map passed")
assert.Equal(t, tt.wantTemplate, templ, "unexpected template")
tt.printer(w, templ, data, fm)
Expand Down Expand Up @@ -1514,8 +1514,8 @@ Including newlines.
And then another long line. Blah blah blah does anybody ever read these things?`,
}

HelpPrinter = func(w io.Writer, templ string, data interface{}) {
funcMap := map[string]interface{}{
HelpPrinter = func(w io.Writer, templ string, data any) {
funcMap := map[string]any{
"wrapAt": func() int {
return 30
},
Expand Down Expand Up @@ -1597,8 +1597,8 @@ func TestWrappedCommandHelp(t *testing.T) {
cmd.setupDefaults([]string{"cli.test"})
cmd.setupCommandGraph()

HelpPrinter = func(w io.Writer, templ string, data interface{}) {
funcMap := map[string]interface{}{
HelpPrinter = func(w io.Writer, templ string, data any) {
funcMap := map[string]any{
"wrapAt": func() int {
return 30
},
Expand Down Expand Up @@ -1665,8 +1665,8 @@ func TestWrappedSubcommandHelp(t *testing.T) {
},
}

HelpPrinter = func(w io.Writer, templ string, data interface{}) {
funcMap := map[string]interface{}{
HelpPrinter = func(w io.Writer, templ string, data any) {
funcMap := map[string]any{
"wrapAt": func() int {
return 30
},
Expand Down Expand Up @@ -1737,8 +1737,8 @@ func TestWrappedHelpSubcommand(t *testing.T) {
},
}

HelpPrinter = func(w io.Writer, templ string, data interface{}) {
funcMap := map[string]interface{}{
HelpPrinter = func(w io.Writer, templ string, data any) {
funcMap := map[string]any{
"wrapAt": func() int {
return 30
},
Expand Down Expand Up @@ -1821,8 +1821,8 @@ func TestCategorizedHelp(t *testing.T) {
},
}

HelpPrinter = func(w io.Writer, templ string, data interface{}) {
funcMap := map[string]interface{}{
HelpPrinter = func(w io.Writer, templ string, data any) {
funcMap := map[string]any{
"wrapAt": func() int {
return 30
},
Expand Down
Loading