mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-10 04:43:33 +00:00
refactor(esp_console): improved error handling, added tests and docs
* Updated documentation of esp_console_register_help_command * Improved help command error handling * Added test for the help command
This commit is contained in:
@@ -220,6 +220,26 @@ static struct {
|
||||
struct arg_end *end;
|
||||
} help_args;
|
||||
|
||||
static void print_arg_help(cmd_item_t *it)
|
||||
{
|
||||
/* First line: command name and hint
|
||||
* Pad all the hints to the same column
|
||||
*/
|
||||
const char *hint = (it->hint) ? it->hint : "";
|
||||
printf("%-s %s\n", it->command, hint);
|
||||
/* Second line: print help.
|
||||
* Argtable has a nice helper function for this which does line
|
||||
* wrapping.
|
||||
*/
|
||||
printf(" "); // arg_print_formatted does not indent the first line
|
||||
arg_print_formatted(stdout, 2, 78, it->help);
|
||||
/* Finally, print the list of arguments */
|
||||
if (it->argtable) {
|
||||
arg_print_glossary(stdout, (void **) it->argtable, " %12s %s\n");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static int help_command(int argc, char **argv)
|
||||
{
|
||||
int nerrors = arg_parse(argc, argv, (void **) &help_args);
|
||||
@@ -230,34 +250,40 @@ static int help_command(int argc, char **argv)
|
||||
}
|
||||
|
||||
cmd_item_t *it;
|
||||
int ret_value = 1;
|
||||
|
||||
/* Print summary of each command */
|
||||
SLIST_FOREACH(it, &s_cmd_list, next) {
|
||||
if (it->help == NULL) {
|
||||
continue;
|
||||
if (help_args.help_cmd->count == 0) {
|
||||
/* Print summary of each command */
|
||||
SLIST_FOREACH(it, &s_cmd_list, next) {
|
||||
if (it->help == NULL) {
|
||||
continue;
|
||||
}
|
||||
print_arg_help(it);
|
||||
}
|
||||
if (strlen(help_args.help_cmd->sval[0]) > 0 &&
|
||||
strcmp(help_args.help_cmd->sval[0], it->command) != 0) {
|
||||
continue;
|
||||
ret_value = 0;
|
||||
} else {
|
||||
/* Print summary of given command */
|
||||
bool found_command = false;
|
||||
SLIST_FOREACH(it, &s_cmd_list, next) {
|
||||
if (it->help == NULL) {
|
||||
continue;
|
||||
}
|
||||
if (strcmp(help_args.help_cmd->sval[0], it->command) == 0) {
|
||||
print_arg_help(it);
|
||||
found_command = true;
|
||||
ret_value = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* First line: command name and hint
|
||||
* Pad all the hints to the same column
|
||||
*/
|
||||
const char *hint = (it->hint) ? it->hint : "";
|
||||
printf("%-s %s\n", it->command, hint);
|
||||
/* Second line: print help.
|
||||
* Argtable has a nice helper function for this which does line
|
||||
* wrapping.
|
||||
*/
|
||||
printf(" "); // arg_print_formatted does not indent the first line
|
||||
arg_print_formatted(stdout, 2, 78, it->help);
|
||||
/* Finally, print the list of arguments */
|
||||
if (it->argtable) {
|
||||
arg_print_glossary(stdout, (void **) it->argtable, " %12s %s\n");
|
||||
|
||||
/* If given command has not been found, print error message*/
|
||||
if (!found_command) {
|
||||
printf("help: Unrecognized option '%s'. Please use correct command as argument "
|
||||
"or type 'help' only to print help for all commands\n", help_args.help_cmd->sval[0]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
|
||||
return ret_value;
|
||||
}
|
||||
|
||||
esp_err_t esp_console_register_help_command(void)
|
||||
@@ -267,7 +293,8 @@ esp_err_t esp_console_register_help_command(void)
|
||||
|
||||
esp_console_cmd_t command = {
|
||||
.command = "help",
|
||||
.help = "Print the list of registered commands",
|
||||
.help = "Print the summary of all registered commands if no arguments "
|
||||
"are given, otherwise print summary of given command.",
|
||||
.func = &help_command,
|
||||
.argtable = &help_args
|
||||
};
|
||||
|
Reference in New Issue
Block a user