-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
flag: add BoolFunc(name, usage string, fn func(string)error) #53747
Comments
You mean that you want https://pkg.go.dev/flag#Bool? |
No, Im aware that bool exists, but if you need to have a function in a flag you need to always set a value... Im preparing a PR for this |
Current implementation forces a func flag to have a value When displaying the usage is possible to see a "value" required for the flag and when you execute it without flag you receive the error flag needs an argument Converted funcValue to a struct and create 2 additional functions to avoid breaking changes Fixes golang#53747
Current implementation forces a func flag to have a value When displaying the usage is possible to see a "value" required for the flag and when you execute it without flag you receive the error flag needs an argument Converted funcValue to a struct and create 2 additional functions to avoid breaking changes Fixes golang#53747
Change https://go.dev/cl/416514 mentions this issue: |
This is a really welcome change!! |
I added flag.Func. I also wrote my own helper function that works like your proposal: https://pkg.go.dev/github.com/carlmjohnson/flagx#BoolFunc I’m not totally convinced it belongs in the standard library though. So far it has been less useful than I thought it would be. It’s only slightly more convenient than just using a flag.Bool. |
For me it’s extremely useful since I’m delegating the responsibility of registering the flags to other packages and for that I have an interface kind of like a routing. So basically each package can contain n commands and each command has a function to handle it. Also as far as I understood from the existing code, it was written to allow more “boolFlag” types in the future
|
In the CL, it's not that there's no argument, it's that you must use =value. |
This proposal has been added to the active column of the proposals project |
Should it be |
It should be func(string) error like the others, because you can say
The same as a custom flag implementation that returns true from IsBoolFlag. |
Updated title. Does anyone object to adding this? |
Should I update the code to reflect these changes before the proposal review? |
While this can already be done via implementations that have |
I'm +0 on adding it. As I said, I have a version of this in my personal toolbox and so far I have been underwhelmed with how useful it is. On the other hand, it rounds out the functionality in a pretty clear way and it doesn't harm anything to have it. I do think though that if the callback signature ends up being |
@fnxpt Accepting or rejecting the proposal is independent of the sample implementation. So, sure, update it, but no rush. |
It seems like this is missing and fills in a hole that people can fall into otherwise. So even though it's not going to be an everyday kind of function, it seems worth adding. |
Based on the discussion above, this proposal seems like a likely accept. |
renamed the function to BoolFunc. Runned the tests and it fails on API check, in which file should I signature? |
Thanks @carlmjohnson |
No change in consensus, so accepted. 🎉 |
@rsc what are the next steps. There is already a PR with the changes mentioned |
@fnxpt Do you want to update the PR to fix the merge conflict, or are you not working on this anymore? |
Fixes golang#53747 Supercedes #416514
Change https://go.dev/cl/476015 mentions this issue: |
Fixes golang#53747 Based on [CL 416514](https://go.dev/cl/416514)
Change https://go.dev/cl/499417 mentions this issue: |
For #53747 Change-Id: Ia5e2f89c1184f2dfd6d672b838b0dbb579e6c954 Reviewed-on: https://go-review.googlesource.com/c/go/+/499417 Reviewed-by: Eli Bendersky <eliben@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Bypass: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
Using flag.Func forces you to always pass an argument. Sometimes we don't really need an argument and its really convenient to be able to just call this flags without passing any dummy value.
Example:
Setting the flag --start-server will ask you to specify a value and also mentions it on the usage
-start-server value
Starts the server
The text was updated successfully, but these errors were encountered: