Example application menu entry API.
More...
#include <common.h>
|
virtual const std::string & | key () const =0 |
| Get the command key. More...
|
|
virtual const std::string | usage (bool format=false) const =0 |
| Get the command help documentation. More...
|
|
virtual void | run (std::vector< std::string > &args)=0 |
| Execute the command code. More...
|
|
|
static void | add (ICommand *command) |
| Add a command to the registry. More...
|
|
static void | remove (ICommand *command) |
| Remove a command to the registry. More...
|
|
static void | run (std::string &cmd, std::vector< std::string > &args) |
| Find and run the command with the given key , using the arguments in args . More...
|
|
static std::ostream & | help (std::ostream &stream) |
| Generate the help screen, based on the commands in the registry. More...
|
|
Example application menu entry API.
Definition at line 37 of file apps/common.h.
◆ add()
void ICommand::add |
( |
ICommand * |
command | ) |
|
|
static |
Add a command to the registry.
- Parameters
-
[in] | command | command to be added to the registry. |
Definition at line 47 of file common.cpp.
References key(), and registry.
49 registry.insert(std::pair<std::string, ICommand *>(command->
key(), command));
static std::map< std::string, ICommand * > registry
Command registry.
virtual const std::string & key() const =0
Get the command key.
◆ help()
std::ostream & ICommand::help |
( |
std::ostream & |
stream | ) |
|
|
static |
Generate the help screen, based on the commands in the registry.
- Parameters
-
[in] | stream | reference to the stream to print the help screen. |
- Returns
- reference to the stream.
Definition at line 71 of file common.cpp.
References registry.
79 static std::string cache =
"";
80 static uint8_t cache_count = 0;
88 std::stringstream stream;
91 std::vector<entry> entries;
95 [&entries, &size](std::pair <std::string, ICommand *> e)
100 const std::string &raw = e.second->usage ();
102 char *temp =
new char[raw.size ()+1];
104 strncpy (temp, raw.c_str (), raw.size ())[raw.size ()] = 0;
106 std::vector <char *> lines;
108 char *saveptr = NULL;
109 for (
char *p = strtok_r (temp,
"\n", &saveptr); p != NULL; p = strtok_r (NULL,
"\n", &saveptr))
114 std::for_each (lines.begin (), lines.end (), [&entries, &size](
char *line)
118 char *saveptr = NULL;
119 char *p = strtok_r (line,
":", &saveptr);
120 e.cmd = std::string (p);
122 p = strtok_r (NULL,
":", &saveptr);
123 e.help = std::string (p);
125 entries.push_back (e);
127 if (size < e.cmd.size ())
129 size = e.cmd.size ();
141 stream <<
"================================================" << std::endl;
142 stream <<
"HAN-FUN Example Application : v" << HF_VERSION << std::endl;
143 stream <<
"================================================" << std::endl << std::endl;
145 stream << std::setfill(
' ');
147 std::for_each (entries.begin (), entries.end (), [&stream, size](entry &e)
149 stream << std::left << std::setw (size) << e.cmd <<
"\t: " << e.help << std::endl;
153 stream << std::left << std::setw(size) <<
"h/?" <<
"\t: " <<
"this help menu." 154 << std::endl << std::endl;
155 stream <<
"Select an Option (Q/q to exit): " << std::endl;
157 cache = stream.str();
static std::map< std::string, ICommand * > registry
Command registry.
static std::ostream & help(std::ostream &stream)
Generate the help screen, based on the commands in the registry.
◆ key()
virtual const std::string& ICommand::key |
( |
| ) |
const |
|
pure virtual |
Get the command key.
- Returns
- the string that represents the command key.
Implemented in Command.
Referenced by add(), remove(), and run().
◆ remove()
void ICommand::remove |
( |
ICommand * |
command | ) |
|
|
static |
Remove a command to the registry.
- Parameters
-
[in] | command | command to be removed from the registry. |
Definition at line 59 of file common.cpp.
References key(), and registry.
static std::map< std::string, ICommand * > registry
Command registry.
virtual const std::string & key() const =0
Get the command key.
◆ run() [1/2]
virtual void ICommand::run |
( |
std::vector< std::string > & |
args | ) |
|
|
pure virtual |
Execute the command code.
- Parameters
-
[in] | args | the arguments passed to the code. |
◆ run() [2/2]
void ICommand::run |
( |
std::string & |
cmd, |
|
|
std::vector< std::string > & |
args |
|
) |
| |
|
static |
Find and run the command with the given key
, using the arguments in args
.
- Parameters
-
[in] | cmd | the key for the command to be run. |
[in] | args | vector containing the arguments to call the command with. |
Definition at line 171 of file common.cpp.
References key(), LOG, and registry.
177 LOG(ERROR) <<
"Command '" <<
key <<
"' not found !" <<
NL;
182 it->second->run(args);
static std::map< std::string, ICommand * > registry
Command registry.
#define NL
Helper define for new-line and stream clear.
virtual const std::string & key() const =0
Get the command key.
#define LOG(X)
Log messages with the level given by X.
static std::ostream & help(std::ostream &stream)
Generate the help screen, based on the commands in the registry.
◆ usage()
virtual const std::string ICommand::usage |
( |
bool |
format = false | ) |
const |
|
pure virtual |
Get the command help documentation.
- Parameters
-
[in] | format | if true the usage string SHOULD be formatted to be displayed to the user. |
- Returns
- command help documentation.
Implemented in Command.
The documentation for this struct was generated from the following files: