Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 27, 2019 Setting default values for Main function's args Array | ||||
---|---|---|---|---|
| ||||
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] |
June 27, 2019 Re: Setting default values for Main function's args Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vaidas | On Thursday, 27 June 2019 at 17:05:05 UTC, Vaidas wrote:
> Is it possible to set the default values for the Main function's arguments?
No, as far as the language is concerned, a value is always being passed from the operating system, so those default values would never trigger.
What you could do though is just check inside main:
void main(string[] args) {
if(args.length <= 1) args = ["defaults", "here"];
}
Keep in mind that args[0] is almost always set to the name of the executable, so length == 0 is liekly never going to happen.
|
June 27, 2019 Re: Setting default values for Main function's args Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vaidas | On Thursday, 27 June 2019 at 17:05:05 UTC, Vaidas wrote:
> 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]
consider this:
module d-program;
void main(string[] args)
{
import std.stdio;
writeln("args = ", args);
}
---
~/> ./d-program abc def
Output:
args = ["~/d-program", "abc", "def"]
---
~/> ./d-program
Output:
args = ["~/d-program"]
|
June 27, 2019 Re: Setting default values for Main function's args Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vaidas | On Thursday, 27 June 2019 at 17:05:05 UTC, Vaidas wrote:
> 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]
Your main function is receiving an empty array as an argument, which overrides the default argument.
The correct way to do what you want to do is this:
void main(string[] args)
{
string[] defaultArgs = ["my", "default", "arguments"];
if (args.length == 0) {
args = defaultArgs;
}
// Process args...
}
|
June 27, 2019 Re: Setting default values for Main function's args Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | 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].
|
June 27, 2019 Re: Setting default values for Main function's args Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | 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"] |
Copyright © 1999-2021 by the D Language Foundation