Command Line Flags in D Programming Language

Command-line flags are a common way to specify options for command-line programs. For example, in wc -l the -l is a command-line flag.

D provides a std.getopt module supporting basic command-line flag parsing. We’ll use this module to implement our example command-line program.

import std.stdio;
import std.getopt;

void main(string[] args)
{
    // Basic flag declarations are available for string,
    // integer, and boolean options. Here we declare a
    // string flag `word` with a default value "foo"
    // and a short description.
    string word = "foo";
    int numb = 42;
    bool fork = false;
    string svar = "bar";

    // Once all flags are declared, call `getopt`
    // to execute the command-line parsing.
    auto helpInformation = getopt(
        args,
        "word", "a string", &word,
        "numb", "an int", &numb,
        "fork", "a bool", &fork,
        "svar", "a string var", &svar
    );

    // If the user passed the --help or -h flag, we print the help message.
    if (helpInformation.helpWanted)
    {
        defaultGetoptPrinter("Some information about the program.",
            helpInformation.options);
        return;
    }

    // Here we'll just dump out the parsed options and
    // any trailing positional arguments.
    writeln("word:", word);
    writeln("numb:", numb);
    writeln("fork:", fork);
    writeln("svar:", svar);
    writeln("tail:", args[1 .. $]);
}

To experiment with the command-line flags program it’s best to first compile it and then run the resulting binary directly.

$ dmd command_line_flags.d

Try out the built program by first giving it values for all flags.

$ ./command_line_flags --word=opt --numb=7 --fork --svar=flag
word:opt
numb:7
fork:true
svar:flag
tail:[]

Note that if you omit flags they automatically take their default values.

$ ./command_line_flags --word=opt
word:opt
numb:42
fork:false
svar:bar
tail:[]

Trailing positional arguments can be provided after any flags.

$ ./command_line_flags --word=opt a1 a2 a3
word:opt
...
tail:[a1, a2, a3]

Use --help flag to get automatically generated help text for the command-line program.

$ ./command_line_flags --help
Some information about the program.
--word a string
--numb an int
--fork a bool
--svar a string var
-h --help This help information.

If you provide a flag that wasn’t specified to the getopt function, the program will print an error message and show the help text again.

$ ./command_line_flags --wat
Unrecognized option --wat
Some information about the program.
...

In D, the std.getopt module provides similar functionality to Go’s flag package. The main differences are:

  1. In D, we use getopt function to parse command-line arguments, which takes the arguments array and a series of flag specifications.
  2. D’s getopt automatically handles the --help flag and generates help text.
  3. In D, we pass references to variables that will hold the flag values, rather than getting pointers returned.
  4. D doesn’t separate flag declaration and parsing into distinct steps like Go does.

Despite these differences, the overall concept and usage of command-line flags remain similar between the two languages.