getopt helpWanted

novice2 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 29 15:02:27 PDT 2015


Hello.

Help me please to understand, how to show usage help to user, who 
enter wrong options?
For example, user not provided required filename.
I want to show error message, and program usage help text.
But likely getopt don't provide help text until valid options 
will be parsed.

Reduced code:
///////////////
import std.stdio: writefln;
import std.getopt;

void main (string[] args)
{
   string fname;
   GetoptResult helpInfo;

   try
   {
     helpInfo = getopt(
       args,
       std.getopt.config.required, "file|f", "File name", &fname);

     writefln("Options parsed: fname=%s", fname);
   }
   catch(Exception e)
   {
     writefln("\nERROR: %s", e.msg);
     defaultGetoptPrinter("Program usage:", helpInfo.options);
   }
}
///////////////

Output:
ERROR: Required option file|fwas not supplied
Program usage:


More information about the Digitalmars-d-learn mailing list