parseArgs

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

This function takes a reference to an already existing instance of the destination class or struct if manual construction is needed prior to parsing.

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)
  2. void parseArgs(string[] args, T instance, OptionFormatter formatter)
    void
    parseArgs
    (
    T
    )
    (
    ref string[] args
    ,
    ref T instance
    ,)
    if (
    is(T == class) ||
    is(T == struct)
    )

Parameters

args string[]

The array of arguments provided in main().

instance T

The instance that will contain parsed values.

formatter OptionFormatter

Optional option formatter.

Throws

std.getopt.GetoptException if parsing fails.

Examples

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

    string optional;

    this( string optional )
    {
        this.optional = optional;
    }
}

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

    auto parsed = MyOptions( "some default value" );
    args.parseArgs( parsed );
    writelfn( "optional value: %s", parsed.optional );
}

Meta

Authors

Tony J. Hudgins