Skip to content

Commit

Permalink
Add hashmap type show.
Browse files Browse the repository at this point in the history
If flag is StringMapFlag ,it will print
`--property string=string [--property string=string]…`

Signed-off-by: jokemanfire <[email protected]>
  • Loading branch information
jokemanfire committed Jan 13, 2025
1 parent 3043a47 commit c6b26b8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
6 changes: 3 additions & 3 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ type LocalFlag interface {

// FlagType is an interface to detect if a flag is a string, bool, etc.
type FlagType interface {
GetFlagType() string
TypeName() string
}

func newFlagSet(name string, flags []Flag) (*flag.FlagSet, error) {
Expand Down Expand Up @@ -312,8 +312,8 @@ func stringifyFlag(f Flag) string {
// if needsPlaceholder is true, placeholder is empty
if needsPlaceholder && placeholder == "" {
// try to get type from flag
if v1, ok := f.(FlagType); ok && v1.GetFlagType() != "" {
placeholder = v1.GetFlagType()
if ft, ok := f.(FlagType); ok && ft.TypeName() != "" {
placeholder = ft.TypeName()
} else {
placeholder = defaultPlaceholder
}
Expand Down
15 changes: 11 additions & 4 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,25 @@ func (f *FlagBase[T, C, V]) GetValue() string {
return fmt.Sprintf("%v", f.Value)
}

// GetFlagType returns the type of the flag.
func (f *FlagBase[T, C, V]) GetFlagType() string {
// TypeName returns the type of the flag.
func (f *FlagBase[T, C, V]) TypeName() string {
ty := reflect.TypeOf(f.Value)
if ty == nil {
return ""
}
switch ty.Kind() {
// if it is a Slice, then return the slice's inner type. Will nested slices be used in the future?
if ty.Kind() == reflect.Slice {
case reflect.Slice:
elemType := ty.Elem()
return elemType.Name()
// if it is a Map, then return the map's key and value types.
case reflect.Map:
keyType := ty.Key()
valueType := ty.Elem()
return fmt.Sprintf("%s=%s", keyType.Name(), valueType.Name())
default:
return ty.Name()
}
return ty.Name()
}

// Apply populates the flag given the flag set and environment
Expand Down
14 changes: 7 additions & 7 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2706,7 +2706,7 @@ func TestFlagDefaultValue(t *testing.T) {
name: "stringMap",
flag: &StringMapFlag{Name: "flag", Value: map[string]string{"default1": "default2"}},
toParse: []string{"--flag", "parsed="},
expect: `--flag value [ --flag value ] (default: default1="default2")`,
expect: `--flag string=string [ --flag string=string ] (default: default1="default2")`,
},
}
for _, v := range cases {
Expand Down Expand Up @@ -2845,7 +2845,7 @@ func TestFlagDefaultValueWithEnv(t *testing.T) {
name: "stringMap",
flag: &StringMapFlag{Name: "flag", Value: map[string]string{"default1": "default2"}, Sources: EnvVars("ssflag")},
toParse: []string{"--flag", "parsed="},
expect: `--flag value [ --flag value ] (default: default1="default2")` + withEnvHint([]string{"ssflag"}, ""),
expect: `--flag string=string [ --flag string=string ] (default: default1="default2")` + withEnvHint([]string{"ssflag"}, ""),
environ: map[string]string{
"ssflag": "some-other-env_value=",
},
Expand Down Expand Up @@ -3007,11 +3007,11 @@ var stringMapFlagTests = []struct {
value map[string]string
expected string
}{
{"foo", nil, nil, "--foo value [ --foo value ]\t"},
{"f", nil, nil, "-f value [ -f value ]\t"},
{"f", nil, map[string]string{"Lipstick": ""}, "-f value [ -f value ]\t(default: Lipstick=)"},
{"test", nil, map[string]string{"Something": ""}, "--test value [ --test value ]\t(default: Something=)"},
{"dee", []string{"d"}, map[string]string{"Inka": "Dinka", "dooo": ""}, "--dee value, -d value [ --dee value, -d value ]\t(default: Inka=\"Dinka\", dooo=)"},
{"foo", nil, nil, "--foo string=string [ --foo string=string ]\t"},
{"f", nil, nil, "-f string=string [ -f string=string ]\t"},
{"f", nil, map[string]string{"Lipstick": ""}, "-f string=string [ -f string=string ]\t(default: Lipstick=)"},
{"test", nil, map[string]string{"Something": ""}, "--test string=string [ --test string=string ]\t(default: Something=)"},
{"dee", []string{"d"}, map[string]string{"Inka": "Dinka", "dooo": ""}, "--dee string=string, -d string=string [ --dee string=string, -d string=string ]\t(default: Inka=\"Dinka\", dooo=)"},
}

func TestStringMapFlagHelpOutput(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,6 @@ func (f *FlagBase[T, C, V]) GetDefaultText() string
func (f *FlagBase[T, C, V]) GetEnvVars() []string
GetEnvVars returns the env vars for this flag

func (f *FlagBase[T, C, V]) GetFlagType() string
GetFlagType returns the type of the flag.

func (f *FlagBase[T, C, V]) GetUsage() string
GetUsage returns the usage string for the flag

Expand Down Expand Up @@ -748,6 +745,9 @@ func (f *FlagBase[T, C, V]) String() string
func (f *FlagBase[T, C, V]) TakesValue() bool
TakesValue returns true if the flag takes a value, otherwise false

func (f *FlagBase[T, C, V]) TypeName() string
TypeName returns the type of the flag.

type FlagCategories interface {
// AddFlags adds a flag to a category, creating a new category if necessary.
AddFlag(category string, fl Flag)
Expand Down Expand Up @@ -789,7 +789,7 @@ var FlagStringer FlagStringFunc = stringifyFlag
display a flag.

type FlagType interface {
GetFlagType() string
TypeName() string
}
FlagType is an interface to detect if a flag is a string, bool, etc.

Expand Down
6 changes: 3 additions & 3 deletions testdata/godoc-v3.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,8 @@ func (f *FlagBase[T, C, V]) GetDefaultText() string
func (f *FlagBase[T, C, V]) GetEnvVars() []string
GetEnvVars returns the env vars for this flag

func (f *FlagBase[T, C, V]) GetFlagType() string
GetFlagType returns the type of the flag.
func (f *FlagBase[T, C, V]) TypeName() string
TypeName returns the type of the flag.

func (f *FlagBase[T, C, V]) GetUsage() string
GetUsage returns the usage string for the flag
Expand Down Expand Up @@ -789,7 +789,7 @@ var FlagStringer FlagStringFunc = stringifyFlag
display a flag.

type FlagType interface {
GetFlagType() string
TypeName() string
}
FlagType is an interface to detect if a flag is a string, bool, etc.

Expand Down

0 comments on commit c6b26b8

Please sign in to comment.