Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Value and SliceValue implementation #359

Open
mmatczuk opened this issue Sep 22, 2022 · 0 comments
Open

Generic Value and SliceValue implementation #359

mmatczuk opened this issue Sep 22, 2022 · 0 comments

Comments

@mmatczuk
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant