std.getopt suggestion

Nick Sabalausky a at a.a
Sat Oct 1 14:17:31 PDT 2011


"Andrei Alexandrescu" <SeeWebsiteForEmail at erdani.org> wrote in message 
news:j67s2l$9h2$1 at digitalmars.com...
> On 10/1/11 12:56 PM, Nick Sabalausky wrote:
>> "Andrei Alexandrescu"<SeeWebsiteForEmail at erdani.org>  wrote in message
>> news:j679o0$1hdm$1 at digitalmars.com...
>>> On 10/1/11 5:42 AM, Jacob Carlborg wrote:
>>>>
>>>> I forgot to mention that I don't want the order of the arguments to
>>>> matter. I should be possible to write:
>>>>
>>>> "foo bar -b -a"
>>>>
>>>> Or at least it should be possible to put global options anywhere in the
>>>> command line.
>>>>
>>>> So I guess that would be using the passThrough option the first time
>>>> getopt is called and then using the noPassThrough option when it's
>>>> called the second time from the action implementation.
>>>>
>>>> So there we have a use case for calling getopt more than once, at least
>>>> I have a use case for that.
>>>
>>> Clearly there are legit use cases. The use of monostate does not impede
>>> them.
>>>
>>
>> It makes them error-prone whenever you want to use a different option.
>>
>> The use of Jon's suggestion does not impede the single-use cases. But it
>> does make the other cases less problematic. So there's benefit with no
>> downside.
>
> I don't see it like that. Someone who's liable to make errors with one 
> function and three variables is just as liable to make errors with one 
> function and one object containing three variables.
>

It's well established that the errors with three global vars are a hell of a 
lot easier to make. And yes, that *does* apply to getopt:

------------------------------------------------------
// Original version
void oneFunc(...)
{
    ...
    getopt(...); // Use defaults
}
void anotherFunc(...)
{
    ...
    getopt(...); // Use defaults
}
oneFunc(...);
anotherFunc(...);
------------------------------------------------------
// Make change: Version 2
void oneFunc(...)
{
    ...
    // Don't want defaults anymore
    endOfOptions = ...;
    optionChar = ...;
    getopt(...);
}
void anotherFunc(...)
{
    ...
    getopt(...); // Use defaults OH FUCK THEY'RE NOT THE DEFAULTS ANYMORE 
BOOM
}
oneFunc(...);
anotherFunc(...);
------------------------------------------------------

Now compare that with the proposal:

------------------------------------------------------
// Same damn original version
void oneFunc(...)
{
    ...
    getopt(...); // Use defaults
}
void anotherFunc(...)
{
    ...
    getopt(...); // Use defaults
}
oneFunc(...);
anotherFunc(...);
------------------------------------------------------
// Make change: Version 2B
void oneFunc(...)
{
    ...
    // Don't want defaults anymore
    auto opts = GetOpt(endOfOptions:..., optionChar:...);
    opts.getopt(...);
}
void anotherFunc(...)
{
    ...
    getopt(...); // Use defaults JESUS FUCK LOOK AT THAT IT'S STILL FUCKING 
WORKING **RIGHT** HOLY CRAP
}
oneFunc(...);
anotherFunc(...);
------------------------------------------------------

Wow! Every damn programmer in the world was right all along! Globals *are* 
worse! Who'd have thought?!




More information about the Digitalmars-d mailing list