-
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: clean up error message #26822
Comments
The error message can be improved, but changing the public API requires going through the proposal process. Updating the issue. |
Thanks a lot, @robpike, for the quick response. Much appreciated! Improving the error messages sounds good to me. |
What's missing from this proposal is the proposal of what the new API looks like. The current five errors are:
As far as exported API, can we make do with a single new flag.Error struct, or do these need to be multiple error types? Maybe something like:
is at least a starting point for a discussion. |
@rsc, thanks a lot for looking into the issue.
[updated]: It may be the lack of coffee, but the meaning of the |
I'm not sure what |
Implicit was to distinguish between -x=true and -x for a boolean x. (is "Value" implicit?) Does anyone want to sketch an implementation and based on that send back a proposed API? |
I've started work on an implementation to explore this. |
Change https://golang.org/cl/141017 mentions this issue: |
CL 141017 implements typed parse errors for discussion in this issue. I felt that there were two distinct categories of errors that would be useful to distinguish: undefined/unknown flags and syntax errors for known flags. I represented these with two new error types: // UndefinedError is the custom error type returned when Parse encounters
// a flag that is not defined in the FlagSet.
type UndefinedError struct {
Name string
}
// SyntaxError is the custom error type returned when Parse encounters
// a flag value or name that cannot be parsed.
type SyntaxError struct {
Flag *Flag // Flag may be nil if the flag name could not be determined
Value string
Err error
} The current error strings are mapped to the two new types and I added a test to check compatibility for people who may be currently matching string prefixes. |
/cc @robpike |
I am not convinced we need special types here. I don't see how a program needs to behave in an important different way based on the particular way a flag fails. The current state is very simple. Why not just improve the error messages? |
@robpike changing the error messages would break existing users who are already string matching on the error text. Hyrum's rule in play. The design I put in the CL would let people cleanly detect the type of error and also gives them the core information to build their own error message. It preserves existing messages for the string matchers like the poster of comment above. |
I agree with that being the reason, my question was whether the reasons apply to the flag package, whose errors do not require recovery or special handling. If the command line is wrong, the program does not continue. Even subcommands do not continue. The user reads the help message and tries again. |
The particular issue that led to this proposal is using FlagSet to enable the overlay of some additional parsing rules:
|
That is an argument for a different, external, flags package, one with different rules for parsing flags, not an argument for changing this one. |
I don't think so. The inclusion of the FlagSet type is a signal to users that the flag package is intended to be used for creating alternate flag arrangements such as for subcommands. A FlagSet is also not tied to the current program's command line. It could be used to check arguments being provided to another program. Providing better information about failures seems reasonable. |
If you want to look at the registered flags and make a new flag parser, do that. Don't try to parse the parser errors - just do a fresh parse. See rsc.io/getopt for an example. |
Let's fix the error messages to be cleaner and then worry about whether to export any new API separately. Assigning to Rob and repurposing this issue for the error messages alone. |
Change https://golang.org/cl/143257 mentions this issue: |
What version of Go are you using (
go version
)?go version go1.9.7 linux/amd64 (but it affects basically all versions of go).
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?GOHOSTARCH="amd64"
GOHOSTOS="linux"
What did you do?
Provide a non-numerical input for an integer flag yields the following error:
What did you expect to see?
Something more user-friendly. Currently, the error message must be interpreted by the user and should guide users a bit more in a direction to solve the issue. See containers/buildah#673 as an example.
What did you see instead?
Proposal
I want to suggest two options:
ErrInvalidValue
andErrInvalidBoolen
) that can be used by users of theflag
package (e.g.,urfave/cli
).The text was updated successfully, but these errors were encountered: