|
iash
v0.5
Library to provide a bash-like shell in an application
|
Class to register and execute Commands in an iash shell. More...
#include <CommandDispatcher.h>
Public Member Functions | |
| CommandDispatcher (iash *parent) | |
| Initializes a CommandDispatcher with the given shell parent. More... | |
| void | registerCommand (Command *cmd) |
| Registers a given Command with this CommandDispatcher instance. More... | |
| template<typename ExtCommand , typename... Args> | |
| void | registerCommand (Args &&... args) |
| Registers the given Command type with this CommandDispatcher instance. More... | |
| Command * | unregisterCommand (const std::string &name) |
| Removes the command with the given name from the registry, and removes any of its registered aliases. More... | |
| const Command * | unregisterCommand (const Command *cmd) |
| Removes the command at the given memory address. More... | |
| int | dispatch (const UserCommand *userCmd) |
| Searches the command and alias registries and calls the appropriate Command. More... | |
Class to register and execute Commands in an iash shell.
iash has an internal instance of this class, there is typically no need to instantiate it manually.
iash does not provide direct access to this API through its own API, but commands can be registered through iash::addCommand.
| CommandDispatcher::CommandDispatcher | ( | iash * | parent | ) |
Initializes a CommandDispatcher with the given shell parent.
| parent | the shell this CommandDispatcher belongs to |
| int CommandDispatcher::dispatch | ( | const UserCommand * | userCmd | ) |
Searches the command and alias registries and calls the appropriate Command.
| userCmd | the user's input command |
| void CommandDispatcher::registerCommand | ( | Command * | cmd | ) |
Registers a given Command with this CommandDispatcher instance.
This will call Command::init to set the parent shell and add the Command's name and aliases to the command registry.
This function requires a dynamically allocated Command, passed like so:
The CommandDispatcher will automatically free the allocated memory, so there is no need to somehow catch the pointer to the allocated memory.
If a Command instance with the same name and type is already registered with this class, this function will not add the new instance. If the classes must be replaced, the old one should be unregistered first.
This is not the recommended method to use when registering commands. CommandDispatcher::registerCommand() should be used instead.
| cmd | a dynamically allocated Command object to register |
|
inline |
Registers the given Command type with this CommandDispatcher instance.
This function will allocate the memory for the Command internally; all the client must do is provide the type:
If a Command instance of the same type is already registered with this class, this function will not add the new instance. If the classes must be replaced, the old one should be unregistered first.
| args | the arguments to pass to the constructor |
| Command* CommandDispatcher::unregisterCommand | ( | const std::string & | name | ) |
Removes the command with the given name from the registry, and removes any of its registered aliases.
Note that, since Commands are typically created with dynamic memory, you must catch the pointer returned by this function and delete it.
| name | the name of the Command to unregister |