crash on args.getopt

FG via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 25 08:29:31 PST 2015


On 2015-01-25 at 11:42, Tobias Pankrath wrote:
> On Sunday, 25 January 2015 at 10:21:34 UTC, Suliman wrote:
>> But is it good practice to fail with exception during passing unknown parameters? Maybe std.getopt.config.passThrough should be as default?
>>
>> I really can't remember Apps that crush if pass to in unknown parameters.
>
> Almost all programs fail with an error message, if you pass unknown parameter. Just catch that exception.

It's much better to fail up front than to create the illusion that everything is fine after the user has mistyped one of the parameters. Let's say "foo a b" copies from a to b, but "foo --reverse a b" does the opposite. Then, when someone types for example "foo --revrse a b", and it is silently accepted, the program does exactly the opposite of what the user expects!

A side note: GTK applications accept extra parameters used to initialize GTK, but it is not passThrough. What really happens is that the app calls gtk_init(&argc, &argv) (or Main.init(args) in case of GtkD) before it starts parsing arguments on its own. The call to gtk_init removes the options recognized by GTK from the list of program arguments, and this filtered array can be then processed in a normal way -- which means that whichever parameters are still left unrecognised, the program should fail with an error message.


More information about the Digitalmars-d-learn mailing list