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 --- func.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 func.go (limited to 'func.go') diff --git a/func.go b/func.go new file mode 100644 index 0000000..e5f5436 --- /dev/null +++ b/func.go @@ -0,0 +1,37 @@ +package pflag + +// -- func Value +type funcValue func(string) error + +func (f funcValue) Set(s string) error { return f(s) } + +func (f funcValue) Type() string { return "func" } + +func (f funcValue) String() string { return "" } // same behavior as stdlib 'flag' package + +// Func defines a func flag with specified name, callback function and usage string. +// +// The callback function will be called every time "--{name}={value}" (or equivalent) is +// parsed on the command line, with "{value}" as an argument. +func (f *FlagSet) Func(name string, usage string, fn func(string) error) { + f.FuncP(name, "", usage, fn) +} + +// FuncP is like Func, but accepts a shorthand letter that can be used after a single dash. +func (f *FlagSet) FuncP(name string, shorthand string, usage string, fn func(string) error) { + var val Value = funcValue(fn) + f.VarP(val, name, shorthand, usage) +} + +// Func defines a func flag with specified name, callback function and usage string. +// +// 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) { + 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) { + CommandLine.FuncP(name, shorthand, usage, fn) +} -- 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 '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