CommandLineHandler

Handles the Command Line options and commands.

Members

Functions

commandNames
string[] commandNames()

Create the list of all supported commands

getCommand
Command getCommand(string name)

Get an instance of the requested command.

prepareCommand
Command prepareCommand(string name, CommandArgs args)

Get an instance of the requested command after the args are sent.

prepareOptions
string[] prepareOptions(CommandArgs args)

Parses the general options and sets up the log level and the root_path

Variables

commandGroups
CommandGroup[] commandGroups;

The list of commands that can be handled

options
CommonOptions options;

General options parser

Examples

Can get the command names

CommandLineHandler handler;
handler.commandGroups = getCommands();

assert(handler.commandNames == ["init", "run", "build", "test", "lint", "generate",
	"describe", "clean", "dustmite", "fetch", "add", "remove",
	"upgrade", "add-path", "remove-path", "add-local", "remove-local", "list", "search",
	"add-override", "remove-override", "list-overrides", "clean-caches", "convert"]);

It sets the cwd as root_path by default

CommandLineHandler handler;

auto args = new CommandArgs([]);
handler.prepareOptions(args);
assert(handler.options.root_path == std.file.getcwd());

It can set a custom root_path

CommandLineHandler handler;

auto args = new CommandArgs(["--root=/tmp/test"]);
handler.prepareOptions(args);
assert(handler.options.root_path == "/tmp/test".absolutePath.buildNormalizedPath);

args = new CommandArgs(["--root=./test"]);
handler.prepareOptions(args);
assert(handler.options.root_path == "./test".absolutePath.buildNormalizedPath);

It sets the info log level by default

scope(exit) setLogLevel(LogLevel.info);
CommandLineHandler handler;

auto args = new CommandArgs([]);
handler.prepareOptions(args);
assert(getLogLevel() == LogLevel.info);

It can set a custom error level

scope(exit) setLogLevel(LogLevel.info);
CommandLineHandler handler;

auto args = new CommandArgs(["--vverbose"]);
handler.prepareOptions(args);
assert(getLogLevel() == LogLevel.debug_);

handler = CommandLineHandler();
args = new CommandArgs(["--verbose"]);
handler.prepareOptions(args);
assert(getLogLevel() == LogLevel.diagnostic);

handler = CommandLineHandler();
args = new CommandArgs(["--vquiet"]);
handler.prepareOptions(args);
assert(getLogLevel() == LogLevel.none);

handler = CommandLineHandler();
args = new CommandArgs(["--quiet"]);
handler.prepareOptions(args);
assert(getLogLevel() == LogLevel.warn);

handler = CommandLineHandler();
args = new CommandArgs(["--verror"]);
handler.prepareOptions(args);
assert(getLogLevel() == LogLevel.error);

It returns the run command by default

CommandLineHandler handler;
handler.commandGroups = getCommands();
assert(handler.getCommand("").name == "run");

It returns the help command when there is none set and the --help arg was set

CommandLineHandler handler;
auto args = new CommandArgs(["--help"]);
handler.prepareOptions(args);
handler.commandGroups = getCommands();
assert(cast(HelpCommand)handler.getCommand("") !is null);

It returns the help command when the help command is sent

CommandLineHandler handler;
handler.commandGroups = getCommands();
assert(cast(HelpCommand) handler.getCommand("help") !is null);

It returns the init command when the init command is sent

CommandLineHandler handler;
handler.commandGroups = getCommands();
assert(handler.getCommand("init").name == "init");

It returns null when a missing command is sent

CommandLineHandler handler;
handler.commandGroups = getCommands();
assert(handler.getCommand("missing") is null);

Meta