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.
Andrei