spf13 / pflag

Drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
BSD 3-Clause "New" or "Revised" License
2.43k stars 348 forks source link

not a superset of flag: missing Func() #335

Open muir opened 3 years ago

muir commented 3 years ago

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

riadevatix commented 2 years ago

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

avdb13 commented 2 years ago

Does anyone have a temporary workaround?

hoshsadiq commented 2 years ago

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"})