-
Notifications
You must be signed in to change notification settings - Fork 58
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
Allow use of alternative FlagSet implementations #56
Comments
This is a non-goal according to the README. |
No promises, but I would be interested to see what a PR looked like. |
I played with this a bit but found that the use of calls to flag.Flag is limiting. This is tricky to work around and I have a feeling you wouldn't want it in your repo. The cleanest way to do this in Go is to use an adapter type that implements the same functions with an interface{} instead of *flag.Flag. Something like type flagSetAdapter {
std *flag.FlagSet
psx *pflag.FlagSet
}
func newFlagSetAdapter(flg interface{}) (flagSetAdapter, error) {
switch flg := flg.(type) {
case *flag.FlagSet:
return flagSetAdapter{flg,nil}
// etc...
}
} This gets tricky when it comes to implementing an adapter method for |
I think a minimal set of used FlagSet methods are as follows:
Those can be easily implemented for both flag and pflag. For example:
If this seems reasonable, I can work on a PR. |
I haven't looked into it deeply, but I also think it ought to be feasible. If you want to do a preliminary PR I'd be happy to give it a review. It would be important that the existing API, using the stdlib flag.FlagSet, not change. That is, if you want to change func Parse(fs *flag.FlagSet, ...) to func Parse(fs SomeInterface, ...) it would be important that the flag.FlagSet satisfy that interface without an adapter. |
I have two different approaches and I am not sure which one is better so I sent two PRs. One(#63) with a Another one(#62) defines |
This is cool. I prefer #62, can you run with that? |
Ok, I will work on #62. I need to ask one more question. Since |
Do whatever you need to get a working prototype, then we can assess next steps if anything needs to change. I noticed pflag has some "FromGoFlag" adapters, maybe we could PR pflag to expose some of that if necessary. |
Just FYI the "official" upstream pflag repo is notoriously slow at merging PRs or addressing issues. |
Looking at this again, I'm reasonably optimistic that there exists a FlagSet abstraction which would be satisfied by the stdlib flag.FlagSet via an adapter, and also permit other implementations like pflag. Stay tuned... |
Hi @peterbourgon, I can take a stab at this feature if you'd be okay with that, but was curious what your thoughts were. I see from prior work #64 was creating a new wrapper around the
I wanted to align directionally with what you were thinking before I attempted. Any high level direction of what you are looking for? |
So the main thing is that this module must not depend on |
Out of curiosity, why is the dependency on |
|
I hope this is addressed by #113. |
Alternative FlagSet implementations such as https://github.com/spf13/pflag allow for different styles of flags.
If ff and ffcli used a minimal interface with only the functions they call, in place of flag.FlagSet, this would allow users to use the FlagSet implementation of their choosing.
The text was updated successfully, but these errors were encountered: