Re: stdlib.exit()
On 2012-08-20 22:04, David wrote: If i interpret it correctly, a call to Runtime.terminate() (as I am doing right now), is the only and correct way? I would think so. -- /Jacob Carlborg
Re: stdlib.exit()
On Mon, 20 Aug 2012 21:03:25 +0100, David d...@dav1d.de wrote: You could define a custom ExitException and throw that, catching it at the top level and returning the error code stored inside it, from main(). Not ideal, but it would work. Thi is really not ideal Didn't I just say that.. :p Not ideal, but it would work. - Regan Heath , since this is for a argument-parser, --help should print the help and then exit. Still, it can be done.. int main(string[] args) { int retval = 0; try { ParseArguments(args); } catch(ExitException e) { // Silent retval = e.retval; } catch(Exception e) { writefln(Error: %s, e...); retval = 1; } return retval; } void ParseArguments(string[] args) { .. parse .. calls CmdHelp(); } void CmdHelp() { writeln(...); throw ExitException(0); } R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Re: stdlib.exit()
I use the following: import std.c.stdlib; exit(0);
Re: stdlib.exit()
Am 20.08.2012 09:15, schrieb Tracey: I use the following: import std.c.stdlib; exit(0); Yeah, but that doesn't terminate the runtime and will not call any dtors, right?
Re: stdlib.exit()
I don't know about the runtime, but destructors are not called (not sure about module destructors) import std.stdio; import std.c.stdlib; void main() { S s; exit(-1); } struct S { ~this() { writeln(destructor); } } This prints nothing, meaning that the destructor wasn't called. But why do you want destructors to be called when you call exit()? You are exiting in an abnormal way anyway.
Re: stdlib.exit()
On Mon, 20 Aug 2012 10:03:56 +0100, David d...@dav1d.de wrote: Am 20.08.2012 09:15, schrieb Tracey: I use the following: import std.c.stdlib; exit(0); Yeah, but that doesn't terminate the runtime and will not call any dtors, right? std.c.stdlib.exit is the C runtime exit function which doesn't know anything about D's runtime, which is layered on top. So, I would not expect any D runtime cleanup to occur. You could define a custom ExitException and throw that, catching it at the top level and returning the error code stored inside it, from main(). Not ideal, but it would work. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Re: stdlib.exit()
Regan Heath: You could define a custom ExitException and throw that, catching it at the top level and returning the error code stored inside it, from main(). Not ideal, but it would work. Seems OK. Another solution is to add a dexit() in Phobos :-) Bye, bearophile
Re: stdlib.exit()
On 2012-08-20 16:27, bearophile wrote: Regan Heath: You could define a custom ExitException and throw that, catching it at the top level and returning the error code stored inside it, from main(). Not ideal, but it would work. Seems OK. Another solution is to add a dexit() in Phobos :-) Or the D runtime could just use the atexit function defined in stdlib.h. Just add a callback which terminates the D runtime. http://pubs.opengroup.org/onlinepubs/009695299/functions/atexit.html -- /Jacob Carlborg
Re: stdlib.exit()
But why do you want destructors to be called when you call exit()? You are exiting in an abnormal way anyway. exit(0) is a normal termination, so destructors should be called. I wanna be nice and deallocate the allocated memory and unload dynamic libraries.
Re: stdlib.exit()
Or the D runtime could just use the atexit function defined in stdlib.h. Just add a callback which terminates the D runtime. http://pubs.opengroup.org/onlinepubs/009695299/functions/atexit.html If i interpret it correctly, a call to Runtime.terminate() (as I am doing right now), is the only and correct way?
Re: stdlib.exit()
You could define a custom ExitException and throw that, catching it at the top level and returning the error code stored inside it, from main(). Not ideal, but it would work. Thi is really not ideal, since this is for a argument-parser, --help should print the help and then exit.
Re: stdlib.exit()
That's what I do: import std.c.stdlib; void main(string[] args) { if( args.length == 1 ) { writeln(Some arguments, please!); exit(-1); } }
Re: stdlib.exit()
On Sunday, 19 August 2012 at 23:48:01 UTC, Minas Mina wrote: That's what I do: import std.c.stdlib; void main(string[] args) { if( args.length == 1 ) { writeln(Some arguments, please!); exit(-1); } } Sorry, forgot to import std.stdio.