The D Scripting Language

Steven Schveighoffer schveiguy at yahoo.com
Thu Nov 11 04:17:57 PST 2010


On Wed, 10 Nov 2010 19:12:26 -0500, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 11/10/10 3:58 PM, Tomek Sowiński wrote:
>> Andrei Alexandrescu napisał:
>>
>>>> Speaking of getopt, when writing the 'grep' snippet I missed anonymous
>>>> options a lot:
>>>>
>>>> bool h, i; string expr; string[] files;
>>>> getopt(args, "h",&h, "i",&i,&expr,&files);
>>>>
>>>> They can be implemented with relatively little effort.
>>>
>>> Not getting the example. How would anonymous options work?
>>
>> // Let's match assignments.
>> auto args = ["program.exe", ".*=.*;", "file1.d", "file2.d", "file3.d"];
>> bool h, i; string expr; string[] files;
>> getopt(args, "h",&h, "i",&i,&expr,&files);
>> assert(!h);
>> assert(!i);
>> assert(expr == ".*=.*;");
>> assert(files == ["file1.d", "file2.d", "file3.d"]);
>> assert(args == ["program.exe"]);
>>
>> Staying conservative, anonymous options would only be allowed at the  
>> end of the
>> option list, because their order matters (unlike named options).  
>> Perhaps this can
>> be relaxed with time.
>
> I still don't see added value over the existing situation. Currently  
> getopt leaves whatever wasn't an option in args[1 .. $] (without  
> shuffling order), so the code above would simply use args[1] for expr  
> and args[2 .. $] for files.

1. uses same type check/conversion that is used for options
2. Less boilerplate code.

For 2, without anonymous options, you either use args[1] everywhere, or  
you assign args[1] to a variable at the top.  To just do it in one line is  
convenient, I kind of like the idea.

-Steve


More information about the Digitalmars-d mailing list