utest/msh: supports autocomplete of utest cases for utest_run by kurisaW · Pull Request #10701 · RT-Thread/rt-thread

@unicornx 对于第一个问题,我看了下RT-Thread Finsh组件似乎并不支持这种两个函数的回调吧,因为是要支持执行utest_run这个函数时自动补齐(这里执行msh_utest_list_complete()),我注意到msh是支持msh_auto_complete的,但是像这块各模块也是单拎出来去实现对应的自动补齐逻辑,例如posix file:msh_auto_complete_path

如果要支持自动补齐,目前来看最好的方案就是集成在msh中了,也就是我目前的方案

如果有可行的方案,能否给出更加具体的说明及参考?

这么修改是有些麻烦了,这个PR实现了针对MSH的自动补全子选项特性。可以使用这类接口去实现此功能,我这边针对utest.c稍做修改实现了补全功能,供作者参考:
@unicornx 汪老师也可以看看这样是否更合理一些

+#define MAX_UTEST_OPTIONS 128  /* 支持更多测试用例,可根据需要调整 */
+static struct msh_cmd_opt utest_testcase_run_msh_options[MAX_UTEST_OPTIONS];
+
+static void utest_build_options(void)
+{
+    rt_size_t i;
+    rt_size_t option_index = 0;
+
+    if (tc_num >= MAX_UTEST_OPTIONS - 1)
+    {
+        LOG_W("Too many test cases (%d), only first %d will have completion", tc_num, MAX_UTEST_OPTIONS - 1);
+    }
+
+    rt_memset(utest_testcase_run_msh_options, 0, sizeof(utest_testcase_run_msh_options));
+
+    rt_size_t max_cases = (tc_num < MAX_UTEST_OPTIONS - 1) ? tc_num : MAX_UTEST_OPTIONS - 1;       
+    for (i = 0; i < max_cases; i++)
+    {
+        utest_testcase_run_msh_options[option_index].id = i + 1;
+        utest_testcase_run_msh_options[option_index].name = tc_table[i].name;
+        utest_testcase_run_msh_options[option_index].des = RT_NULL;
+        option_index++;
+    }
+
+    utest_testcase_run_msh_options[option_index].id = 0;
+    utest_testcase_run_msh_options[option_index].name = RT_NULL;
+    utest_testcase_run_msh_options[option_index].des = RT_NULL;
+}
+
+static void utest_build_options(void);
+
 int utest_init(void)
 {
     /* initialize the utest commands table.*/
@@ -123,6 +154,9 @@ int utest_init(void)
             LOG_E("no memory, tc_fail_list init failed!");
         }
     }
+
+    utest_build_options();
+
     return tc_num;
 }
 INIT_COMPONENT_EXPORT(utest_init);
@@ -335,6 +369,7 @@ INIT_APP_EXPORT(utest_auto_run);
 int utest_testcase_run(int argc, char** argv)
 {
     static char utest_name[UTEST_NAME_MAX_LEN];
+    int opt_id;
     rt_memset(utest_name, 0x0, sizeof(utest_name));

     tc_loop = 1;
@@ -343,32 +378,47 @@ int utest_testcase_run(int argc, char** argv)
     {
         utest_thread_create(RT_NULL);
     }
-    else if (argc == 2 || argc == 3 || argc == 4)
+    else if (argc >= 2)
     {
-        if (rt_strcmp(argv[1], "-thread") == 0)
+        opt_id = msh_cmd_opt_id_get(argc, argv, (void*)utest_testcase_run_msh_options);
+
+        if (opt_id >= 1 && opt_id <= tc_num)
         {
-            if (argc == 3 || argc == 4)
+            rt_size_t test_index = opt_id - 1;
+            rt_strncpy(utest_name, tc_table[test_index].name, sizeof(utest_name) -1);
+            if (argc == 3)
             {
-                rt_strncpy(utest_name, argv[2], sizeof(utest_name) -1);
-                if (argc == 4)
-                {
-                    tc_loop = atoi(argv[3]);
-                }
+                tc_loop = atoi(argv[2]);
             }
-            utest_thread_create(utest_name);
-        }
-        else if (rt_strcmp(argv[1], "-help") == 0)
-        {
-            utest_help();
+            utest_do_run(utest_name);
         }
         else
         {
-            rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1);
-            if (argc == 3)
+            if (rt_strcmp(argv[1], "-thread") == 0)
             {
-                tc_loop = atoi(argv[2]);
+                if (argc == 3 || argc == 4)
+                {
+                    rt_strncpy(utest_name, argv[2], sizeof(utest_name) -1);
+                    if (argc == 4)
+                    {
+                        tc_loop = atoi(argv[3]);
+                    }
+                }
+                utest_thread_create(utest_name);
+            }
+            else if (rt_strcmp(argv[1], "-help") == 0)
+            {
+                utest_help();
+            }
+            else
+            {
+                rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1);
+                if (argc == 3)
+                {
+                    tc_loop = atoi(argv[2]);
+                }
+                utest_do_run(utest_name);
             }
-            utest_do_run(utest_name);
         }
     }
     else
@@ -379,7 +429,7 @@ int utest_testcase_run(int argc, char** argv)

     return RT_EOK;
 }
-MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [-thread or -help] [testcase name] [loop num]);
+MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [-thread or -help] [testcase name] [loop num], optenable);
image