Feature: (issue_1451) customized slice flag separator (#1546)
* feat: customized slice flag separator * feat: modify go doc * feat: update unit test
This commit is contained in:
parent
82bdf5f42e
commit
63cb372cce
6
app.go
6
app.go
@ -103,6 +103,8 @@ type App struct {
|
|||||||
// cli.go uses text/template to render templates. You can
|
// cli.go uses text/template to render templates. You can
|
||||||
// render custom help text by setting this variable.
|
// render custom help text by setting this variable.
|
||||||
CustomAppHelpTemplate string
|
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
|
// Boolean to enable short-option handling so user can combine several
|
||||||
// single-character bool arguments into one
|
// single-character bool arguments into one
|
||||||
// i.e. foobar -o -v -> foobar -ov
|
// i.e. foobar -o -v -> foobar -ov
|
||||||
@ -241,6 +243,10 @@ func (a *App) Setup() {
|
|||||||
if a.Metadata == nil {
|
if a.Metadata == nil {
|
||||||
a.Metadata = make(map[string]interface{})
|
a.Metadata = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(a.SliceFlagSeparator) != 0 {
|
||||||
|
defaultSliceFlagSeparator = a.SliceFlagSeparator
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) newRootCommand() *Command {
|
func (a *App) newRootCommand() *Command {
|
||||||
|
4
flag.go
4
flag.go
@ -15,6 +15,8 @@ import (
|
|||||||
|
|
||||||
const defaultPlaceholder = "value"
|
const defaultPlaceholder = "value"
|
||||||
|
|
||||||
|
var defaultSliceFlagSeparator = ","
|
||||||
|
|
||||||
var (
|
var (
|
||||||
slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano())
|
slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano())
|
||||||
|
|
||||||
@ -378,5 +380,5 @@ func flagFromEnvOrFile(envVars []string, filePath string) (value string, fromWhe
|
|||||||
}
|
}
|
||||||
|
|
||||||
func flagSplitMultiValues(val string) []string {
|
func flagSplitMultiValues(val string) []string {
|
||||||
return strings.Split(val, ",")
|
return strings.Split(val, defaultSliceFlagSeparator)
|
||||||
}
|
}
|
||||||
|
18
flag_test.go
18
flag_test.go
@ -3384,3 +3384,21 @@ func TestSliceShortOptionHandle(t *testing.T) {
|
|||||||
t.Fatal("Action callback was never called")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -316,6 +316,8 @@ type App struct {
|
|||||||
// cli.go uses text/template to render templates. You can
|
// cli.go uses text/template to render templates. You can
|
||||||
// render custom help text by setting this variable.
|
// render custom help text by setting this variable.
|
||||||
CustomAppHelpTemplate string
|
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
|
// Boolean to enable short-option handling so user can combine several
|
||||||
// single-character bool arguments into one
|
// single-character bool arguments into one
|
||||||
// i.e. foobar -o -v -> foobar -ov
|
// i.e. foobar -o -v -> foobar -ov
|
||||||
|
2
testdata/godoc-v2.x.txt
vendored
2
testdata/godoc-v2.x.txt
vendored
@ -316,6 +316,8 @@ type App struct {
|
|||||||
// cli.go uses text/template to render templates. You can
|
// cli.go uses text/template to render templates. You can
|
||||||
// render custom help text by setting this variable.
|
// render custom help text by setting this variable.
|
||||||
CustomAppHelpTemplate string
|
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
|
// Boolean to enable short-option handling so user can combine several
|
||||||
// single-character bool arguments into one
|
// single-character bool arguments into one
|
||||||
// i.e. foobar -o -v -> foobar -ov
|
// i.e. foobar -o -v -> foobar -ov
|
||||||
|
Loading…
Reference in New Issue
Block a user