Repository: spf13/viper (go)

Issue:
error on handle corba StringArray flag
I got an error while handle cobra StringArray flag. I think this may be a BUG

``` go
flags.StringArrayP("URLS", "U", []string{"http://github.com", "http://github.com"}, "test URLS")
viper.BindPFlag("URLS", flags.Lookup("URLS"))
Result:=viper.GetStringSlice("URLS")
```

Result is only one element=> 

`Result=[[http://github.com,http://github.com]]`
It should be `[http://github.com http://github.com]`


Pull request:
Add a proper processing for pflag.StringArray
This PR fixes #246 and adds tests to changes from #398.

Please, consider it to merge!

Fixes #246
Fixes #398

Patch:
diff --git a/viper.go b/viper.go
index 60f9868..e8c0462 100644
--- a/viper.go
+++ b/viper.go
@@ -1153,7 +1153,7 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) interface{} {
 			return cast.ToInt(flag.ValueString())
 		case "bool":
 			return cast.ToBool(flag.ValueString())
-		case "stringSlice":
+		case "stringSlice", "stringArray":
 			s := strings.TrimPrefix(flag.ValueString(), "[")
 			s = strings.TrimSuffix(s, "]")
 			res, _ := readAsCSV(s)
@@ -1232,7 +1232,7 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) interface{} {
 				return cast.ToInt(flag.ValueString())
 			case "bool":
 				return cast.ToBool(flag.ValueString())
-			case "stringSlice":
+			case "stringSlice", "stringArray":
 				s := strings.TrimPrefix(flag.ValueString(), "[")
 				s = strings.TrimSuffix(s, "]")
 				res, _ := readAsCSV(s)


Test patch:
diff --git a/viper_test.go b/viper_test.go
index f748c7a..4192748 100644
--- a/viper_test.go
+++ b/viper_test.go
@@ -917,6 +917,54 @@ func TestBindPFlagsStringSlice(t *testing.T) {
 	}
 }
 
+// nolint: dupl
+func TestBindPFlagsStringArray(t *testing.T) {
+	tests := []struct {
+		Expected []string
+		Value    string
+	}{
+		{[]string{}, ""},
+		{[]string{"jeden"}, "jeden"},
+		{[]string{"dwa,trzy"}, "dwa,trzy"},
+		{[]string{"cztery,\"piec , szesc\""}, "cztery,\"piec , szesc\""},
+	}
+
+	v := New() // create independent Viper object
+	defaultVal := []string{"default"}
+	v.SetDefault("stringarray", defaultVal)
+
+	for _, testValue := range tests {
+		flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
+		flagSet.StringArray("stringarray", testValue.Expected, "test")
+
+		for _, changed := range []bool{true, false} {
+			flagSet.VisitAll(func(f *pflag.Flag) {
+				f.Value.Set(testValue.Value)
+				f.Changed = changed
+			})
+
+			err := v.BindPFlags(flagSet)
+			if err != nil {
+				t.Fatalf("error binding flag set, %v", err)
+			}
+
+			type TestStr struct {
+				StringArray []string
+			}
+			val := &TestStr{}
+			if err := v.Unmarshal(val); err != nil {
+				t.Fatalf("%+#v cannot unmarshal: %s", testValue.Value, err)
+			}
+			if changed {
+				assert.Equal(t, testValue.Expected, val.StringArray)
+				assert.Equal(t, testValue.Expected, v.Get("stringarray"))
+			} else {
+				assert.Equal(t, defaultVal, val.StringArray)
+			}
+		}
+	}
+}
+
 // nolint: dupl
 func TestBindPFlagsIntSlice(t *testing.T) {
 	tests := []struct {