Thread overview
Weird behavior with getopt for invalid user input
Dec 08, 2018
Andrew Pennebaker
Dec 09, 2018
Andrew Pennebaker
Dec 09, 2018
zabruk
December 08, 2018
In many getopt implementations, the -h/--help banner is printed when the user supplies invalid arguments. However, D's getopt instead throws an exception, printing a stack trace and confusing some non-technical users.

That's okay, we can catch GetoptException and try to print the usage information instead. But that also doesn't work so well, as the getopt spec cannot bind to a variable when an exception is thrown. Again, other getopt implementations tend to separate the getopt spec object construction from argument validation into separate function calls, which gives the programmer more flexibility for how to handle any errors.

Sigh, when I manually `defaultGetoptPrinter(format("Usage: %s [OPTIONS]", program), opts.options);`, I think I can do this by typing out the getopt spec a second time, overriding the args with ["-h"]. But this is starting to require a ridiculous number of hacks, when getopt could have just executed -h on error, as many users would expect.
December 09, 2018
On Saturday, 8 December 2018 at 19:48:24 UTC, Andrew Pennebaker wrote:
> In many getopt implementations, the -h/--help banner is printed when the user supplies invalid arguments. However, D's getopt instead throws an exception, printing a stack trace and confusing some non-technical users.
>
> That's okay, we can catch GetoptException and try to print the usage information instead. But that also doesn't work so well, as the getopt spec cannot bind to a variable when an exception is thrown. Again, other getopt implementations tend to separate the getopt spec object construction from argument validation into separate function calls, which gives the programmer more flexibility for how to handle any errors.
>
> Sigh, when I manually `defaultGetoptPrinter(format("Usage: %s [OPTIONS]", program), opts.options);`, I think I can do this by typing out the getopt spec a second time, overriding the args with ["-h"]. But this is starting to require a ridiculous number of hacks, when getopt could have just executed -h on error, as many users would expect.

Posted a workaround:

https://forum.dlang.org/post/kfupjcpsovzcxwaifyst@forum.dlang.org
December 09, 2018
https://issues.dlang.org/show_bug.cgi?id=14525
Reported: 2015-04-29