C:getopt_long()始终返回无效选项

文森佐·克里斯蒂亚诺(Vincenzo Cristiano)

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

val字段struct option仅用于告知getopt_long()遇到长选项时返回什么;它没有定义一个简短的选择。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

getopt_long() 的段错误

getopt_long:仅当用户提供命令行选项时才修改标志

使用getopt_long处理用户错误

getopt_long不传递参数

将`getopt_long`与无法识别的long选项一起使用时出现分段错误

使用`getopt_long`时了解`option long_options []`

getopt_long()看不到终结符

getopt_long 函數中“:”的意義是什麼?

将getopt_long()与我自己的代码捆绑在一起?

为什么从getopt_long()中省略这一行会导致段错误?

当有错误的命令行参数时,如何让 getopt_long() 不打印任何内容?

为什么C getopt_long_only()没有为未知选项设置optopt?

选项有选项C ++时使用getopt

PayPal IPN始终返回“无效”

Getopt :: Long :: Descriptive-选项规范中的错误:

如何使用Getopt :: Long将选项存储在复杂的哈希中

无法使用C的getopt()访问多个选项

获取选项值始终返回“ null”

如何在C ++中使getopt严格执行选项检查?

C的getopt无法在argv的末尾(或中间)解析选项

如何使用getOpt确定C中是否没有选项?

如果从第三个参数开始,getopt始终返回-1

在哈希中添加Getopt :: Long选项,即使使用重复说明符也是如此

如何使用Getopt :: Long [:: Descriptive]处理由任意数字组成的选项?

PHP-贝宝IPN始终返回无效

Momentjs严格格式始终返回“无效日期”

Okta API-身份验证始终返回无效的会话

NodeJS护照-ldapauth始终返回“无效的用户名/密码”

尝试使用bat文件打开程序,但始终返回无效