diff --git a/app.go b/app.go index e7f79c5130..10198f4332 100644 --- a/app.go +++ b/app.go @@ -107,6 +107,8 @@ type App struct { CustomAppHelpTemplate string // SliceFlagSeparator is used to customize the separator for SliceFlag, the default is "," SliceFlagSeparator string + // DisableSliceFlagSeparator is used to disable SliceFlagSeparator, the default is false + DisableSliceFlagSeparator bool // Boolean to enable short-option handling so user can combine several // single-character bool arguments into one // i.e. foobar -o -v -> foobar -ov @@ -264,6 +266,8 @@ func (a *App) Setup() { if len(a.SliceFlagSeparator) != 0 { defaultSliceFlagSeparator = a.SliceFlagSeparator } + + disableSliceFlagSeparator = a.DisableSliceFlagSeparator } func (a *App) newRootCommand() *Command { diff --git a/flag.go b/flag.go index b66a75da5e..5c0a8b7328 100644 --- a/flag.go +++ b/flag.go @@ -15,7 +15,10 @@ import ( const defaultPlaceholder = "value" -var defaultSliceFlagSeparator = "," +var ( + defaultSliceFlagSeparator = "," + disableSliceFlagSeparator = false +) var ( slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano()) @@ -380,5 +383,9 @@ func flagFromEnvOrFile(envVars []string, filePath string) (value string, fromWhe } func flagSplitMultiValues(val string) []string { + if disableSliceFlagSeparator { + return []string{val} + } + return strings.Split(val, defaultSliceFlagSeparator) } diff --git a/flag_test.go b/flag_test.go index 3a2bc2b67e..f8205085a3 100644 --- a/flag_test.go +++ b/flag_test.go @@ -3402,3 +3402,20 @@ func TestCustomizedSliceFlagSeparator(t *testing.T) { } } } + +func TestFlagSplitMultiValues_Disabled(t *testing.T) { + disableSliceFlagSeparator = true + defer func() { + disableSliceFlagSeparator = false + }() + + opts := []string{"opt1", "opt2", "opt3,op", "opt4"} + ret := flagSplitMultiValues(strings.Join(opts, defaultSliceFlagSeparator)) + if len(ret) != 1 { + t.Fatalf("failed to disable split slice flag, want: 1, but got: %d", len(ret)) + } + + if ret[0] != strings.Join(opts, defaultSliceFlagSeparator) { + t.Fatalf("failed to disable split slice flag, want: %s, but got: %s", strings.Join(opts, defaultSliceFlagSeparator), ret[0]) + } +} diff --git a/godoc-current.txt b/godoc-current.txt index b8dbf6ad0a..6afd244f25 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -318,6 +318,8 @@ type App struct { CustomAppHelpTemplate string // SliceFlagSeparator is used to customize the separator for SliceFlag, the default is "," SliceFlagSeparator string + // DisableSliceFlagSeparator is used to disable SliceFlagSeparator, the default is false + DisableSliceFlagSeparator bool // 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 b8dbf6ad0a..6afd244f25 100644 --- a/testdata/godoc-v2.x.txt +++ b/testdata/godoc-v2.x.txt @@ -318,6 +318,8 @@ type App struct { CustomAppHelpTemplate string // SliceFlagSeparator is used to customize the separator for SliceFlag, the default is "," SliceFlagSeparator string + // DisableSliceFlagSeparator is used to disable SliceFlagSeparator, the default is false + DisableSliceFlagSeparator bool // Boolean to enable short-option handling so user can combine several // single-character bool arguments into one // i.e. foobar -o -v -> foobar -ov