From a9d966018178bc1af7cb9d866210813532df12a6 Mon Sep 17 00:00:00 2001 From: Josue Barros Date: Tue, 17 Nov 2026 06:46:33 -0300 Subject: [PATCH 1/2] fix: join map-source slice values with commas NewMapValueSource formatted a list with %+v, so slice flags received one value like "[80 443]" instead of separate elements. Join slice and array values with commas, matching the command-line and cli-altsrc behavior; update the map source expectation. --- value_source.go | 17 ++++++++++++++--- value_source_test.go | 10 +++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/value_source.go b/value_source.go index 231851e5c2..63dccba410 100644 --- a/value_source.go +++ b/value_source.go @@ -3,6 +3,7 @@ package cli import ( "fmt" "os" + "reflect" "strings" ) @@ -250,9 +251,19 @@ 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) { + parts := make([]string, rv.Len()) + for i := range parts { + parts[i] = fmt.Sprintf("%+v", rv.Index(i).Interface()) + } + return strings.Join(parts, ","), true + } + + return fmt.Sprintf("%+v", v), true } diff --git a/value_source_test.go b/value_source_test.go index 719148bd09..9a727f6ad0 100644 --- a/value_source_test.go +++ b/value_source_test.go @@ -310,7 +310,7 @@ func TestMapValueSource(t *testing.T) { }, }, }, - val: "[10]", + val: "10", found: true, }, } @@ -341,3 +341,11 @@ 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) + } +} From 403677034a84c6ffac86a92be970fa5d36b4413b Mon Sep 17 00:00:00 2001 From: Josue Barros Date: Tue, 17 Nov 2026 08:25:14 -0300 Subject: [PATCH 2/2] Use the command slice separator for map sources Map-backed slice values now join elements with the owning command's effective SliceFlagSeparator, including when the source is wrapped in a nested ValueSourceChain. This keeps elements containing commas intact when a different separator is configured. --- command_test.go | 34 ++++++++++++++++++++++++++++++++++ flag_impl.go | 5 +++++ value_source.go | 26 +++++++++++++++++++++++--- value_source_test.go | 8 ++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/command_test.go b/command_test.go index 263ad82cde..3fc69942d3 100644 --- a/command_test.go +++ b/command_test.go @@ -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) + }) + } +} diff --git a/flag_impl.go b/flag_impl.go index b3a0d3c20e..204a1398f1 100644 --- a/flag_impl.go +++ b/flag_impl.go @@ -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. diff --git a/value_source.go b/value_source.go index 63dccba410..1ad4bae06d 100644 --- a/value_source.go +++ b/value_source.go @@ -92,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 { @@ -231,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 { @@ -258,11 +271,18 @@ func (mvs *mapValueSource) Lookup() (string, bool) { 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, ","), true + return strings.Join(parts, separator), true } return fmt.Sprintf("%+v", v), true diff --git a/value_source_test.go b/value_source_test.go index 9a727f6ad0..94ecebf693 100644 --- a/value_source_test.go +++ b/value_source_test.go @@ -349,3 +349,11 @@ func TestMapValueSourceJoinsSliceValues(t *testing.T) { 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) + } +}