-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
proposal: flag: add Func #39557
Comments
In the compiler we have this badly-named function:
A better name would be flag.Func probably. One downside of this is that returning the empty string from String means that flag.PrintDefaults never prints a default for that setting. Maybe that's OK. It's unclear that the value argument is necessary in this case. Maybe the API (at the top level, same in FlagSet) would be:
Thoughts? /cc @robpike |
In my flagext package, I set the default in Usage with value but have to explain in the docs that it doesn't do anything besides change the help text. It might be simpler to just drop it and let users write |
I think it should be |
Sorry for the rapid fire responses. If the signature were |
Yes, returning an error from the func makes a lot of sense. OK, so it sounds like the API is:
Any other thoughts? |
Seems good to me. :-) What should the example in the docs do that would be wryly funny? |
Change https://golang.org/cl/240014 mentions this issue: |
There's a CL but the proposal is still open. I had to read the issue and the CL code several times to understand what this is and why we want it. It's not a bad idea, just badly explained. |
Yes, the code part of the CL is trivial, so I just sent it off during a down period today. The hard part will be writing a good docstring. |
Ignoring the CL, it sounds like people are generally in favor of the API discussed above. Based on the discussion, then, this seems like a likely accept. |
We can work out the docs separately. No change in consensus, so accepted. |
Now that Go 1.15 is out, can the CL be reviewed or do I need to do something else first? |
For those stuck with Go below 1.16, you can already use the type |
I wrote a library of extensions to the flag package. Most of them are too specific to fit into flag itself or easy enough to write on one's own that there's no real point in adding them to the standard library, but there is one extension that I think would make a good addition to the standard library, flagext.Callback.
flagext.Callback is essentially just a wrapper that converts a function to a flag.Value. flag.Value has two methods (plus an optional third method for flag.Getter), but the
String() string
(andGet() interface{}
) method is pure boilerplate and can be ignored by just putting a Stringer onto a barefunc(s string) error
.My function's signature is
Example:
I think that this, like http.HandlerFunc, is so convenient that it would be good to have in the standard library. If it were in the standard library, it could be a method on flagset instead of a function that takes a flagset, so adding it to the standard library would have advantages that couldn't be reproduced by just importing my external package.
My proposed addition is
and the corresponding top level package function.
The text was updated successfully, but these errors were encountered: