pflag icon indicating copy to clipboard operation
pflag copied to clipboard

not a superset of flag: missing Func()

Open muir opened this issue 3 years ago • 3 comments

As of Go 1.16, base "flag" now has:

func (f *FlagSet) Func(name, usage string, fn func(string) error)`

Please add for feature parity -- Func() is probably the best thing about the base "flag" package

muir avatar Oct 29 '21 04:10 muir

Agreed. This one is missing. Hope they'll add this.

riadevatix avatar Feb 03 '22 09:02 riadevatix

Does anyone have a temporary workaround?

avdb13 avatar Feb 03 '22 19:02 avdb13

As a workaround you can use go's flag parser:

fs := pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
goflag.Func("flagname", "help message for flagname", func(string) error {
	fmt.Println("functioning")
	return nil
})
fs.AddGoFlagSet(goflag.CommandLine)
fs.Parse([]string{"--flagname", "123"})

hoshsadiq avatar Feb 17 '22 00:02 hoshsadiq