Summary: getopt improvements by Igor Lesik
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD

--- Comment #0 from Andrei Alexandrescu <> 2010-02-07 
18:38:17 PST ---
Below is a paste of the page at The
code should be added to std.getopt, perhaps as an overload of the current
function. Thanks Igor! I will add your name to the authors list when I make the


One way to extend Phobos library std.getopt.getopt function is make a wrapper
around it that could be feeded with usage information about every option and
let it automatically gather all usage strings into one usage/help message
block, similarly to Boost program_options library

Below is an example how getoptEx could be used:

import getoptex;
import std.stdio;

void main(string[] args)
    string inputFile, outputFile;

    bool helpPrinted = getoptEx(
        "Test program to demonstrate getoptEx\n"~
        "written by Igor Lesik on Feb 2010\n"~
        "Usage: test1 { --switch }\n",
            "\tinput file name,\n"~
            "\tmust be html file",
            "\toutput file name",
            "\tprint name of\n"~
            "\tthe author",
            delegate() {writeln("Igor Lesik");}

    if (helpPrinted)

    writeln("Input file name:", inputFile);
    writeln("Output file name:", outputFile);
If you call the program with --help option, then output is:

>test1.exe --help

Test program to demonstrate getoptEx
written by Igor Lesik on Feb 2010
Usage: test1 { --switch }

        input file name,
        must be html file
        output file name
        print name of
        the author

        produce help message
getopEx implementation:

module getoptex;

import std.stdio;
import std.getopt;

private import std.contracts;
private import std.typetuple;
private import std.conv;

bool getoptEx(T...)(string helphdr, ref string[] args, T opts)
            "Invalid arguments string passed: program name missing");

    string helpMsg = GetoptHelp(opts); // extract all help strings

    bool helpPrinted = false; // state tells if called with "--help"

    void printHelp()
        writeln("\n", helphdr, "\n", helpMsg,
            "--help", "\n\tproduce help message");
        helpPrinted = true;

    getopt(args, GetoptEx!(opts), "help", &printHelp);

    return helpPrinted;

private template GetoptEx(TList...)
    static if (TList.length)
        static if (is(typeof(TList[0]) : config))
            // it's a configuration flag, lets move on
            alias TypeTuple!(TList[0],GetoptEx!(TList[1 .. $])) GetoptEx;
            // it's an option string, eat help string
            alias TypeTuple!(TList[0],TList[2],GetoptEx!(TList[3 .. $]))
        alias TList GetoptEx;

private string GetoptHelp(T...)(T opts)
    static if (opts.length)
        static if (is(typeof(opts[0]) : config))
            // it's a configuration flag, skip it
            return GetoptHelp(opts[1 .. $]);
            // it's an option string
            string option  = to!(string)(opts[0]);
            string help    = to!(string)(opts[1]);

            return( "--"~option~"\n"~help~"\n"~GetoptHelp(opts[3 .. $]) );
        return to!(string)("\n");
Page generated by Ddoc.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to