forked from ogier/pflag
-
Notifications
You must be signed in to change notification settings - Fork 366
Open
Description
Using Go generics it's possible to greatly streamline the process of binding custom value types.
I have created a library as a POC github.com/mmatczuk/anyflag.
To work with it users have to provide the type and a parser (aka fromString) function.
Example:
var ba *url.Userinfo
cmd.Flags().VarP(anyflag.NewValue[*url.Userinfo](nil, &ba, parseBasicAuth), "basic-auth", "", "basic auth")
var bas []*url.Userinfo
cmd.Flags().VarP(anyflag.NewSliceValue[*url.Userinfo](nil, &bas, parseBasicAuth), "basic-auth", "", "basic auth")See full running example in example_test.go.
It's also possible to replace the concrete implementations here with the generic one largely reducing the code surface.
Please let me know what do you think.
Metadata
Metadata
Assignees
Labels
No labels