savonet / ocaml-posix

Bindings to the various POSIX APIs
https://www.liquidsoap.info/ocaml-posix/
MIT License
27 stars 2 forks source link

getopt: Unknown_option exception raised with useless character when calling getopt_long #4

Closed wizeman closed 3 years ago

wizeman commented 3 years ago

On Linux, calling getopt_long when the user passed an unknown long option e.g. --non-existent, the Unknown_option c exception gets raised but the c argument is just a zero.

I understand this might be a limitation of the underlying getopt's interface, but I think different exception arguments should be used, otherwise there seems to be no way to know or print an error message indicating which option was invalid (also due to issue #3).

Alternatively, if the Unknown_option exception were to return the list of (unparsed?) arguments and the index of the invalid argument (probably based on optind?), then we could find out which option was invalid and print a useful error message.

toots commented 3 years ago

Hi! Thanks for this report again. Same too: what distribution are you using? These things can be very distro-specific..

wizeman commented 3 years ago

I'm using NixOS 20.09 but I think in this case it doesn't seem to be distro-specific, it seems to be a problem with the interface of this library.

Specifically, the Unknown_option exception can only carry a char, which means that if the user passed a --unknown-option there is no way to know that easily or print a useful error message (as far as I can tell), because --unknown-option doesn't fit on a char. So this doesn't seem to be just an implementation bug, because there would be no way to solve it without changing the interface.

I was thinking it would be more useful if Unknown_option would carry a string rather than a char. This way, you could pass the complete unknown option (including dash characters) in the exception so that the program could print an error message to the user.