Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling slice flag separator #1582

Merged
merged 3 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -246,6 +248,8 @@ func (a *App) Setup() {
if len(a.SliceFlagSeparator) != 0 {
defaultSliceFlagSeparator = a.SliceFlagSeparator
}

disableSliceFlagSeparator = a.DisableSliceFlagSeparator
}

func (a *App) newRootCommand() *Command {
Expand Down
9 changes: 8 additions & 1 deletion flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (

const defaultPlaceholder = "value"

var defaultSliceFlagSeparator = ","
var (
defaultSliceFlagSeparator = ","
disableSliceFlagSeparator = false
)

var (
slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano())
Expand Down Expand Up @@ -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)
}
17 changes: 17 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
2 changes: 2 additions & 0 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions testdata/godoc-v3.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down