std.getopt

Processing of command line options.

The getopt module implements a getopt function, which adheres to the POSIX syntax for command line options. GNU extensions are supported in the form of long options introduced by a double dash ("--"). Support for bundling of command line options, as was the case with the more traditional single-letter approach, is provided but not enabled by default.

Types 5

classGetOptException : Exception

Thrown on one of the following conditions:

  • An unrecognized command-line argument is passed, and

    std.getopt.config.passThrough was not present.

  • A command-line option was not found, and

    std.getopt.config.required was present.

  • A callback option is missing a value.
enumconfig

Configuration options for getopt.

You can pass them to getopt in any position, except in between an option string and its bound pointer.

caseSensitiveTurn case sensitivity on
caseInsensitiveTurn case sensitivity off (default)
bundlingTurn bundling on
noBundlingTurn bundling off (default)
passThroughPass unrecognized arguments through
noPassThroughSignal unrecognized arguments as errors (default)
stopOnFirstNonOptionStop at first argument that does not look like an option
keepEndOfOptionsDo not erase the endOfOptions separator from args
requiredMake the next option a required option

The result of the getopt function.

helpWanted is set if the option --help or -h was passed to the option parser.

Fields
bool helpWanted
Option[] options
structOption

Information about an option.

Fields
string optShort
string optLong
string help
bool required
private structconfiguration

Functions 10

fnGetoptResult getopt(T...)(ref string[] args, T opts)Parse and remove command line options from a string array.
private fnOption splitAndGet(string opt) pure @trusted nothrow
private fnstring optionValidatorErrorFormat(string msg, size_t idx)
private fnauto getoptTo(R)(string option, string value, size_t idx, string file = __FILE__, size_t line = __LINE__)
private fnvoid getoptImpl(T...)(ref string[] args, ref configuration cfg, ref GetoptResult rslt, ref GetOptException excep, void[][string] visitedLongOpts, void[][string] visitedShortOpts, T opts)
private fnbool handleOption(R)(string option, R receiver, ref string[] args, ref configuration cfg, bool incremental)
private fnbool optMatch(string arg, scope string optPattern, ref string value, configuration cfg) @safe
private fnvoid setConfig(ref configuration cfg, config option) @safe pure nothrow @nogc
fnvoid defaultGetoptPrinter(string text, Option[] opt) @safeThis function prints the passed `Option`s and text in an aligned manner on `stdout`.
fnvoid defaultGetoptFormatter(Output)(Output output, string text, Option[] opt, string style = "%*s %*s%*s%s\n")This function writes the passed text and `Option` into an output range in the manner described in the documentation of function `defaultGetoptPrinter`, unless the style option is used.

Variables 5

vardchar optionChar

The option character (default '-').

Defaults to '-' but it can be assigned to prior to calling getopt.

varstring endOfOptions

The string that conventionally marks the end of all options (default '--').

Defaults to "--" but can be assigned to prior to calling getopt. Assigning an empty string to endOfOptions effectively disables it.

vardchar assignChar

The assignment character used in options with parameters (default '=').

Defaults to '=' but can be assigned to prior to calling getopt.

varstring arraySep

When set to "", parameters to array and associative array receivers are treated as an individual argument. That is, only one argument is appended or inserted per appearance of the option switch. If arraySep is set to something else, then each parameter is first split by the separator, and the individual pieces are treated as arguments to the same option.

Defaults to "" but can be assigned to prior to calling getopt.

private enumvarautoIncrementChar = '+'

Templates 1

tmploptionValidator(A...)
Functions
auto validator()