p-ranav / argparse

Argument Parser for Modern C++
MIT License
2.59k stars 244 forks source link

Error messages on parsing failure could be clearer #362

Open cschreib-ibex opened 3 months ago

cschreib-ibex commented 3 months ago

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:

It seems the issue could be here https://github.com/p-ranav/argparse/blob/af442b4da0cd7a07b56fa709bd16571889dc7fda/include/argparse/argparse.hpp#L246-L259

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.