www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - program arguments in module constructor?

reply Johan Granberg <lijat.meREM OVEgmail.com> writes:
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).

(The problem i try to solve is working around the SDLmain hack to 
initialize SDL in the module constructor of my library so that when main 
is called the SDL will already bee initialized)

Thanks in advance.
May 16 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
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
May 16 2006
parent Johan Granberg <lijat.meREM OVEgmail.com> writes:
Sean Kelly wrote:

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

the problem. (I have a temporary fix where i call a function the first thing I do in main (that calls main recursively at a later stage) )
 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

if this should bee implemented Walter would have to do it.
May 16 2006
prev sibling next sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
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).
 
 (The problem i try to solve is working around the SDLmain hack to 
 initialize SDL in the module constructor of my library so that when main 
 is called the SDL will already bee initialized)
 
 Thanks in advance.

I'm not sure how it might be accomplished in linux, but in Windows you could use 'LPTSTR GetCommandLineA()' to retrieve it, but it will be as 'char*' rather than 'char[][]'. There is 'LPWSTR* CommandLineToArvW(LPCWSTR, int*)' to convert it to an argc/argv pair, but it isn't available in ASCII mode that I know of. -- Chris Nicholson-Sauls
May 16 2006
parent reply Georg Wrede <georg.wrede nospam.org> writes:
Chris Nicholson-Sauls wrote:
 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).

 (The problem i try to solve is working around the SDLmain hack to 
 initialize SDL in the module constructor of my library so that when 
 main is called the SDL will already bee initialized)

 Thanks in advance.

I'm not sure how it might be accomplished in linux, but in Windows you could use 'LPTSTR GetCommandLineA()' to retrieve it, but it will be as 'char*' rather than 'char[][]'. There is 'LPWSTR* CommandLineToArvW(LPCWSTR, int*)' to convert it to an argc/argv pair, but it isn't available in ASCII mode that I know of.

And then there's the compatibility problem of different command line handling by the operating system. In MSDOS "myprog foo.*" gives the program a single argument containing "foo.*". In Linux, "myprog foo.*" gives the program either a command line with all the file names matching foo.*, or if there are none, the string foo.* itself.
May 16 2006
parent reply Johan Granberg <lijat.meREM OVEgmail.com> writes:
Georg Wrede wrote:
 And then there's the compatibility problem of different command line 
 handling by the operating system.
 
 In MSDOS "myprog foo.*" gives the program a single argument containing 
 "foo.*".
 
 In Linux, "myprog foo.*" gives the program either a command line with 
 all the file names matching foo.*, or if there are none, the string 
 foo.* itself.

Does not matter for all uses, in my case I needed it to "FIX" an ugly main hack (SDLmain) in SDL for use in a library of mine.
May 19 2006
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Johan Granberg wrote:

 Does not matter for all uses, in my case I needed it to "FIX" an ugly 
 main hack (SDLmain) in SDL for use in a library of mine.

Can't you just require the user to rewrite their main() ? extern(C) int SDL_main(int argc, char **argv) { ... } int main(char[][] args) { return SDL_InitApplication(args); } That's what I did. --anders
May 19 2006
parent reply Johan Granberg <lijat.meREM OVEgmail.com> writes:
Anders F Björklund wrote:
 That's what I did.
 --anders

I try to avoid it because it's a practice i dislike. But in the end I might do it anyway. (I realized that if someone using C linked to my library the gc and module constructors would have to bee initialized)
May 19 2006
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Johan Granberg wrote:

 I try to avoid it because it's a practice i dislike. But in the end I 
 might do it anyway.

It's a kludge, that's for sure. GLUT has a more sane approach to it. SDL has some other evil uses of macros in the headers, as well... As I wrote somewhere else, one alternative is using the Carbon version for Mac OS X which is easier to initialize since it is using C and not Objecive-C like the Cocoa version. And I guess you also have the X11 ? In the end, rewriting all macros is probably the most likely to work. I'm doing the same thing for the "require_" macros that Apple uses... (expanding the macro for all the ugly ONERRORGOTO that it really is) I really, really should bundle a new packaged version of SDL and GL. --anders
May 19 2006
parent Johan Granberg <lijat.meREM OVEgmail.com> writes:
Anders F Björklund wrote:
 As I wrote somewhere else, one alternative is using the Carbon version 
 for Mac OS X which is easier to initialize since it is using C and not 
 Objecive-C like the Cocoa version. And I guess you also have the X11 ?

Yes but creating a library depending on X11 is probable even worse. X11 is great for porting but new code probably should not use it. or?
 
 I really, really should bundle a new packaged version of SDL and GL.
 
 --anders

That would bee appreciated.
May 20 2006
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Tue, 16 May 2006 20:02:51 +0200, 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).
 
 (The problem i try to solve is working around the SDLmain hack to 
 initialize SDL in the module constructor of my library so that when main 
 is called the SDL will already bee initialized)

I don't understand what is the effective difference between what you want and ... //file.d void main(char[][] args) { someFunk(args); runProgram(); } //end of file Why is it essential that the initialization be performed by the module ctor rather than the first function called by main()? I can understand your desire from a stylistic POV but not from a purely run-time operational POV. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 17/05/2006 10:10:06 AM
May 16 2006
parent Johan Granberg <lijat.meREM OVEgmail.com> writes:
Derek Parnell wrote:
 I don't understand what is the effective difference between what you want
 and ...
 
  //file.d
  void main(char[][] args)
  {
  	someFunk(args);
  	runProgram();
  }
  //end of file
 
 Why is it essential that the initialization be performed by the module ctor
 rather than the first function called by main()?
 
 I can understand your desire from a stylistic POV but not from a purely
 run-time operational POV.
 

It would not matter and I would not bother if it was a program I was developing. But because it is a library I'm developing it would bee an unclean API interface and a source of problems when users forget to add the InitLibrary function at the first line of main and get unpredictable errors. Anyway my initial post was mostly a question about the possibility of such a mechanism. I will probably use a init function and require it to bee at the firs line of main.
May 19 2006