| |
---|
| | */ |
---|
| | struct CommandLineHandler |
---|
| | { |
---|
| | /// The list of commands that can be handled |
---|
| | CommandGroup[] commands; |
---|
| | CommandGroup[] commandGroups; |
---|
| | |
---|
| | /// General options parser |
---|
| | CommonOptions options; |
---|
| | |
---|
| |
---|
| | Returns the list of the supported command names |
---|
| | */ |
---|
| | string[] commandNames() |
---|
| | { |
---|
| | return commands.map!(g => g.commands.map!(c => c.name).array).join; |
---|
| | return commandGroups.map!(g => g.commands.map!(c => c.name).array).join; |
---|
| | } |
---|
| | |
---|
| | /** Parses the general options and sets up the log level |
---|
| | and the root_path |
---|
| |
---|
| | { |
---|
| | name = "run"; |
---|
| | } |
---|
| | |
---|
| | foreach (grp; commands) |
---|
| | foreach (grp; commandGroups) |
---|
| | foreach (c; grp.commands) |
---|
| | if (c.name == name) { |
---|
| | return c; |
---|
| | } |
---|
| |
---|
| | |
---|
| | /// Can get the command names |
---|
| | unittest { |
---|
| | CommandLineHandler handler; |
---|
| | handler.commands = getCommands(); |
---|
| | handler.commandGroups = getCommands(); |
---|
| | |
---|
| | assert(handler.commandNames == ["init", "run", "build", "test", "lint", "generate", |
---|
| | "describe", "clean", "dustmite", "fetch", "install", "add", "remove", "uninstall", |
---|
| | "upgrade", "add-path", "remove-path", "add-local", "remove-local", "list", "search", |
---|
| |
---|
| | |
---|
| | /// It returns the `run` command by default |
---|
| | unittest { |
---|
| | CommandLineHandler handler; |
---|
| | handler.commands = getCommands(); |
---|
| | handler.commandGroups = getCommands(); |
---|
| | assert(handler.getCommand("").name == "run"); |
---|
| | } |
---|
| | |
---|
| | /// It returns the `help` command when there is none set and the --help arg |
---|
| |
---|
| | unittest { |
---|
| | CommandLineHandler handler; |
---|
| | auto args = new CommandArgs(["--help"]); |
---|
| | handler.prepareOptions(args); |
---|
| | handler.commands = getCommands(); |
---|
| | handler.commandGroups = getCommands(); |
---|
| | assert(cast(HelpCommand)handler.getCommand("") !is null); |
---|
| | } |
---|
| | |
---|
| | /// It returns the `help` command when the `help` command is sent |
---|
| | unittest { |
---|
| | CommandLineHandler handler; |
---|
| | handler.commands = getCommands(); |
---|
| | handler.commandGroups = getCommands(); |
---|
| | assert(cast(HelpCommand) handler.getCommand("help") !is null); |
---|
| | } |
---|
| | |
---|
| | /// It returns the `init` command when the `init` command is sent |
---|
| | unittest { |
---|
| | CommandLineHandler handler; |
---|
| | handler.commands = getCommands(); |
---|
| | handler.commandGroups = getCommands(); |
---|
| | assert(handler.getCommand("init").name == "init"); |
---|
| | } |
---|
| | |
---|
| | /// It returns null when a missing command is sent |
---|
| | unittest { |
---|
| | CommandLineHandler handler; |
---|
| | handler.commands = getCommands(); |
---|
| | handler.commandGroups = getCommands(); |
---|
| | assert(handler.getCommand("missing") is null); |
---|
| | } |
---|
| | |
---|
| | /** Processes the given command line and executes the appropriate actions. |
---|
| |
---|
| | |
---|
| | if (cmd is null) { |
---|
| | logError("Unknown command: %s", command_name_argument.value); |
---|
| | writeln(); |
---|
| | showHelp(handler.commands, common_args); |
---|
| | showHelp(handler.commandGroups, common_args); |
---|
| | return 1; |
---|
| | } |
---|
| | |
---|
| | if (cast(HelpCommand)cmd !is null) { |
---|
| | showHelp(handler.commands, common_args); |
---|
| | showHelp(handler.commandGroups, common_args); |
---|
| | return 0; |
---|
| | } |
---|
| | |
---|
| | if (handler.options.help) { |
---|
| |
---|
| | |