Skip to content

Commit

Permalink
Allow disabling SliceFlag separator altogether
Browse files Browse the repository at this point in the history
  • Loading branch information
feedmeapples committed Nov 16, 2022
1 parent 600ef6e commit 0f8707a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -264,6 +266,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 @@ -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)
}
16 changes: 16 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

0 comments on commit 0f8707a

Please sign in to comment.