Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 16, 2006 program arguments in module constructor? | ||||
---|---|---|---|---|
| ||||
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 Re: program arguments in module constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Granberg | 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 Re: program arguments in module constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Granberg | 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 Re: program arguments in module constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | 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: If the solution is modding the standard library i might just live with 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 I agree that incompatibilities between ares an phobos is a bad thing so if this should bee implemented Walter would have to do it. |
May 16, 2006 Re: program arguments in module constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Nicholson-Sauls | 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 17, 2006 Re: program arguments in module constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Granberg | 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 19, 2006 Re: program arguments in module constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede | 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 Re: program arguments in module constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | 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 Re: program arguments in module constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Granberg | 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 Re: program arguments in module constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Anders F Björklund wrote:
> That's what I did.
> --anders
I saw that :) (I'm using your SDL bindings)
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)
|
Copyright © 1999-2021 by the D Language Foundation