getopt_long不传递参数

雅各布

我可能只是在下面的代码上引起了我的注意,但是我无法让getopt_long传递参数ASK_MASK0的“ maska”。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>

typedef char Char;
typedef int Int;
typedef unsigned char UInt8;

enum eArgs {
    ARG_MOTION=258, //258
    ARG_MASK0,  //259
    ARG_MASK1,  //260
    ARG_NO_TTY  //261
};


void main(Int argc, Char *argv[])
{
    const Char shortOptions[] = "h";
    const struct option longOptions[] = {
        {"motion",           required_argument, NULL, ARG_MOTION},
        {"maska",             required_argument, NULL, ARG_MASK0},
        {"maskb",             required_argument, NULL, ARG_MASK1},
        {"notty",            no_argument,       NULL, ARG_NO_TTY},
        {"help",             no_argument,       NULL, 'h'},
        {0, 0, 0, 0}
    };

    Char    *end;
    Int     index;
    Int     c,i;
    UInt8   mask[10];

    fprintf(stderr, "argc=%i\n", argc);
    for(i=1; i < argc; i++) {
        fprintf(stderr, "argv[%i]=%s\n", i,argv[i]);
    }

    for (;;) {
        c = getopt_long(argc, argv, shortOptions, longOptions, &index);

        fprintf(stderr,"c=%i\n", c);

        if (c == -1) {
            break;
        }
    }
}

我在Ubuntu上将代码编译为:

gcc test_parseargs.c -o main

并运行为:

./main --motion --maska=0 --maskb=1 --notty

获取输出:

argc=5
argv[1]=--motion
argv[2]=--maska=0
argv[3]=--maskb=1
argv[4]=--notty
c=258
c=260
c=261
c=-1

我的代码有什么问题?

用户名

问题在于你的论点

{"motion",           required_argument, NULL, ARG_MOTION}, //code

./main --motion --maska=0 --maskb=1 --notty

缺少运动参数。请尝试以下。

./main --motion=1 --maska=0 --maskb=1 --notty

在上一轮中,--maska = 0被视为运动参数

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章