自学内容网 自学内容网

getopt_long使用详解及实例

getopt_long介绍

getopt_long 是一个在 Linux 和类 Unix 系统中常用的函数,用于解析命令行参数。它支持短选项(如 -a)和长选项(如 --long-option),并且能够处理带有参数的选项。以下是 getopt_long 的详细使用方法

  1. 函数原型
    getopt_long 的函数原型如下:
int getopt_long(int argc, char *const argv[], const char *shortopts,
                const struct option *longopts, int *longindex);

  1. 参数说明
  • argc 和 argv:这两个参数与 main 函数中的参数一致,分别表示命令行参数的数量和参数数组。
  • shortopts:这是一个字符串,表示短选项的格式。例如,“ab:c::” 表示 a 是无参数选项,b 是带必需参数的选项,c 是带可选参数的选项。
  • longopts:这是一个指向 struct option 数组的指针,用于定义长选项的格式。每个 struct option 结构体包含以下字段:
    name:长选项的名称。
  • has_arg:指示选项是否带参数,0 表示无参数,1 表示必需参数,2 表示可选参数。
  • flag:如果为 NULL,则 getopt_long 返回 val;否则,返回 0,并将 val 存储到 flag 指向的变量中。
  • val:将要返回的值或存储到 flag 指向的变量中的值。
  • longindex:这是一个指向整数的指针,用于返回长选项在 longopts 数组中的索引。通常设置为 NULL。
  1. 返回值
    如果遇到有效的短选项或长选项,getopt_long 返回该选项的字符(对于短选项)或 val(对于长选项)。
    如果所有选项都已处理完毕,返回 -1。

  2. 如何得带参选项的参数值
    char *optarg是在getopt.h头文件中声明的指针变量,用于得到每个参数选型的参数值。

getopt_long使用实例

static void show_usage(const char *cmd)
{
    printf("Usage: %s [options] ... \n", cmd);
    printf("  -h, --help           display this help and exit\n");
    printf("  -v, --version        get and show version\n");
     printf(" -r, --reboot         reboot RSU\n");
    printf("  -l, --rxlevel        setting rx level\n");
    printf("  -p, --tpwer          setting tx power\n");
    printf("  -i, --id             setting id mode\n");
    printf("  -c, --idc            setting id change interval times\n");
    printf("  -m, --setmmi         setting SetMMI\n");

    exit(0);
}

int main(int argc, char **argv)
{   
    uint8_t verbuf[200] = {0};
    uint8_t vst_buf[512] = {0};
    uint32_t verlen;

    cmd_55aa_protocol_init();

    char resp[100] = {0};
    int ret;
    int option;
    char *name = NULL;
    char *content = "Hello, World!";

    const char * const short_options = "hvrl:p:i:c:m:";
    const struct option long_options[] = {
        { "help",    0, NULL, 'h' },
        { "version", 0, NULL, 'v' },
        { "rboot",   0, NULL, 'r' },
        { "tpwer",   1, NULL, 'p' },
        { "rxlevel", 1, NULL, 'l' },
        { "idc",     1, NULL, 'c' },
        { "id",      1, NULL, 'i' },
        { "setmmi",  1, NULL, 'm' },
        { NULL,      0, NULL,  0  }
    };

    if (argc < 2)
    {
        show_usage(argv[0]);
    }

    cmd_55aa_t9_init();

    uint8_t txpower = 24;
    uint8_t rxlevel = 16;
    uint8_t rsuidmode = 1;
    uint8_t idchangeinterval = 2;
    uint8_t setmmi = 1;
    uint8_t gettxpower;

     while ((option = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) 
    {
        switch (option)
        {
        case 'h':
            show_usage(argv[0]);
            break;
        case 'v':
            cmd_55aa_t9_get_ver_test(verbuf, sizeof(verbuf), &verlen);
            printf("\r\nversion: %s\r\n", verbuf);
            break;
        case 'p':
            txpower = atoi(optarg);
            if (0 > txpower || txpower > 31)
            {
                printf("txpower %d is invalid, range: 0-31\n", txpower);
                exit(-1);
            }
            printf("txpower %d\n", txpower);
            break;
        case 'l':
            rxlevel = atoi(optarg);
            if (0 > rxlevel || rxlevel > 31)
            {
                printf("rxlevel %d is invalid, range: 0-31.\n", rxlevel);
                exit(-1);
            }
            printf("rxlevel %d\n", rxlevel);
            break;
        case 'c':
            idchangeinterval = atoi(optarg);
            printf("idchangeinterval %d\n", idchangeinterval);
            break;
        case 'i':
            rsuidmode = atoi(optarg);
            printf("rsuidmode %d\n", rsuidmode);
            if (rsuidmode != 0 && rsuidmode != 1)
            {
                printf("rsuidmode %d is invalid, range: 0-1.\n", rsuidmode);
                exit(-1);
            }
            break;
        case 'm':
            setmmi = atoi(optarg);
            printf("setmmi %d\n", setmmi);
            if (setmmi != 0 && setmmi != 1)
            {
                printf("setmmi %d is invalid, range: 0-1.\n", setmmi);
                exit(-1);
            }
            break;
        case 'r':
            break;
        case '?':
        default :
            printf("Error: option invalid\n");
            exit(-1);
            break;
        }
    }
    
    printf("\r\n");
    sleep(1);
    while (1)
    {
        cmd_55aa_t9_txpower_set(txpower);
        cmd_55aa_t9_txpower_get(&gettxpower);
        cmd_55aa_t9_rxlevel_set(rxlevel);
        cmd_55aa_t9_id_changeinterval_set(idchangeinterval);

        cmd_55aa_t9_rsuid_mode_set(rsuidmode);
        sleep(1);
        cmd_55aa_t9_setmmi(setmmi);
   
        cmd_55aa_antenna_open();

        sleep(5);

        cmd_55aa_antenna_close();
    }

    cmd_55aa_protocol_deinit();
    
    return 0;
}

原文地址:https://blog.csdn.net/qq_36413982/article/details/142920234

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!