From eee066371a5bd8a4b281a264bbf8ad093d101d6c Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Fri, 26 Jun 2015 17:54:59 -0700 Subject: [PATCH] Support -flag=var1,var2,var3 to fix #2332 --- helper/flag-slice/flag.go | 2 +- helper/flag-slice/flag_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/helper/flag-slice/flag.go b/helper/flag-slice/flag.go index da75149dc..587b674fa 100644 --- a/helper/flag-slice/flag.go +++ b/helper/flag-slice/flag.go @@ -11,6 +11,6 @@ func (s *StringFlag) String() string { } func (s *StringFlag) Set(value string) error { - *s = append(*s, value) + *s = append(*s, strings.Split(value, ",")...) return nil } diff --git a/helper/flag-slice/flag_test.go b/helper/flag-slice/flag_test.go index f72e1d960..61d8682b2 100644 --- a/helper/flag-slice/flag_test.go +++ b/helper/flag-slice/flag_test.go @@ -14,6 +14,8 @@ func TestStringFlag_implements(t *testing.T) { } } +// TestStringFlagSet tests for setting the same flag more than once on the CLI +// like: blah -flag foo -flag bar func TestStringFlagSet(t *testing.T) { sv := new(StringFlag) err := sv.Set("foo") @@ -31,3 +33,18 @@ func TestStringFlagSet(t *testing.T) { t.Fatalf("Bad: %#v", sv) } } + +// TestMultiStringFlag tests for setting the same flag using a comma-separated +// list of items like: blah -flag=foo,bar +func TestMultiStringFlag(t *testing.T) { + sv := new(StringFlag) + err := sv.Set("chocolate,vanilla") + if err != nil { + t.Fatalf("err :%s", err) + } + + expected := []string{"chocolate", "vanilla"} + if !reflect.DeepEqual([]string(*sv), expected) { + t.Fatalf("Expected: %#v, found: %#v", expected, sv) + } +}