the D scripting language -- command line

spir denis.spir at gmail.com
Thu Nov 11 02:27:36 PST 2010


[started separate thread]

On Thu, 11 Nov 2010 00:58:31 +0100
Tomek Sowiński <just at ask.me> 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 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?

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d mailing list