diff options
| author | Georges Varouchas <[email protected]> | 2025-06-09 22:38:23 +0400 |
|---|---|---|
| committer | Georges Varouchas <[email protected]> | 2025-06-09 23:14:57 +0400 |
| commit | 4730aa0d979f34d4f7427d524b84043557ba72ef (patch) | |
| tree | 1216ad677454c2ffba6da37299a473a2296d5b98 | |
| parent | f4c97c2487b06cff392d2958534e7195f79847fb (diff) | |
fix help message for Func and BoolFunc flags #430
* have '.Type()' for boolfuncValue return "boolfunc" (dedicated value,
which now makes it distinct from funcValue)
* hide extra "(default )" by stating that "" should be treated as
a zero value for a boolFlag
note:
- a boolfuncValue matches 'case boolFlag:', as it
implements the boolFlag interface,
- treating "" as a value which shouldn't trigger a "(default )"
for a regular Bool flag does not look like a breaking change
* hide extra "[=something]" for boolfuncValue
* set default placeholder name for "boolfunc" and "func" flag types
| -rw-r--r-- | bool_func.go | 2 | ||||
| -rw-r--r-- | bool_func_test.go | 30 | ||||
| -rw-r--r-- | flag.go | 8 | ||||
| -rw-r--r-- | func_test.go | 30 |
4 files changed, 66 insertions, 4 deletions
diff --git a/bool_func.go b/bool_func.go index 05783a9..76422bf 100644 --- a/bool_func.go +++ b/bool_func.go @@ -5,7 +5,7 @@ type boolfuncValue func(string) error func (f boolfuncValue) Set(s string) error { return f(s) } -func (f boolfuncValue) Type() string { return "func" } +func (f boolfuncValue) Type() string { return "boolfunc" } func (f boolfuncValue) String() string { return "" } // same behavior as stdlib 'flag' package diff --git a/bool_func_test.go b/bool_func_test.go index ffb4fa9..c16be83 100644 --- a/bool_func_test.go +++ b/bool_func_test.go @@ -145,3 +145,33 @@ func TestBoolFuncCompat(t *testing.T) { } }) } + +func TestBoolFuncUsage(t *testing.T) { + t.Run("regular func flag", func(t *testing.T) { + // regular boolfunc flag: + // expect to see '--flag1' followed by the usageMessage, and no mention of a default value + fset := NewFlagSet("unittest", ContinueOnError) + fset.BoolFunc("flag1", "usage message", func(s string) error { return nil }) + usage := fset.FlagUsagesWrapped(80) + + usage = strings.TrimSpace(usage) + expected := "--flag1 usage message" + if usage != expected { + t.Fatalf("unexpected generated usage message\n expected: %s\n got: %s", expected, usage) + } + }) + + t.Run("func flag with placeholder name", func(t *testing.T) { + // func flag, with a placeholder name: + // if usageMesage contains a placeholder, expect '--flag2 {placeholder}'; still expect no mention of a default value + fset := NewFlagSet("unittest", ContinueOnError) + fset.BoolFunc("flag2", "usage message with `name` placeholder", func(s string) error { return nil }) + usage := fset.FlagUsagesWrapped(80) + + usage = strings.TrimSpace(usage) + expected := "--flag2 name usage message with name placeholder" + if usage != expected { + t.Fatalf("unexpected generated usage message\n expected: %s\n got: %s", expected, usage) + } + }) +} @@ -549,7 +549,7 @@ func (f *FlagSet) PrintDefaults() { func (f *Flag) defaultIsZeroValue() bool { switch f.Value.(type) { case boolFlag: - return f.DefValue == "false" + return f.DefValue == "false" || f.DefValue == "" case *durationValue: // Beginning in Go 1.7, duration zero values are "0s" return f.DefValue == "0" || f.DefValue == "0s" @@ -599,8 +599,10 @@ func UnquoteUsage(flag *Flag) (name string, usage string) { name = flag.Value.Type() switch name { - case "bool": + case "bool", "boolfunc": name = "" + case "func": + name = "value" case "float64": name = "float" case "int64": @@ -718,7 +720,7 @@ func (f *FlagSet) FlagUsagesWrapped(cols int) string { switch flag.Value.Type() { case "string": line += fmt.Sprintf("[=\"%s\"]", flag.NoOptDefVal) - case "bool": + case "bool", "boolfunc": if flag.NoOptDefVal != "true" { line += fmt.Sprintf("[=%s]", flag.NoOptDefVal) } diff --git a/func_test.go b/func_test.go index 9be74fa..d492b48 100644 --- a/func_test.go +++ b/func_test.go @@ -151,3 +151,33 @@ func TestFuncCompat(t *testing.T) { } }) } + +func TestFuncUsage(t *testing.T) { + t.Run("regular func flag", func(t *testing.T) { + // regular func flag: + // expect to see '--flag1 value' followed by the usageMessage, and no mention of a default value + fset := NewFlagSet("unittest", ContinueOnError) + fset.Func("flag1", "usage message", func(s string) error { return nil }) + usage := fset.FlagUsagesWrapped(80) + + usage = strings.TrimSpace(usage) + expected := "--flag1 value usage message" + if usage != expected { + t.Fatalf("unexpected generated usage message\n expected: %s\n got: %s", expected, usage) + } + }) + + t.Run("func flag with placeholder name", func(t *testing.T) { + // func flag, with a placeholder name: + // if usageMesage contains a placeholder, expect that name; still expect no mention of a default value + fset := NewFlagSet("unittest", ContinueOnError) + fset.Func("flag2", "usage message with `name` placeholder", func(s string) error { return nil }) + usage := fset.FlagUsagesWrapped(80) + + usage = strings.TrimSpace(usage) + expected := "--flag2 name usage message with name placeholder" + if usage != expected { + t.Fatalf("unexpected generated usage message\n expected: %s\n got: %s", expected, usage) + } + }) +} |
