Skip to content

Commit 63cb372

Browse files
authored
Feature: (issue_1451) customized slice flag separator (#1546)
* feat: customized slice flag separator * feat: modify go doc * feat: update unit test
1 parent 82bdf5f commit 63cb372

5 files changed

Lines changed: 31 additions & 1 deletion

File tree

app.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ type App struct {
103103
// cli.go uses text/template to render templates. You can
104104
// render custom help text by setting this variable.
105105
CustomAppHelpTemplate string
106+
// SliceFlagSeparator is used to customize the separator for SliceFlag, the default is ","
107+
SliceFlagSeparator string
106108
// Boolean to enable short-option handling so user can combine several
107109
// single-character bool arguments into one
108110
// i.e. foobar -o -v -> foobar -ov
@@ -241,6 +243,10 @@ func (a *App) Setup() {
241243
if a.Metadata == nil {
242244
a.Metadata = make(map[string]interface{})
243245
}
246+
247+
if len(a.SliceFlagSeparator) != 0 {
248+
defaultSliceFlagSeparator = a.SliceFlagSeparator
249+
}
244250
}
245251

246252
func (a *App) newRootCommand() *Command {

flag.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515

1616
const defaultPlaceholder = "value"
1717

18+
var defaultSliceFlagSeparator = ","
19+
1820
var (
1921
slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano())
2022

@@ -378,5 +380,5 @@ func flagFromEnvOrFile(envVars []string, filePath string) (value string, fromWhe
378380
}
379381

380382
func flagSplitMultiValues(val string) []string {
381-
return strings.Split(val, ",")
383+
return strings.Split(val, defaultSliceFlagSeparator)
382384
}

flag_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3384,3 +3384,21 @@ func TestSliceShortOptionHandle(t *testing.T) {
33843384
t.Fatal("Action callback was never called")
33853385
}
33863386
}
3387+
3388+
// Test issue #1541
3389+
func TestCustomizedSliceFlagSeparator(t *testing.T) {
3390+
defaultSliceFlagSeparator = ";"
3391+
defer func() {
3392+
defaultSliceFlagSeparator = ","
3393+
}()
3394+
opts := []string{"opt1", "opt2", "opt3,op", "opt4"}
3395+
ret := flagSplitMultiValues(strings.Join(opts, ";"))
3396+
if len(ret) != 4 {
3397+
t.Fatalf("split slice flag failed, want: 4, but get: %d", len(ret))
3398+
}
3399+
for idx, r := range ret {
3400+
if r != opts[idx] {
3401+
t.Fatalf("get %dth failed, wanted: %s, but get: %s", idx, opts[idx], r)
3402+
}
3403+
}
3404+
}

godoc-current.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ type App struct {
316316
// cli.go uses text/template to render templates. You can
317317
// render custom help text by setting this variable.
318318
CustomAppHelpTemplate string
319+
// SliceFlagSeparator is used to customize the separator for SliceFlag, the default is ","
320+
SliceFlagSeparator string
319321
// Boolean to enable short-option handling so user can combine several
320322
// single-character bool arguments into one
321323
// i.e. foobar -o -v -> foobar -ov

testdata/godoc-v2.x.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ type App struct {
316316
// cli.go uses text/template to render templates. You can
317317
// render custom help text by setting this variable.
318318
CustomAppHelpTemplate string
319+
// SliceFlagSeparator is used to customize the separator for SliceFlag, the default is ","
320+
SliceFlagSeparator string
319321
// Boolean to enable short-option handling so user can combine several
320322
// single-character bool arguments into one
321323
// i.e. foobar -o -v -> foobar -ov

0 commit comments

Comments
 (0)