Re: Setting default values for Main function's args Array

2019-06-27 Thread Vaidas via Digitalmars-d-learn

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"]




Setting default values for Main function's args Array

2019-06-27 Thread Vaidas via Digitalmars-d-learn
Is it possible to set the default values for the Main function's 
arguments?

It seems that I'm getting Range error.


import std.stdio : writeln;
void main(string[] args = ["asdsfasdf", "asdklfajsdk", 
"asdfasdfasd"]){


   writeln("", args[1]);
}


Output:

vaidas@vaidas-SATELLITE-L855:~/Desktop$ rdmd newfile.d

core.exception.RangeError@newfile.d(4): Range violation


??:? _d_arrayboundsp [0x555f5b79f8e9]
??:? _Dmain [0x555f5b79e7ee]