@@ -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 )
0 commit comments