CommandArgs

Encapsulates a set of application arguments.

This class serves two purposes. The first is to provide an API for parsing command line arguments (getopt). At the same time it records all calls to getopt and provides a list of all possible options using the recognizedArgs property.

Constructors

this
this(string[] args)

Initializes the list of source arguments.

Members

Functions

dropAllArgs
void dropAllArgs()

Resets the list of available source arguments.

extractAllRemainingArgs
string[] extractAllRemainingArgs()

Returns the list of unprocessed arguments, including the app arguments and resets the list of available source arguments.

extractRemainingArgs
string[] extractRemainingArgs()

Returns the list of unprocessed arguments, ignoring the app arguments, and resets the list of available source arguments.

getopt
void getopt(string names, T* var, string[] help_text, bool hidden)
Undocumented in source. Be warned that the author may not have intended to support it.

Properties

appArgs
string[] appArgs [@property getter]

Returns the list of app args.

hasAppArgs
bool hasAppArgs [@property getter]

Checks if the app arguments are present.

hasAppVersion
bool hasAppVersion [@property getter]

Checks if the --version argument is present on the first position in the list.

recognizedArgs
const(Arg)[] recognizedArgs [@property getter]

Returns the list of all options recognized.

Structs

Arg
struct Arg
Undocumented in source.

Examples

Using CommandArgs

/// It should not find the app version for an empty arg list
assert(new CommandArgs([]).hasAppVersion == false);

/// It should find the app version when `--version` is the first arg
assert(new CommandArgs(["--version"]).hasAppVersion == true);

/// It should not find the app version when `--version` is the second arg
assert(new CommandArgs(["a", "--version"]).hasAppVersion == false);

/// It returns an empty app arg list when `--` arg is missing
assert(new CommandArgs(["1", "2"]).appArgs == []);

/// It returns an empty app arg list when `--` arg is missing
assert(new CommandArgs(["1", "2"]).appArgs == []);

/// It returns app args set after "--"
assert(new CommandArgs(["1", "2", "--", "a"]).appArgs == ["a"]);
assert(new CommandArgs(["1", "2", "--"]).appArgs == []);
assert(new CommandArgs(["--"]).appArgs == []);
assert(new CommandArgs(["--", "a"]).appArgs == ["a"]);

/// It returns the list of all args when no args are processed
assert(new CommandArgs(["1", "2", "--", "a"]).extractAllRemainingArgs == ["1", "2", "--", "a"]);

It removes the extracted args

auto args = new CommandArgs(["-a", "-b", "--", "-c"]);
bool value;
args.getopt("b", &value, [""]);

assert(args.extractAllRemainingArgs == ["-a", "--", "-c"]);

It should not be able to remove app args

auto args = new CommandArgs(["-a", "-b", "--", "-c"]);
bool value;
args.getopt("-c", &value, [""]);

assert(!value);
assert(args.extractAllRemainingArgs == ["-a", "-b", "--", "-c"]);

Meta