the D scripting language -- command line

Tomek Sowiński just at ask.me
Thu Nov 11 14:04:32 PST 2010


spir napisał:

>> // 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 thought once at a default interface between the command-line and a
> program's startup routine, main(). The idea would be for main to have
> parameters automagically fed from whatever the user provides. With a
> command-line syntax inspired by named func call. It lets the program
> itself be called more or less like a func, via program-level parameter
> definition:
> 
> // program "findword"
> int main (string filename, string word, bool verbose=false) {...}
> 
> // use it
> $ findword filename=foo.txt word=foo verbose=true
> 
> Parameters having default value can indeed be omitted by the user.
> Optionally, the first arg (often corresponding to the "object" on which a
> command applies, like the receiver in OO, and thus usually "obvious") can
> be automatically mapped to the first param of main:
> 
> $ findword foo.txt word=foo
> 
> This is, indeed, a language-specific feature. On the other hand, it brings
> to the user consistent program-call format -- rather than each program
> defining its own. Maybe after sometime the same syntax trick and syntax
> would adopted by other languages. I find this very coool on the programmer
> side as well. It must indeed by implemented in the language('s runtime)
> itself. To have such a feature without breaking any code, a possibility
> may be to use a different name than "main", eg "script". (I guess such a
> feature would make some difference in judging a language's adequacy to
> scripting.)
> 
> What do you think?

Nice and terse, but let's not get carried away with the whole scripting thing. I'd 
be more than happy with getopt working at capacity. It can easily phase out 
most common boilerplate arg parsing.

-- 
Tomek


More information about the Digitalmars-d mailing list