C: getopt_long() 总是 returns 无效选项

C: getopt_long() always returns invalid option

我正在尝试使用 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;
        }

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

如果你想让一个选项有一个单字符版本,它需要进入 getopt_long() 的第三个参数,而不仅仅是在长选项数组中:

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

struct optionval字段只是用来告诉getopt_long()遇到那个长选项时return做什么;它没有定义短选项。