On Thursday, 27 June 2019 at 17:22:36 UTC, Paul Backus wrote:
On Thursday, 27 June 2019 at 17:20:37 UTC, Paul Backus wrote:
void main(string[] args)
{
string[] defaultArgs = ["my", "default", "arguments"];
if (args.length == 0) {
args = defaultArgs;
}
// Process args...
}
Correction: you should check for `args.length == 1`, since (as
Adam points out) the name of the program will be passed as
args[0].
I'm feeling, that overwriting the zero argument that is
containing the program's path is mostly never a good idea.
Here, zero argument will not be overwritten.
Program.d
import std.stdio : writeln;
void main(string[] args)
{
string[] defaultArgs = [args[0], "default", "arguments"];
if (args.length == 1) {
args = defaultArgs;
}
// Process args...
writeln("", args);
}
Output:
vaidas@vaidas-SATELLITE-L855:~/Desktop$ rdmd program.d
["/tmp/.rdmd-1000/rdmd-program.d-7E2D9881B29D67DB2D97D001FFD2817D/program", "default",
"arguments"]