feat(system/console): add help command verbose level option

Support users to demonstrate command info in different verbose levels.

Closes https://github.com/espressif/esp-idf/issues/9158
This commit is contained in:
Xiaoyu Liu
2024-05-17 18:09:32 +08:00
parent ea2f512cb5
commit 18f6be771d
4 changed files with 176 additions and 4 deletions

View File

@@ -39,6 +39,8 @@ typedef struct cmd_item_ {
SLIST_ENTRY(cmd_item_) next; //!< next command in the list
} cmd_item_t;
typedef void (*const fn_print_arg_t)(cmd_item_t*);
/** linked list of command structures */
static SLIST_HEAD(cmd_list_, cmd_item_) s_cmd_list;
@@ -52,6 +54,8 @@ static char *s_tmp_line_buf;
static const cmd_item_t *find_command_by_name(const char *name);
static esp_console_help_verbose_level_e s_verbose_level = ESP_CONSOLE_HELP_VERBOSE_LEVEL_1;
esp_err_t esp_console_init(const esp_console_config_t *config)
{
if (!config) {
@@ -245,6 +249,7 @@ esp_err_t esp_console_run(const char *cmdline, int *cmd_ret)
static struct {
struct arg_str *help_cmd;
struct arg_int *verbose_level;
struct arg_end *end;
} help_args;
@@ -268,6 +273,17 @@ static void print_arg_help(cmd_item_t *it)
printf("\n");
}
static void print_arg_command(cmd_item_t *it)
{
const char *hint = (it->hint) ? it->hint : "";
printf("%-s %s\n\n", it->command, hint);
}
static fn_print_arg_t print_verbose_level_arr[ESP_CONSOLE_HELP_VERBOSE_LEVEL_MAX_NUM] = {
print_arg_command,
print_arg_help,
};
static int help_command(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void **) &help_args);
@@ -279,18 +295,33 @@ static int help_command(int argc, char **argv)
cmd_item_t *it;
int ret_value = 1;
esp_console_help_verbose_level_e verbose_level;
if (help_args.help_cmd->count == 0) {
/* Print summary of each command */
/* If verbose level is not provided, set it as default value */
if (help_args.verbose_level->count == 0) {
verbose_level = s_verbose_level;
}
/* Else use the verbose level from input argument */
else {
verbose_level = (esp_console_help_verbose_level_e)help_args.verbose_level->ival[0];
}
/* Check if selected verbose level is valid, proceed if yes, else return */
if (verbose_level >= ESP_CONSOLE_HELP_VERBOSE_LEVEL_MAX_NUM) {
printf("help: invalid verbose level %d", (int)verbose_level);
return 1;
}
/* Print info of each command based on verbose level */
SLIST_FOREACH(it, &s_cmd_list, next) {
if (it->help == NULL) {
continue;
}
print_arg_help(it);
print_verbose_level_arr[verbose_level](it);
}
ret_value = 0;
} else {
/* Print summary of given command */
/* Print summary of given command, verbose option will be ignored */
bool found_command = false;
SLIST_FOREACH(it, &s_cmd_list, next) {
if (it->help == NULL) {
@@ -317,7 +348,9 @@ static int help_command(int argc, char **argv)
esp_err_t esp_console_register_help_command(void)
{
help_args.help_cmd = arg_str0(NULL, NULL, "<string>", "Name of command");
help_args.end = arg_end(1);
help_args.verbose_level = arg_intn("v", "verbose", "<0|1>", 0, 1,
"If specified, list console commands with given verbose level");
help_args.end = arg_end(2);
esp_console_cmd_t command = {
.command = "help",
@@ -328,3 +361,12 @@ esp_err_t esp_console_register_help_command(void)
};
return esp_console_cmd_register(&command);
}
esp_err_t esp_console_set_help_verbose_level(esp_console_help_verbose_level_e verbose_level)
{
/* legal verbose level sanity check */
if (verbose_level >= ESP_CONSOLE_HELP_VERBOSE_LEVEL_MAX_NUM) {
return ESP_ERR_INVALID_ARG;
}
s_verbose_level = verbose_level;
return ESP_OK;
}