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

Feature: (issue_1451) customized slice flag separator #1546

Merged
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
6 changes: 6 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ type App struct {
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
CustomAppHelpTemplate string
// SliceFlagSeparator is used to customize the separator for SliceFlag, the default is ","
SliceFlagSeparator string
// 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 @@ -241,6 +243,10 @@ func (a *App) Setup() {
if a.Metadata == nil {
a.Metadata = make(map[string]interface{})
}

if len(a.SliceFlagSeparator) != 0 {
defaultSliceFlagSeparator = a.SliceFlagSeparator
}
}

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

const defaultPlaceholder = "value"

var defaultSliceFlagSeparator = ","

var (
slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano())

Expand Down Expand Up @@ -378,5 +380,5 @@ func flagFromEnvOrFile(envVars []string, filePath string) (value string, fromWhe
}

func flagSplitMultiValues(val string) []string {
return strings.Split(val, ",")
return strings.Split(val, defaultSliceFlagSeparator)
}
18 changes: 18 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3384,3 +3384,21 @@ func TestSliceShortOptionHandle(t *testing.T) {
t.Fatal("Action callback was never called")
}
}

// Test issue #1541
func TestCustomizedSliceFlagSeparator(t *testing.T) {
defaultSliceFlagSeparator = ";"
defer func() {
defaultSliceFlagSeparator = ","
}()
opts := []string{"opt1", "opt2", "opt3,op", "opt4"}
ret := flagSplitMultiValues(strings.Join(opts, ";"))
if len(ret) != 4 {
t.Fatalf("split slice flag failed, want: 4, but get: %d", len(ret))
}
for idx, r := range ret {
if r != opts[idx] {
t.Fatalf("get %dth failed, wanted: %s, but get: %s", idx, opts[idx], r)
}
}
}
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 {
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
CustomAppHelpTemplate string
// SliceFlagSeparator is used to customize the separator for SliceFlag, the default is ","
SliceFlagSeparator string
// 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-v2.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ type App struct {
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
CustomAppHelpTemplate string
// SliceFlagSeparator is used to customize the separator for SliceFlag, the default is ","
SliceFlagSeparator string
// 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