From 508efc376d69cb8d5dfec2e32f9b61303a4e5179 Mon Sep 17 00:00:00 2001 From: feedmeapples Date: Thu, 10 Nov 2022 21:47:37 -0500 Subject: [PATCH 1/3] Allow disabling SliceFlag separator altogether --- app.go | 4 ++++ flag.go | 9 ++++++++- flag_test.go | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 221b6ad014..6f395b5dea 100644 --- a/app.go +++ b/app.go @@ -104,6 +104,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 @@ -246,6 +248,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 a776c7497a..7970e8d39c 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()) @@ -387,5 +390,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 e1a040d965..a44aced73c 100644 --- a/flag_test.go +++ b/flag_test.go @@ -3402,3 +3402,19 @@ 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, ",")) + 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, ",") { + t.Fatalf("failed to disable split slice flag, want: %s, but got: %s", strings.Join(opts, ","), ret[0]) + } +} From d48e8971ce2070224ee17096ec09f6daaee8e557 Mon Sep 17 00:00:00 2001 From: feedmeapples Date: Thu, 10 Nov 2022 21:51:59 -0500 Subject: [PATCH 2/3] replace test hardcode with defaultSliceFlagSeparator --- flag_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/flag_test.go b/flag_test.go index a44aced73c..1679b2c3c3 100644 --- a/flag_test.go +++ b/flag_test.go @@ -3408,13 +3408,14 @@ func TestFlagSplitMultiValues_Disabled(t *testing.T) { defer func() { disableSliceFlagSeparator = false }() + opts := []string{"opt1", "opt2", "opt3,op", "opt4"} - ret := flagSplitMultiValues(strings.Join(opts, ",")) + 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, ",") { - t.Fatalf("failed to disable split slice flag, want: %s, but got: %s", strings.Join(opts, ","), ret[0]) + 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]) } } From e3f2cc66b5cf49925b3d051717d6d524d7135858 Mon Sep 17 00:00:00 2001 From: feedmeapples Date: Thu, 10 Nov 2022 22:11:22 -0500 Subject: [PATCH 3/3] godoc --- godoc-current.txt | 2 ++ testdata/godoc-v3.x.txt | 2 ++ 2 files changed, 4 insertions(+) diff --git a/godoc-current.txt b/godoc-current.txt index 65ec07ac68..9e0ac58f98 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -316,6 +316,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-v3.x.txt b/testdata/godoc-v3.x.txt index 65ec07ac68..f31b3f6a4a 100644 --- a/testdata/godoc-v3.x.txt +++ b/testdata/godoc-v3.x.txt @@ -316,6 +316,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