全球最实用的IT互联网信息网站!

AI人工智能P2P分享&下载搜索网页发布信息网站地图

当前位置:诺佳网 > 电子/半导体 > 模拟技术 >

分享一个常用的终端菜单系统

时间:2023-01-18 12:43

人气:

作者:admin

标签:   分享  用的  常用的  一个 

导读:在嵌入式开发中,因为只有黑框框的终端,所以在终端输入指令是比较麻烦的,每次都需要重新实现解析字符串。...

嵌入式开发中,因为只有黑框框的终端,所以在终端输入指令是比较麻烦的,每次都需要重新实现解析字符串。

本篇文章分享一个自己常用的一套终端菜单系统。

代码如下:

#include 
#include 
#include 


typedef enum CmdType
{
    CMD1, CMD2, CMD3, CMD4, CMD5, QUIT
}CmdType;


typedef struct CmdList
{
    CmdType type;
    unsigned char info[50];
}CmdList;


CmdList g_cmd_list[] =
{
    {CMD1, "run cmd1"},
    {CMD2, "run cmd2"},
    {CMD3, "run cmd3"},
    {CMD4, "run cmd4"},
    {CMD5, "run cmd5"},
    {QUIT, "to quit"},
};


void printf_cmd_str()
{
    int size = sizeof(g_cmd_list) / sizeof(g_cmd_list[0]);
    printf("support cmd:\\n");
    for (int i = 0; i < size; i++) {
        printf("    .%d-->%s\\n", i, g_cmd_list[i].info);
    }
    printf("eg. you can input \".0\" to run this cmd.\\n");
}


int main(int argc, char *argv[])
{
    int cmd_size = sizeof(g_cmd_list) / sizeof(g_cmd_list[0]);
    while (1) {
        printf_cmd_str();
        char data[20] = {0};
        if (fgets(data, 20, stdin) < 0) {
            printf("fgets error\\n");
            continue;
        }
        if (data[0] == '.') {
            int id = atoi(&data[1]);
            if (id >= cmd_size || id < 0) {
                printf("input err\\n");
                continue;
            }
            int cmd = g_cmd_list[id].type;
            if (cmd == CMD1) {
                printf("run cmd1\\n");
            }
            else if (cmd == CMD2) {
                printf("run cmd2\\n");
            }
            else if (cmd == CMD3) {
                printf("run cmd3\\n");
            }
            else if (cmd == CMD4) {
                printf("run cmd4\\n");
            }
            else if (cmd == CMD5) {
                printf("run cmd5\\n");
            }
            else if (cmd == QUIT) {
                printf("to quit\\n");
                break;
            }
            else {
                printf("this cmd is not supported\\n");
            }
        }
        else {
            printf("input invalid\\n");
        }
    }


    return 0;
}

运行结果如下:

****@****:~/zcl$ ./a.out 
support cmd:
    .0-->run cmd1
    .1-->run cmd2
    .2-->run cmd3
    .3-->run cmd4
    .4-->run cmd5
    .5-->to quit
eg. you can input ".0" to run this cmd.
.0
run cmd1
support cmd:
    .0-->run cmd1
    .1-->run cmd2
    .2-->run cmd3
    .3-->run cmd4
    .4-->run cmd5
    .5-->to quit
eg. you can input ".0" to run this cmd.
.5
to quit
****@****:~/zcl$

审核编辑:刘清

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信