Support -flag=var1,var2,var3 to fix #2332

This commit is contained in:
Chris Bednarski 2015-06-26 17:54:59 -07:00
parent e112d9b288
commit eee066371a
2 changed files with 18 additions and 1 deletions

View File

@ -11,6 +11,6 @@ func (s *StringFlag) String() string {
} }
func (s *StringFlag) Set(value string) error { func (s *StringFlag) Set(value string) error {
*s = append(*s, value) *s = append(*s, strings.Split(value, ",")...)
return nil return nil
} }

View File

@ -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) { func TestStringFlagSet(t *testing.T) {
sv := new(StringFlag) sv := new(StringFlag)
err := sv.Set("foo") err := sv.Set("foo")
@ -31,3 +33,18 @@ func TestStringFlagSet(t *testing.T) {
t.Fatalf("Bad: %#v", sv) 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)
}
}