parseArgs

Use std.getopt.getopt to parse command-line arguments into a class or struct.

This function automatically invokes the constructor for the class or struct and requires a public, parameterless constructor to work. Does not take opCall() into account for classes.

The caller can optionally provide an OptionFormatter delegate to format options into a prett-printed string to display on the help screen. If no delegate is provided the default std.getopt.defaultGetoptFormatter function will be used as a fallback.

Automatically prints the help text and exits the process when help is requested.

  1. T parseArgs(string[] args, OptionFormatter formatter)
    T
    parseArgs
    (
    T
    )
    (
    ref string[] args
    ,)
    if (
    is(T == class) ||
    is(T == struct)
    )
  2. void parseArgs(string[] args, T instance, OptionFormatter formatter)

Parameters

args string[]

The array of arguments provided in main().

formatter OptionFormatter

Optional option formatter.

Throws

std.getopt.GetoptException if parsing fails.

Examples

struct MyOptions
{
    @ShortName( 'v' )
    bool verbose;
}

void main( string[] args )
{
    import std.stdio;

    auto parsed = args.parseArgs!MyOptions;
    writelfn( "is verbose? %s", parsed.verbose ? "yes" : "no" );
}

Meta

Authors

Tony J. Hudgins