From 196b222a8b53ad8abf6463a1edf59f03b72a5f50 Mon Sep 17 00:00:00 2001 From: Sergey Goroshko Date: Fri, 1 May 2020 23:06:09 +0300 Subject: [PATCH] fix #1121(StringSliceFlag set default value into destination) --- flag_string_slice.go | 8 ++++++++ flag_test.go | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/flag_string_slice.go b/flag_string_slice.go index ac363bf609..1a221d01d5 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -116,6 +116,14 @@ func (f *StringSliceFlag) GetValue() string { // Apply populates the flag given the flag set and environment func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { + + if f.Destination != nil { + if f.Value != nil { + f.Destination.slice = make([]string, len(f.Value.slice)) + copy(f.Destination.slice, f.Value.slice) + } + } + if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok { f.Value = &StringSlice{} destination := f.Value diff --git a/flag_test.go b/flag_test.go index 431eb03a13..7dbfbdc5bd 100644 --- a/flag_test.go +++ b/flag_test.go @@ -386,6 +386,18 @@ func TestStringSliceFlagApply_SetsAllNames(t *testing.T) { expect(t, err, nil) } +func TestStringSliceFlagApply_DefaultValueWithDestination(t *testing.T) { + defValue := []string{"UA", "US"} + + fl := StringSliceFlag{Name: "country", Value: NewStringSlice(defValue...), Destination: NewStringSlice("CA")} + set := flag.NewFlagSet("test", 0) + _ = fl.Apply(set) + + err := set.Parse([]string{}) + expect(t, err, nil) + expect(t, defValue, fl.Destination.Value()) +} + var intFlagTests = []struct { name string expected string