0

我正在尝试使用getopt_long向已经工作的 C 程序添加一个新选项。我要添加的选项是-S但每次我尝试运行我得到的代码时:

./HidCom: invalid option --'S'

我只是在long_options向量中添加了一个关于我想以这种方式添加的选项的新元素:

    {"Status",  required_argument, 0, 'S'},

下面是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/hiddev.h>
#include <linux/input.h>
#include <time.h>
#include <ctype.h>
#include <getopt.h>
#include <string.h>


int main (int argc, char **argv) {
    int n=64,d=0,info=0,increment=0,p=0,vid=0x16c0,pid=0x05df,q=0,f=1,ctrl=0,r=1,c,i,j;
    int inputState = 0;
    char path[256],buf[256];
    path[0]=0;
    for(i=0;i<256;i++) buf[i]=0;
    //opterr = 0; in order to show the error
    int option_index = 0;
    struct option long_options[] =
    {
        {"control", no_argument,      &ctrl,1},
        {"c",       no_argument,      &ctrl,1},
        {"delay",   required_argument, 0, 'd'},
        {"d",       required_argument, 0, 'd'},
        {"feature", no_argument,         &f,1},
        {"f",       no_argument,         &f,1},
        {"help",    no_argument,       0, 'h'},
        {"i",       no_argument,      &info,1},
        {"info",    no_argument,      &info,1},
        {"increment",no_argument,&increment,1},
        {"I",       no_argument, &increment,1},
        {"path",    required_argument, 0, 'P'},
        {"pid",     required_argument, 0, 'p'},
        {"quiet",   no_argument,        &q, 1},
        {"q",       no_argument,        &q, 1},
        {"repeat",  required_argument, 0, 'r'},
        {"size",    required_argument, 0, 's'},
        {"Status",  required_argument, 0, 'S'},
        {"vid",     required_argument, 0, 'v'},
        {0, 0, 0, 0}
    };
    while ((c = getopt_long (argc, argv, "cd:fhiIp:qv:r:s:",long_options,&option_index)) != -1)
     switch (c)
        {
        case 'h':
        printf("hid_test [options] [data]\
        // prints help 
        exit(1);
        break;
        case 'c':   //control endpoint
             ctrl=1;
             break;
        case 'd':   //delay
            d = atoi(optarg);
            break;
        case 'f':   //feature report
            f=1;
            break;
        case 'i':   //info
            info=1;
            break;
        case 'I':   //increment
            increment=1;
            break;
        case 'p':   //pid
            sscanf(optarg, "%x", &pid);
            break;
        case 'P':   //path
            strncpy(path,optarg,256);
            break;
        case 'q':   //quiet
            q=1;
            break;
        case 'r':   //repeat
            r = atoi(optarg);
            break;
        case 's':   //report size
            n = atoi(optarg);   //No always add report index
            break;
        case 'S':
            inputState = 1;
            n = atoi(optarg);
            break;
        case 'v':   //vid
            sscanf(optarg, "%x", &vid);
            break;
        case '?':
            fprintf (stderr, "Unknown option character 0x%02x (%c)\n",optopt,optopt);
            return 1;
        default:
         //abort ();
        break;
        }

我也尝试了不同的字符,但我总是得到无效的选项。问题出在哪里?

4

1 回答 1

2

如果您希望选项具有单字符版本,则需要将其放入 的第三个参数中getopt_long(),而不仅仅是在长选项数组中:

getopt_long (argc, argv, "cd:fhiIp:qv:r:s:S:",long_options,&option_index)

val字段struct option只是用来告诉getopt_long()当它遇到那个长选项时要返回什么;它没有定义一个简短的选项。

于 2020-11-18T09:29:21.393 回答