diff --git a/app.go b/app.go index 24704e8f7b..65b12f53a8 100644 --- a/app.go +++ b/app.go @@ -107,6 +107,8 @@ type App struct { // single-character bool arguments into one // i.e. foobar -o -v -> foobar -ov UseShortOptionHandling bool + // SliceFlagSeparator is used to customize the separator for SliceFlag, the default is "," + SliceFlagSeparator string // Enable suggestions for commands and flags Suggest bool @@ -241,6 +243,10 @@ func (a *App) Setup() { if a.Metadata == nil { a.Metadata = make(map[string]interface{}) } + + if len(a.SliceFlagSeparator) != 0 { + defaultSliceFlagSeparator = a.SliceFlagSeparator + } } func (a *App) newRootCommand() *Command { diff --git a/flag.go b/flag.go index 7535424c04..b66a75da5e 100644 --- a/flag.go +++ b/flag.go @@ -15,6 +15,8 @@ import ( const defaultPlaceholder = "value" +var defaultSliceFlagSeparator = "," + var ( slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano()) @@ -378,5 +380,5 @@ func flagFromEnvOrFile(envVars []string, filePath string) (value string, fromWhe } func flagSplitMultiValues(val string) []string { - return strings.Split(val, ",") + return strings.Split(val, defaultSliceFlagSeparator) } diff --git a/flag_test.go b/flag_test.go index d46c1a70f7..4a0d6e9396 100644 --- a/flag_test.go +++ b/flag_test.go @@ -3384,3 +3384,19 @@ func TestSliceShortOptionHandle(t *testing.T) { t.Fatal("Action callback was never called") } } + +func TestCustomizedSliceFlagSeparator(t *testing.T) { + defaultSliceFlagSeparator = ";" + opts := []string{"opt1", "opt2", "opt3,op", "opt4"} + ret := flagSplitMultiValues(strings.Join(opts, ";")) + defaultSliceFlagSeparator = "," + if len(ret) != 4 { + t.Fatalf("split slice flag failed, want: 4, but get: %d", len(ret)) + } + for idx, r := range ret { + if r != opts[idx] { + t.Fatalf("get %dth failed, wanted: %s, but get: %s", idx, opts[idx], r) + } + } +} +