Skip to content

Commit fdc249a

Browse files
committed
Fix usage showing zero defaults for custom Value type
The defaultIsZeroValue() method was erroneously assuming that all types that implement IsBoolFlag() were bools - regardless of the return value of IsBoolFlag(). Fixes spf13#360
1 parent d5e0c06 commit fdc249a

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

flag.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,10 @@ func (f *FlagSet) PrintDefaults() {
536536
// defaultIsZeroValue returns true if the default value for this flag represents
537537
// a zero value.
538538
func (f *Flag) defaultIsZeroValue() bool {
539-
switch f.Value.(type) {
540-
case boolFlag:
539+
if bf, ok := f.Value.(boolFlag); ok && bf.IsBoolFlag() {
541540
return f.DefValue == "false"
541+
}
542+
switch f.Value.(type) {
542543
case *durationValue:
543544
// Beginning in Go 1.7, duration zero values are "0s"
544545
return f.DefValue == "0" || f.DefValue == "0s"

flag_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,6 @@ func TestMultipleNormalizeFlagNameInvocations(t *testing.T) {
11341134
}
11351135
}
11361136

1137-
//
11381137
func TestHiddenFlagInUsage(t *testing.T) {
11391138
f := NewFlagSet("bob", ContinueOnError)
11401139
f.Bool("secretFlag", true, "shhh")
@@ -1149,7 +1148,6 @@ func TestHiddenFlagInUsage(t *testing.T) {
11491148
}
11501149
}
11511150

1152-
//
11531151
func TestHiddenFlagUsage(t *testing.T) {
11541152
f := NewFlagSet("bob", ContinueOnError)
11551153
f.Bool("secretFlag", true, "shhh")
@@ -1202,6 +1200,8 @@ func (cv *customValue) Set(s string) error {
12021200

12031201
func (cv *customValue) Type() string { return "custom" }
12041202

1203+
func (cv *customValue) IsBoolFlag() bool { return false }
1204+
12051205
func TestPrintDefaults(t *testing.T) {
12061206
fs := NewFlagSet("print defaults test", ContinueOnError)
12071207
var buf bytes.Buffer
@@ -1239,7 +1239,7 @@ func TestPrintDefaults(t *testing.T) {
12391239
got := buf.String()
12401240
if got != defaultOutput {
12411241
fmt.Println("\n" + got)
1242-
fmt.Println("\n" + defaultOutput)
1242+
fmt.Print("\n" + defaultOutput)
12431243
t.Errorf("got %q want %q\n", got, defaultOutput)
12441244
}
12451245
}

0 commit comments

Comments
 (0)