the D scripting language -- command line

Adam Ruppe destructionator at gmail.com
Thu Nov 11 05:45:33 PST 2010


spir wrote:
> I thought once at a default interface between the command-line and a
> program's startup routine, main().

We could actually do this with a mixin.

======
 int findword (string filename, string word, bool verbose=false) {...}

mixin MakeMain!(findword);
======


And that MakeMain template reads the arguments off findword() and creates a
traditional main() that translates its args into the needed arguments of the function.


I actually wrote something that does this already, though my goal was to automate
the creation of web apps, it also (used to - I broke it in my last revision) works
for command line programs.

http://arsdnet.net/dcode/web.d

See it in action with an example. Given this function:

   void addTopicToForum(int topicId, int forumId) {
                assert(0, "not implemented");
   }

We get this page:

http://arsdnet.net/cgi-bin/forum/add-topic-to-forum?topicId=10


>From just the function signature, it creates a form, populating it with all the
arguments given on the URL, asking the user for the rest. If you give it all the
arguments needed up front, it just skips to executing the function.


The command line interface worked basically the same way.

$ ./forum forumId=30 topidId=10
Assert error: not implemented


But when adding support for array arguments in CGI, I broke the command line
reader so I commented it out and haven't gotten back to fixing it yet.


While my code is based on taking a struct to the make main mixin, so it can do
multiple functions at once, it'd be pretty easy to modify it to just take one
function directly too.


You're free to take it and do whatever you want with it if you're interested.


More information about the Digitalmars-d mailing list