diff --git a/app.go b/app.go index 1b27070..e838ff6 100644 --- a/app.go +++ b/app.go @@ -103,6 +103,8 @@ type App struct { // cli.go uses text/template to render templates. You can // render custom help text by setting this variable. CustomAppHelpTemplate string + // SliceFlagSeparator is used to customize the separator for SliceFlag, the default is "," + SliceFlagSeparator string // Boolean to enable short-option handling so user can combine several // single-character bool arguments into one // i.e. foobar -o -v -> foobar -ov @@ -239,6 +241,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 fc3f288..a776c74 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()) @@ -385,5 +387,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 b8c068c..e1a040d 100644 --- a/flag_test.go +++ b/flag_test.go @@ -3384,3 +3384,21 @@ func TestSliceShortOptionHandle(t *testing.T) { t.Fatal("Action callback was never called") } } + +// Test issue #1541 +func TestCustomizedSliceFlagSeparator(t *testing.T) { + defaultSliceFlagSeparator = ";" + defer func() { + defaultSliceFlagSeparator = "," + }() + opts := []string{"opt1", "opt2", "opt3,op", "opt4"} + ret := flagSplitMultiValues(strings.Join(opts, ";")) + 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) + } + } +} diff --git a/godoc-current.txt b/godoc-current.txt index 9b5eb86..3697205 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -314,6 +314,8 @@ type App struct { // cli.go uses text/template to render templates. You can // render custom help text by setting this variable. CustomAppHelpTemplate string + // SliceFlagSeparator is used to customize the separator for SliceFlag, the default is "," + SliceFlagSeparator string // Boolean to enable short-option handling so user can combine several // single-character bool arguments into one // i.e. foobar -o -v -> foobar -ov diff --git a/testdata/godoc-v2.x.txt b/testdata/godoc-v2.x.txt index eaf62ab..7f19d30 100644 --- a/testdata/godoc-v2.x.txt +++ b/testdata/godoc-v2.x.txt @@ -314,6 +314,8 @@ type App struct { // cli.go uses text/template to render templates. You can // render custom help text by setting this variable. CustomAppHelpTemplate string + // SliceFlagSeparator is used to customize the separator for SliceFlag, the default is "," + SliceFlagSeparator string // Boolean to enable short-option handling so user can combine several // single-character bool arguments into one // i.e. foobar -o -v -> foobar -ov