Fixing valid options for std.getopt

Jens Mueller jens.k.mueller at gmx.de
Wed Aug 10 00:10:11 PDT 2011


Dmitry Olshansky wrote:
> On 10.08.2011 1:32, Jens Mueller wrote:
> >Hi,
> >
> >I've added some changes to getopt to change when an options is
> >accepted/rejected. I'm going to describe its old behavior in contrast
> >with its old behavior.
> >
> >Consider parsing an option of Type T:
> >
> >T timeout;
> >getopt(args, "t|timeout",&timeout);
> >
> >Short options (single dash):
> >-t v accept (was rejected)
> +1 on this, I recall spending about an hour trying to get why -t v
> is not working. Though I dunno why change -tv, I think it's widely
> used form and not only for numerics.

I believe it's only safe for numerics. Consider the following example.
-tsomestring was passed because -t is supported short option but your
program also accepts the boolean long option --tsomestring. Now
forgetting a single dash will change the behavior silently.
One can argue it's the programmer's job to not define options in this
combination. Basically whenever you define a non-boolean short option
don't specify a long option starting with the same letter. Allowing it
only for numeric values avoids having this rule. Thus, it's less
error-prone I think.

Jens


More information about the Digitalmars-d mailing list