Skip to content

Commit 6eaf080

Browse files
committed
Builtin help command function for added commands
1 parent 768174b commit 6eaf080

2 files changed

Lines changed: 85 additions & 10 deletions

File tree

src/minbasecli.cpp

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,23 @@ MINBASECLI::MINBASECLI()
5959
{
6060
this->initialized = false;
6161
this->received_bytes = 0;
62+
this->use_builtin_help_cmd = false;
6263
this->num_added_commands = 0;
6364
this->cli_result.argc = 0U;
64-
this->cli_result.argv[0][0] = '\0';
65-
this->cli_result.cmd[0] = '\0';
66-
this->rx_read[0] = '\0';
65+
for (int i = 0; i < MINBASECLI_MAX_ARGV; i++)
66+
{
67+
memset(this->cli_result.argv[i], (int)('\0'),
68+
MINBASECLI_MAX_ARGV_LEN - 1U);
69+
}
70+
memset(this->cli_result.cmd, (int)('\0'), MINBASECLI_MAX_CMD_LEN - 1U);
71+
for (int i = 0; i < MINBASECLI_MAX_CMD_TO_ADD; i++)
72+
{
73+
this->added_commands[i].command[0] = '\0';
74+
this->added_commands[i].description[0] = '\0';
75+
this->added_commands[i].callback = nullptr;
76+
}
77+
memset(this->rx_read, (int)('\0'), MINBASECLI_MAX_READ_SIZE - 1U);
78+
memset(this->print_array, (int)('\0'), MINBASECLI_MAX_PRINT_SIZE - 1U);
6779
}
6880

6981
/*****************************************************************************/
@@ -120,6 +132,14 @@ bool MINBASECLI::add_cmd(const char* command,
120132
cmd_cb_info.description[cmd_description_len] = '\0';
121133
cmd_cb_info.callback = callback;
122134

135+
// Set to use the internal "help" command if it is the first command added
136+
if (num_added_commands == 0U)
137+
use_builtin_help_cmd = true;
138+
139+
// If requested to add a custom "help" command, don't use the builtin one
140+
if (strcmp(command, CMD_HELP) == 0)
141+
use_builtin_help_cmd = false;
142+
123143
// Add the new command to the list of binded commands and increase the
124144
// number of added commands
125145
added_commands[num_added_commands] = cmd_cb_info;
@@ -147,20 +167,30 @@ bool MINBASECLI::run()
147167
if (manage(&cli_result) == false)
148168
return false;
149169

150-
// Check if th command is added in the callback handle list
170+
// Compose array of pointer for arguments
171+
char* ptr_argv[MINBASECLI_MAX_ARGV];
172+
for (int i = 0; i < MINBASECLI_MAX_ARGV; i++)
173+
ptr_argv[i] = cli_result.argv[i];
174+
175+
// If no custom "help" command is set
176+
if (use_builtin_help_cmd)
177+
{
178+
// For "help" command, call the builtin "help" function
179+
if (strcmp(cli_result.cmd, CMD_HELP) == 0U)
180+
{
181+
cmd_help(cli_result.argc, ptr_argv);
182+
return true;
183+
}
184+
}
185+
186+
// Check if the command is added in the callback handle list
151187
for (uint8_t i = 0U; i < num_added_commands; i++)
152188
{
153189
// If command is found in the callbacks list, call to the callback
154190
if (strcmp(cli_result.cmd, added_commands[i].command) == 0U)
155191
{
156-
// Compose array of pointer for arguments
157-
char* ptr_argv[MINBASECLI_MAX_ARGV];
158-
for (int i = 0; i < MINBASECLI_MAX_ARGV; i++)
159-
ptr_argv[i] = cli_result.argv[i];
160-
161192
// Call to command callback
162193
added_commands[i].callback(cli_result.argc, ptr_argv);
163-
164194
cmd_found = true;
165195
break;
166196
}
@@ -343,6 +373,29 @@ void MINBASECLI::printf(const char* fstr, ...)
343373
va_end(lst);
344374
}
345375

376+
/**
377+
* @details
378+
* This function is called when a "help" command is received through the CLI if
379+
* any command has been added to be handled through a callback and the "help"
380+
* command has not been added. It shows the list of commands that are added and
381+
* the descriptions of them.
382+
*/
383+
void MINBASECLI::cmd_help(int argc, char* argv[])
384+
{
385+
// Do nothing if there is no added commands
386+
if (num_added_commands == 0U)
387+
return;
388+
389+
// Shows each added command descriptions
390+
this->printf("Available commands:\n\n");
391+
for (uint8_t i = 0U; i < num_added_commands; i++)
392+
{
393+
this->printf("%s - %s\n", added_commands[i].command,
394+
added_commands[i].description);
395+
}
396+
this->printf("\n");
397+
}
398+
346399
/*****************************************************************************/
347400

348401
/* Private Methods */

src/minbasecli.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,29 @@ class MINBASECLI : public MINBASECLI_HAL
220220
*/
221221
void printf(const char* str, ...);
222222

223+
/**
224+
* @brief Internal builtin "help" command callback.
225+
* @param argc Number of arguments.
226+
* @param argv Pointers array of arguments.
227+
*/
228+
void cmd_help(int argc, char* argv[]);
229+
223230
/*************************************************************************/
224231

225232
/* Private Attributes */
226233

227234
private:
228235

236+
/**
237+
* @brief Builtin command "help" text.
238+
*/
239+
static constexpr char CMD_HELP[] = "help";
240+
241+
/**
242+
* @brief Builtin command "help" description text.
243+
*/
244+
static constexpr char CMD_HELP_DESCRIPTION[] = "Shows current info.\n";
245+
229246
/**
230247
* @brief CLI initialized flag.
231248
*/
@@ -236,6 +253,11 @@ class MINBASECLI : public MINBASECLI_HAL
236253
*/
237254
uint32_t received_bytes;
238255

256+
/**
257+
* @brief Store if the builtin "help" command has been setup.
258+
*/
259+
bool use_builtin_help_cmd;
260+
239261
/**
240262
* @brief Current number of commands added to the CLI through add()
241263
* function.

0 commit comments

Comments
 (0)