diff --git a/cmd/urfave-cli-genflags/urfave-cli-genflags b/cmd/urfave-cli-genflags/urfave-cli-genflags new file mode 100755 index 0000000000..b5b1a9dec6 Binary files /dev/null and b/cmd/urfave-cli-genflags/urfave-cli-genflags differ diff --git a/flag.go b/flag.go index e4357f3f3c..de034d104f 100644 --- a/flag.go +++ b/flag.go @@ -141,12 +141,12 @@ type DocGenerationFlag interface { GetEnvVars() []string } -// DocGenerationSliceFlag extends DocGenerationFlag for slice-based flags. -type DocGenerationSliceFlag interface { +// DocGenerationSliceFlag extends DocGenerationFlag for slice/map based flags. +type DocGenerationMultiValueFlag interface { DocGenerationFlag - // IsSliceFlag returns true for flags that can be given multiple times. - IsSliceFlag() bool + // IsMultiValueFlag returns true for flags that can be given multiple times. + IsMultiValueFlag() bool } // Countable is an interface to enable detection of flag values which support @@ -357,8 +357,8 @@ func stringifyFlag(f Flag) string { usageWithDefault := strings.TrimSpace(usage + defaultValueString) pn := prefixedNames(df.Names(), placeholder) - sliceFlag, ok := f.(DocGenerationSliceFlag) - if ok && sliceFlag.IsSliceFlag() { + sliceFlag, ok := f.(DocGenerationMultiValueFlag) + if ok && sliceFlag.IsMultiValueFlag() { pn = pn + " [ " + pn + " ]" } diff --git a/flag_impl.go b/flag_impl.go index 18566fa86c..dc5a1c4c53 100644 --- a/flag_impl.go +++ b/flag_impl.go @@ -189,10 +189,12 @@ func (f *FlagBase[T, C, V]) RunAction(ctx *Context) error { return nil } -// IsSliceFlag returns true if the value type T is of kind slice -func (f *FlagBase[T, C, VC]) IsSliceFlag() bool { +// IsMultiValueFlag returns true if the value type T can take multiple +// values from cmd line. This is true for slice and map type flags +func (f *FlagBase[T, C, VC]) IsMultiValueFlag() bool { // TBD how to specify - return reflect.TypeOf(f.Value).Kind() == reflect.Slice + return reflect.TypeOf(f.Value).Kind() == reflect.Slice || + reflect.TypeOf(f.Value).Kind() == reflect.Map } // IsPersistent returns true if flag needs to be persistent across subcommands diff --git a/godoc-current.txt b/godoc-current.txt index d047329864..ebd177cf7a 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -693,13 +693,13 @@ type DocGenerationFlag interface { DocGenerationFlag is an interface that allows documentation generation for the flag -type DocGenerationSliceFlag interface { +type DocGenerationMultiValueFlag interface { DocGenerationFlag - // IsSliceFlag returns true for flags that can be given multiple times. - IsSliceFlag() bool + // IsMultiValueFlag returns true for flags that can be given multiple times. + IsMultiValueFlag() bool } - DocGenerationSliceFlag extends DocGenerationFlag for slice-based flags. + DocGenerationSliceFlag extends DocGenerationFlag for slice/map based flags. type DurationFlag = FlagBase[time.Duration, NoConfig, durationValue] @@ -827,6 +827,10 @@ func (f *FlagBase[T, C, V]) GetValue() string GetValue returns the flags value as string representation and an empty string if the flag takes no value at all. +func (f *FlagBase[T, C, VC]) IsMultiValueFlag() bool + IsMultiValueFlag returns true if the value type T can take multiple values + from cmd line. This is true for slice and map type flags + func (f *FlagBase[T, C, VC]) IsPersistent() bool IsPersistent returns true if flag needs to be persistent across subcommands @@ -836,9 +840,6 @@ func (f *FlagBase[T, C, V]) IsRequired() bool func (f *FlagBase[T, C, V]) IsSet() bool IsSet returns whether or not the flag has been set through env or file -func (f *FlagBase[T, C, VC]) IsSliceFlag() bool - IsSliceFlag returns true if the value type T is of kind slice - func (f *FlagBase[T, C, V]) IsVisible() bool IsVisible returns true if the flag is not hidden, otherwise false diff --git a/testdata/godoc-v3.x.txt b/testdata/godoc-v3.x.txt index d047329864..ebd177cf7a 100644 --- a/testdata/godoc-v3.x.txt +++ b/testdata/godoc-v3.x.txt @@ -693,13 +693,13 @@ type DocGenerationFlag interface { DocGenerationFlag is an interface that allows documentation generation for the flag -type DocGenerationSliceFlag interface { +type DocGenerationMultiValueFlag interface { DocGenerationFlag - // IsSliceFlag returns true for flags that can be given multiple times. - IsSliceFlag() bool + // IsMultiValueFlag returns true for flags that can be given multiple times. + IsMultiValueFlag() bool } - DocGenerationSliceFlag extends DocGenerationFlag for slice-based flags. + DocGenerationSliceFlag extends DocGenerationFlag for slice/map based flags. type DurationFlag = FlagBase[time.Duration, NoConfig, durationValue] @@ -827,6 +827,10 @@ func (f *FlagBase[T, C, V]) GetValue() string GetValue returns the flags value as string representation and an empty string if the flag takes no value at all. +func (f *FlagBase[T, C, VC]) IsMultiValueFlag() bool + IsMultiValueFlag returns true if the value type T can take multiple values + from cmd line. This is true for slice and map type flags + func (f *FlagBase[T, C, VC]) IsPersistent() bool IsPersistent returns true if flag needs to be persistent across subcommands @@ -836,9 +840,6 @@ func (f *FlagBase[T, C, V]) IsRequired() bool func (f *FlagBase[T, C, V]) IsSet() bool IsSet returns whether or not the flag has been set through env or file -func (f *FlagBase[T, C, VC]) IsSliceFlag() bool - IsSliceFlag returns true if the value type T is of kind slice - func (f *FlagBase[T, C, V]) IsVisible() bool IsVisible returns true if the flag is not hidden, otherwise false