Skip to content

Commit 0add8d1

Browse files
Don't show empty slices as flag default values in help
1 parent 6ad10c6 commit 0add8d1

3 files changed

Lines changed: 47 additions & 9 deletions

File tree

command_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,8 @@ func TestHelp(t *testing.T) {
600600
cli.SubCommands(sub1, sub2),
601601
cli.Flag(new(bool), "delete", 'd', false, "Delete something"),
602602
cli.Flag(new(int), "count", cli.NoShortHand, -1, "Count something"),
603+
cli.Flag(new([]string), "things", cli.NoShortHand, nil, "Names of things"),
604+
cli.Flag(new([]string), "more", cli.NoShortHand, []string{"one", "two"}, "Names of things with a default"),
603605
},
604606
wantErr: false,
605607
},

internal/flag/flag.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func New[T Flaggable](p *T, name string, short rune, value T, usage string) (Fla
107107

108108
// If the default value is not the zero value for the type, it is treated as
109109
// significant and shown to the user
110-
if !isZero(value) {
110+
if !isZeroIsh(value) {
111111
// \t so that defaults get aligned by tabwriter when the command
112112
// dumps the flags
113113
usage += fmt.Sprintf("\t[default: %v]", value)
@@ -905,8 +905,16 @@ func formatStringSlice(slice []string) string {
905905
return s.String()
906906
}
907907

908-
// isZero reports whether value is the zero value for it's type.
909-
func isZero[T Flaggable](value T) bool {
908+
// isZeroIsh reports whether value is the zero value (ish) for it's type.
909+
//
910+
// "ish" means that empty slices will return true from isZeroIsh despite their official
911+
// zero value being nil. The primary use of isZeroIsh is to determine whether or not
912+
// a default value is worth displaying to the user in the help text, and an empty slice
913+
// is probably not.
914+
func isZeroIsh[T Flaggable](value T) bool { //nolint:cyclop // Not much else we can do here
915+
// Note: all the slice values ([]T) are in their own separate branches because if you
916+
// combine them, the resulting value in the body of the case block is 'any' and
917+
// you cannot do len(any)
910918
switch typ := any(value).(type) {
911919
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64:
912920
return typ == 0
@@ -916,8 +924,34 @@ func isZero[T Flaggable](value T) bool {
916924
return typ == ""
917925
case bool:
918926
return !typ
919-
case []byte, net.IP, []int, []int8, []int16, []int32, []int64, []uint, []uint16, []uint32, []uint64, []float32, []float64, []string:
920-
return typ == nil
927+
case []byte:
928+
return len(typ) == 0
929+
case net.IP:
930+
return len(typ) == 0
931+
case []int:
932+
return len(typ) == 0
933+
case []int8:
934+
return len(typ) == 0
935+
case []int16:
936+
return len(typ) == 0
937+
case []int32:
938+
return len(typ) == 0
939+
case []int64:
940+
return len(typ) == 0
941+
case []uint:
942+
return len(typ) == 0
943+
case []uint16:
944+
return len(typ) == 0
945+
case []uint32:
946+
return len(typ) == 0
947+
case []uint64:
948+
return len(typ) == 0
949+
case []float32:
950+
return len(typ) == 0
951+
case []float64:
952+
return len(typ) == 0
953+
case []string:
954+
return len(typ) == 0
921955
case time.Time:
922956
var zero time.Time
923957
return typ.Equal(zero)

testdata/snapshots/TestHelp/with_subcommands_and_flags.snap.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ Commands:
1111

1212
Options:
1313

14-
N/A --count int Count something [default: -1]
15-
-d --delete bool Delete something
16-
-h --help bool Show help for test
17-
-V --version bool Show version info for test
14+
N/A --count int Count something [default: -1]
15+
-d --delete bool Delete something
16+
-h --help bool Show help for test
17+
N/A --more []string Names of things with a default [default: [one two]]
18+
N/A --things []string Names of things
19+
-V --version bool Show version info for test
1820

1921
Use "test [command] -h/--help" for more information about a command.

0 commit comments

Comments
 (0)