From 07a9137fd769f0da2eda5936cc3e3db3232e8166 Mon Sep 17 00:00:00 2001 From: Alon Bar-Lev Date: Sat, 4 Nov 2023 21:06:45 +0200 Subject: [PATCH] change(console): print sorted help console commands may be registered in random order in application, for example each module registers its own commands. the output of help is displayed to human, best to have consistent sorted output so that the implementation ordering will not affect the output and allow the user to see a list in some logic order. Signed-off-by: Alon Bar-Lev --- components/console/commands.c | 13 ++++++++----- .../console/test_apps/console/main/test_console.c | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/components/console/commands.c b/components/console/commands.c index 5e63db87b4..a95be846b6 100644 --- a/components/console/commands.c +++ b/components/console/commands.c @@ -130,14 +130,17 @@ esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd) } item->argtable = cmd->argtable; item->func = cmd->func; - cmd_item_t *last = SLIST_FIRST(&s_cmd_list); + cmd_item_t *last = NULL; + cmd_item_t *it; + SLIST_FOREACH(it, &s_cmd_list, next) { + if (strcmp(it->command, item->command) > 0) { + break; + } + last = it; + } if (last == NULL) { SLIST_INSERT_HEAD(&s_cmd_list, item, next); } else { - cmd_item_t *it; - while ((it = SLIST_NEXT(last, next)) != NULL) { - last = it; - } SLIST_INSERT_AFTER(last, item, next); } return ESP_OK; diff --git a/components/console/test_apps/console/main/test_console.c b/components/console/test_apps/console/main/test_console.c index db7685027a..2e5a28cbd5 100644 --- a/components/console/test_apps/console/main/test_console.c +++ b/components/console/test_apps/console/main/test_console.c @@ -78,6 +78,21 @@ TEST_CASE("esp console help command", "[console][ignore]") TEST_ESP_OK(esp_console_cmd_register(&s_quit_cmd)); TEST_ESP_OK(esp_console_register_help_command()); + const esp_console_cmd_t cmd_a = { + .command = "aaa", + .help = "should appear first in help", + .hint = NULL, + .func = do_hello_cmd, + }; + const esp_console_cmd_t cmd_z = { + .command = "zzz", + .help = "should appear last in help", + .hint = NULL, + .func = do_hello_cmd, + }; + TEST_ESP_OK(esp_console_cmd_register(&cmd_z)); + TEST_ESP_OK(esp_console_cmd_register(&cmd_a)); + TEST_ESP_OK(esp_console_start_repl(s_repl)); vTaskDelay(pdMS_TO_TICKS(5000)); }