Skip to content
Open
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
34 changes: 34 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8333,3 +8333,37 @@ func TestCommand_RunAgainChecksRequiredFlagsAndSources(t *testing.T) {
r.NoError(cmd.Run(buildTestContext(t), []string{"app"}))
r.Equal("from-env", cmd.String("name"))
}

func TestMapSourceSliceUsesCommandSeparator(t *testing.T) {
ms := NewMapSource("config", map[any]any{"ports": []any{"alpha,beta", "gamma"}})

inner := NewValueSourceChain(NewMapValueSource("ports", ms))
testCases := []struct {
name string
sources ValueSourceChain
}{
{name: "direct", sources: NewValueSourceChain(NewMapValueSource("ports", ms))},
{name: "nested chain", sources: NewValueSourceChain(&inner)},
}

for _, testCase := range testCases {
name, sources := testCase.name, testCase.sources
t.Run(name, func(t *testing.T) {
var got []string
cmd := &Command{
Name: "root",
SliceFlagSeparator: ";",
Flags: []Flag{
&StringSliceFlag{Name: "ports", Sources: sources},
},
Action: func(_ context.Context, cmd *Command) error {
got = cmd.StringSlice("ports")
return nil
},
}

require.NoError(t, cmd.Run(buildTestContext(t), []string{"root"}))
assert.Equal(t, []string{"alpha,beta", "gamma"}, got)
})
}
}
5 changes: 5 additions & 0 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ func (f *FlagBase[T, C, V]) setMultiValueParsingConfig(c multiValueParsingConfig
if cf, ok := f.value.(multiValueParsingConfigSetter); ok {
cf.setMultiValueParsingConfig(c)
}
for _, source := range f.Sources.Chain {
if setter, ok := source.(interface{ setSliceSeparator(string) }); ok {
setter.setSliceSeparator(c.SliceFlagSeparator)
}
}
}

// newValue creates the flag's value holding the default Value.
Expand Down
41 changes: 36 additions & 5 deletions value_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"fmt"
"os"
"reflect"
"strings"
)

Expand Down Expand Up @@ -91,6 +92,14 @@ func (vsc *ValueSourceChain) Lookup() (string, bool) {
return s, ok
}

func (vsc *ValueSourceChain) setSliceSeparator(separator string) {
for _, source := range vsc.Chain {
if setter, ok := source.(interface{ setSliceSeparator(string) }); ok {
setter.setSliceSeparator(separator)
}
}
}

func (vsc *ValueSourceChain) LookupWithSource() (string, ValueSource, bool) {
for _, src := range vsc.Chain {
if value, found := src.Lookup(); found {
Expand Down Expand Up @@ -230,8 +239,13 @@ func (ms *mapSource) Lookup(name string) (any, bool) {
}

type mapValueSource struct {
key string
ms MapSource
key string
ms MapSource
sliceSeparator string
}

func (mvs *mapValueSource) setSliceSeparator(separator string) {
mvs.sliceSeparator = separator
}

func NewMapValueSource(key string, ms MapSource) ValueSource {
Expand All @@ -250,9 +264,26 @@ func (mvs *mapValueSource) GoString() string {
}

func (mvs *mapValueSource) Lookup() (string, bool) {
if v, ok := mvs.ms.Lookup(mvs.key); !ok {
v, ok := mvs.ms.Lookup(mvs.key)
if !ok {
return "", false
} else {
return fmt.Sprintf("%+v", v), true
}

rv := reflect.ValueOf(v)
if rv.IsValid() && (rv.Kind() == reflect.Slice || rv.Kind() == reflect.Array) {
// Slice and array values use the command-line separator syntax. An
// element containing the configured separator is still split; choose
// a separator that does not appear in the elements.
separator := mvs.sliceSeparator
if separator == "" {
separator = defaultSliceFlagSeparator
}
parts := make([]string, rv.Len())
for i := range parts {
parts[i] = fmt.Sprintf("%+v", rv.Index(i).Interface())
}
return strings.Join(parts, separator), true
}

return fmt.Sprintf("%+v", v), true
}
18 changes: 17 additions & 1 deletion value_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func TestMapValueSource(t *testing.T) {
},
},
},
val: "[10]",
val: "10",
found: true,
},
}
Expand Down Expand Up @@ -341,3 +341,19 @@ func TestMapValueSourceStringer(t *testing.T) {
assert.Equal(t, `&mapValueSource{key:"bar", src:&mapSource{name:"test"}}`, mvs.GoString())
assert.Equal(t, `key "bar" from map source "test"`, mvs.String())
}

func TestMapValueSourceJoinsSliceValues(t *testing.T) {
ms := NewMapSource("config", map[any]any{"ports": []any{80, 443}})
val, ok := NewMapValueSource("ports", ms).Lookup()
if !ok || val != "80,443" {
t.Fatalf("expected %q, got %q (found=%v)", "80,443", val, ok)
}
}

func TestMapValueSourceJoinsSliceValuesWithComma(t *testing.T) {
ms := NewMapSource("config", map[any]any{"ports": []any{"alpha,beta", "gamma"}})
val, ok := NewMapValueSource("ports", ms).Lookup()
if !ok || val != "alpha,beta,gamma" {
t.Fatalf("expected %q, got %q (found=%v)", "alpha,beta,gamma", val, ok)
}
}
Loading