CommandProcessor Class Reference

#include <command_handler.hpp>

Public Member Functions

 CommandProcessor (int numCmds=8)
 
void addHandler (CmdHandlerFuncPtr pFunc, const char *pPersistantCmdStr, const char *pPersistentCmdHelpStr=0, void *pDataParam=0)
 
void enableShortCmds (bool en)
 
bool handleCommand (str &cmd, CharDev &out)
 

Detailed Description

Command Processor Class

This class allows users to add a command string and associated handler for commands. When user inputs a command, it will call the mapped handler. Note that command input is capitalized to make this class case insensitive.

One handler is already part of this class:

  • "help" : Get list of supported commands

The "help" handler lists out supported commands, "HELP <command>" will show the help-text associated with the given command.

Example Usage:

CMD_HANDLER_FUNC(cmdHandler)
{
// Process the command ...
output.puts("OK");
}
cp.addHandler(cmdHandler, "cmd", "My Cmd Help");

Constructor & Destructor Documentation

CommandProcessor::CommandProcessor ( int  numCmds = 8)
inline

Constructor

Parameters
numCmdsOptional: Initial list of vector size to avoid memory re-allocation
Note
addHandler() will grow the vector of command handlers if more commands are added later

Member Function Documentation

void CommandProcessor::addHandler ( CmdHandlerFuncPtr  pFunc,
const char *  pPersistantCmdStr,
const char *  pPersistentCmdHelpStr = 0,
void *  pDataParam = 0 
)

Adds a command to the command handler list

Parameters
pFuncThe function pointer of the handler of this command
pPersistantCmdStrThe persistent data pointer of a command's text
pPersistentCmdHelpStrThe persistent data pointer of this command's help text
pDataParamOptional Param: The data parameter pointer to pass to your handler when it gets called
Warning
pPersistentCmdStr and pPersistentCmdHelp must always exist in memory without going out of scope because these strings are not copied internally but their pointer is referenced during comparison
Note
command is matched while ignoring case.
void CommandProcessor::enableShortCmds ( bool  en)
inline

Enables short-hand commands. If a registered command is "information", and a command comes in as "info", then it will be handled by "information" handler. First command that matches first two characters will take precedence.

Note
This option is enabled by default.
bool CommandProcessor::handleCommand ( str cmd,
CharDev out 
)

Command handling functions Handles an incoming command and

Returns
null terminated string of the command output
Parameters
cmdInput command either as char* pointer or str object
Returns
char* pointer containing the command's response
Note
"HELP" command will output the list of supported commands.

If command not matched, try to partially match a command. ie: If command is "magic", match "m" as command

Check here if pCommandStr contains partial command :

  • regCmd may be "thermostat", when input is "th" or "th on"
  • So accept this command as shorthand command

The documentation for this class was generated from the following files: