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

Error messages on parsing failure could be clearer #362

Open
cschreib-ibex opened this issue May 31, 2024 · 0 comments
Open

Error messages on parsing failure could be clearer #362

cschreib-ibex opened this issue May 31, 2024 · 0 comments

Comments

@cschreib-ibex
Copy link

cschreib-ibex commented May 31, 2024

Currently, if trying to parse an 'u'-shaped integer

args.add_argument("--port").scan<'u', std::uint16_t>();

... we get the following confusing error messages:

> ./my-app --port abc
pattern 'abc' not found
> ./my-app --port 1.5
pattern '1.5' does not match to the end

The problems:

  • The name of the argument being parsed is not included in the error message.
  • "pattern {value}" is incorrect; the pattern is what we are searching for, but we print the value we got instead. This could be changed to "pattern for {shape}" (accurate, but perhaps not super useful to the user if they don't know what the shape 'u' actually means). Ideally we would print both the value and the shape though, for example "value 'abc' does not match expected format ('u')".

It seems the issue could be here

template <class T, auto Param = 0> struct parse_number {
auto operator()(std::string_view s) -> T {
return do_from_chars<T, Param>(s);
}
};
template <class T> struct parse_number<T, radix_2> {
auto operator()(std::string_view s) -> T {
if (auto [ok, rest] = consume_binary_prefix(s); ok) {
return do_from_chars<T, radix_2>(rest);
}
throw std::invalid_argument{"pattern not found"};
}
};

The specialization of this struct for radix_16 has try/catch blocks to intercept the low-level exception from do_from_chars to convert them to better messages (e.g. "Failed to parse {value} as hexadecimal: {reason}"). But the default parse_number (and the one for radix_2) do not. Still, none of the functions (even radix_16 which tried harder) include the name of the argument that failed parsing.

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