From 69bc3bd5b37fa90e994be9acecf7430269591713 Mon Sep 17 00:00:00 2001 From: Georges Varouchas Date: Thu, 22 May 2025 22:56:18 +0400 Subject: add support for Func() and BoolFunc() #426 Add support for two features which landed in the 'flag' package from the standard library: Func() and BoolFunc() and their two pflag specific versions: FuncP() and BoolFuncP() fixes #426 --- bool_func.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 bool_func.go (limited to 'bool_func.go') diff --git a/bool_func.go b/bool_func.go new file mode 100644 index 0000000..05783a9 --- /dev/null +++ b/bool_func.go @@ -0,0 +1,40 @@ +package pflag + +// -- func Value +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) String() string { return "" } // same behavior as stdlib 'flag' package + +func (f boolfuncValue) IsBoolFlag() bool { return true } + +// BoolFunc defines a func flag with specified name, callback function and usage string. +// +// The callback function will be called every time "--{name}" (or any form that matches the flag) is parsed +// on the command line. +func (f *FlagSet) BoolFunc(name string, usage string, fn func(string) error) { + f.BoolFuncP(name, "", usage, fn) +} + +// BoolFuncP is like BoolFunc, but accepts a shorthand letter that can be used after a single dash. +func (f *FlagSet) BoolFuncP(name, shorthand string, usage string, fn func(string) error) { + var val Value = boolfuncValue(fn) + flag := f.VarPF(val, name, shorthand, usage) + flag.NoOptDefVal = "true" +} + +// BoolFunc defines a func flag with specified name, callback function and usage string. +// +// The callback function will be called every time "--{name}" (or any form that matches the flag) is parsed +// on the command line. +func BoolFunc(name string, usage string, fn func(string) error) { + CommandLine.BoolFuncP(name, "", usage, fn) +} + +// BoolFuncP is like BoolFunc, but accepts a shorthand letter that can be used after a single dash. +func BoolFuncP(name, shorthand string, fn func(string) error, usage string) { + CommandLine.BoolFuncP(name, shorthand, usage, fn) +} -- cgit v1.2.3 From 4730aa0d979f34d4f7427d524b84043557ba72ef Mon Sep 17 00:00:00 2001 From: Georges Varouchas Date: Mon, 9 Jun 2025 22:38:23 +0400 Subject: 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 --- bool_func.go | 2 +- bool_func_test.go | 30 ++++++++++++++++++++++++++++++ flag.go | 8 +++++--- func_test.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) (limited to 'bool_func.go') 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) + } + }) +} diff --git a/flag.go b/flag.go index 0b5be7a..d4dfbc5 100644 --- a/flag.go +++ b/flag.go @@ -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) + } + }) +} -- cgit v1.2.3 From 1a4b5b2e5c7ee4a194cebc579bb34198187df73d Mon Sep 17 00:00:00 2001 From: Georges Varouchas Date: Fri, 27 Jun 2025 18:02:03 +0200 Subject: fix discrepancy in order of arguments for Func() and BoolFunc() #433 for no good reason: the order of arguments would differ when calling pflag.BoolFuncP(...) and (*FlagSet).BoolFuncP(...) (same goes for Func() and FuncP()) in this commit: align all functions on stdlib's order fixes #433 --- bool_func.go | 2 +- func.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'bool_func.go') diff --git a/bool_func.go b/bool_func.go index 76422bf..83d77af 100644 --- a/bool_func.go +++ b/bool_func.go @@ -35,6 +35,6 @@ func BoolFunc(name string, usage string, fn func(string) error) { } // BoolFuncP is like BoolFunc, but accepts a shorthand letter that can be used after a single dash. -func BoolFuncP(name, shorthand string, fn func(string) error, usage string) { +func BoolFuncP(name, shorthand string, usage string, fn func(string) error) { CommandLine.BoolFuncP(name, shorthand, usage, fn) } diff --git a/func.go b/func.go index e5f5436..9f4d88f 100644 --- a/func.go +++ b/func.go @@ -27,11 +27,11 @@ func (f *FlagSet) FuncP(name string, shorthand string, usage string, fn func(str // // The callback function will be called every time "--{name}={value}" (or equivalent) is // parsed on the command line, with "{value}" as an argument. -func Func(name string, fn func(string) error, usage string) { +func Func(name string, usage string, fn func(string) error) { CommandLine.FuncP(name, "", usage, fn) } // FuncP is like Func, but accepts a shorthand letter that can be used after a single dash. -func FuncP(name, shorthand string, fn func(string) error, usage string) { +func FuncP(name, shorthand string, usage string, fn func(string) error) { CommandLine.FuncP(name, shorthand, usage, fn) } -- cgit v1.2.3