handleArguments

Handles program arguments.

This is a new generation (as for Spring 2018...), faster but simpler, getopt-like function, based on UDA and that contains only the code needed to handle the possible arguments, since it is heavily based on the D template meta-programming techniques. The possible arguments are defined at compile time.

bool
handleArguments
(
Flag!"Throw" mustThrow
Locations...
)
(
string[] args
)

Parameters

mustThrow

Indicates wether exceptions are gagged (or if the code to throw them is generated). When set to Throw.No, the function is callable by a @nothrow caller. Success is always indicated in the result.

Locations

Indicates the modules, structs or classes containing the @Argument variables. When declared in an aggregate, the variables must be static.

args
Type: string[]

The arguments passed to main(string[] args), excluding the program name.

Return Value

Type: bool

true if Locations requirements were encountered, false otherwise.

Throws

Only if mustThrow, a ConvException if one of the value passed in an argument does not convert to the matching target type or a simple Exception if one of the Argument flag is not verified (such as ArgFlag.mandatory).

Examples

1 // the definition container does not have to be dedicated to the sole purpose
2 // of hosting the options and several containers are allowed.
3 struct ArgumentDefinition
4 {
5     // simple bool argument.
6     static @Argument("-verbose", "as much blabla as possible...") bool v;
7     // an int, requiring to use "=".
8     static @Argument("--numThreads", "compute in parallel") int n;
9     // a stopper flag: it shortcuts arguments processing if found.
10     static @Argument("--update", "", ArgFlags(ArgFlag.stopper)) bool u;
11     // bool function, works without pointer.
12     static @Argument("--help") void help() @safe {writeln("RTFM");}
13 }
14 // a @safe argument checking...
15 assert(handleArguments!(No.Throw, ArgumentDefinition)(["-verbose", "--numThreads=8"]));
16 // error reporting is then a simple bool,
17 // which is however enough to display the help.
18 if (!handleArguments!(No.Throw, ArgumentDefinition)(["-verbose", "--numThreads=whoops"]))
19     writeln(help!ArgumentDefinition);

Meta