program arguments in module constructor?

Sean Kelly sean at f4.ca
Tue May 16 11:31:25 PDT 2006


Johan Granberg wrote:
> Can this bee done?
> 
> //file.d
> static this()
> {
>     char[][] args=getArgs();//
>     someFunk(args);
> }
> 
> void main(char[][] args)
> {
>     runProgram();
> }
> //end of file
> 
> Basically i want to access the args argument passed to main before main 
> is called. I think that when we have module constructors we should bee 
> able to do this (if we already can pleas tell me).

There is no way to currently do this in user code.  However, you could 
modify internal/dmain2.d to do something like this:

     // at file scope
     char[][] args;

     extern (C) char[][] getArgs() { return args; }

     int main( int argc, char** argv ) {
         // gc_init and related code
         // set global args instead of a local var
         // loop on module ctors
         // call D main
     }

and do this in your module:

     extern (C) char[][] getArgs();

     static this() {
         char[][] args = getArgs();
         // use args
     }

I'm not sure I'd want to build this into Ares however, as technically, 
main() is the entry point for the program.  Also, it would create a 
difference between Ares and Phobos that would make for portability 
problems between the two.


Sean



More information about the Digitalmars-d mailing list