getopt Basic usage

James Gray test at test.com
Sat Aug 15 04:09:19 UTC 2020


I am trying to use getopt and would not like the program to throw 
an unhandled exception when parsing command line options. Is the 
following, adapted from the first example in the getopt 
documentation, a reasonable approach?


import std.getopt;

string data = "file.dat";
int length = 24;
bool verbose;
enum Color { no, yes };
Color color;

void main(string[] args)
{

  try {
  auto helpInformation = getopt(
    args,
    std.getopt.config.stopOnFirstNonOption,
    "length",  &length,    // numeric
    "file",    &data,      // string
    "verbose", &verbose,   // flag
    "color", "Information about this color", &color);    // enum

   if (helpInformation.helpWanted)
   {
    defaultGetoptPrinter("Some information about the program.",
      helpInformation.options);
   }
  }
  catch(Exception e) {
   import std.stdio : writeln;
   writeln(e.msg, "\nFor more information use --help");
  }
}





More information about the Digitalmars-d-learn mailing list