Command Line Arguments in D Programming Language

Command-line arguments are a common way to parameterize execution of programs. For example, dmd hello.d uses hello.d as an argument to the dmd program.

import std.stdio;
import std.array;

void main(string[] args)
{
    // args provides access to raw command-line arguments.
    // Note that the first value in this array is the program name,
    // and args[1..$] holds the arguments to the program.
    string[] argsWithProg = args;
    string[] argsWithoutProg = args[1..$];

    // You can get individual args with normal indexing.
    string arg = args[3];

    writeln(argsWithProg);
    writeln(argsWithoutProg);
    writeln(arg);
}

To experiment with command-line arguments it’s best to compile the program first.

$ dmd command_line_arguments.d
$ ./command_line_arguments a b c d
["./command_line_arguments", "a", "b", "c", "d"]
["a", "b", "c", "d"]
c

Next we’ll look at more advanced command-line processing with flags.