digitalmars.D.learn - Setting default values for Main function's args Array
- Vaidas (4/15) Jun 27 2019 Is it possible to set the default values for the Main function's
- Adam D. Ruppe (10/12) Jun 27 2019 No, as far as the language is concerned, a value is always being
- wjoe (16/31) Jun 27 2019 consider this:
- Paul Backus (12/27) Jun 27 2019 Your main function is receiving an empty array as an argument,
- Paul Backus (4/12) Jun 27 2019 Correction: you should check for `args.length == 1`, since (as
- Vaidas (6/30) Jun 27 2019 I'm feeling, that overwriting the zero argument that is
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.dcore.exception.RangeError newfile.d(4): Range violation---------------- ??:? _d_arrayboundsp [0x555f5b79f8e9] ??:? _Dmain [0x555f5b79e7ee]
Jun 27 2019
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.
Jun 27 2019
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.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"]import std.stdio : writeln; void main(string[] args = ["asdsfasdf", "asdklfajsdk", "asdfasdfasd"]){ writeln("", args[1]); }Output:vaidas vaidas-SATELLITE-L855:~/Desktop$ rdmd newfile.dcore.exception.RangeError newfile.d(4): Range violation---------------- ??:? _d_arrayboundsp [0x555f5b79f8e9] ??:? _Dmain [0x555f5b79e7ee]
Jun 27 2019
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.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... }import std.stdio : writeln; void main(string[] args = ["asdsfasdf", "asdklfajsdk", "asdfasdfasd"]){ writeln("", args[1]); }Output:vaidas vaidas-SATELLITE-L855:~/Desktop$ rdmd newfile.dcore.exception.RangeError newfile.d(4): Range violation---------------- ??:? _d_arrayboundsp [0x555f5b79f8e9] ??:? _Dmain [0x555f5b79e7ee]
Jun 27 2019
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].
Jun 27 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: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.dvoid 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].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"]
Jun 27 2019