Setting default values for Main function's args Array

Vaidas vaidas.boqsc at gmail.com
Thu Jun 27 17:40:26 UTC 2019


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 at vaidas-SATELLITE-L855:~/Desktop$ rdmd program.d
>["/tmp/.rdmd-1000/rdmd-program.d-7E2D9881B29D67DB2D97D001FFD2817D/program", "default", "arguments"]



More information about the Digitalmars-d-learn mailing list