getopt_long

getopt_long可以用来解析命令行参数,也可以进行参数解析。例如

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <getopt.h>
static int verbose_flag;
int
parse(int argc, char **argv)
{
int c;
int digit_optind = 0;
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"verbose", no_argument, &verbose_flag, 1},
{"add", required_argument, 0, 0 },
{"append", no_argument, 0, 0 },
{"delete", required_argument, 0, 0 },
{"verbose", no_argument, 0, 0 },
{"create", required_argument, 0, 'c'},
{"file", required_argument, 0, 0 },
{0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "abc:d:012",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\n");
break;
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf("option %c\n", c);
break;
case 'a':
printf("option a\n");
break;
case 'b':
printf("option b\n");
break;
case 'c':
printf("option c with value '%s'\n", optarg);
break;
case 'd':
printf("option d with value '%s'\n", optarg);
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
if (verbose_flag)
printf ("verbose flag is set\n");
return 0;
}
void
main()
{
char *argv[6]={0};
int argc = 6;
argv[0] = "prog1";
argv[1] = "--add";
argv[2] = "AA";
argv[3] = "-d";
argv[4] = "BB";
argv[5] = "--verbose";
parse(argc, argv);
optind = 1;
char *argv2[4]={0};
int argc2 = 4;
argv2[0] = "prog2";
argv2[1] = "--file";
argv2[2] = "CC";
argv2[3] = "--verbose";
//argv2[3] = "--delete";
//argv2[4] = "DD";
parse(argc2, argv2);
return ;
}
#include <stdio.h> /* for printf */ #include <stdlib.h> /* for exit */ #include <getopt.h> static int verbose_flag; int parse(int argc, char **argv) { int c; int digit_optind = 0; while (1) { int this_option_optind = optind ? optind : 1; int option_index = 0; static struct option long_options[] = { {"verbose", no_argument, &verbose_flag, 1}, {"add", required_argument, 0, 0 }, {"append", no_argument, 0, 0 }, {"delete", required_argument, 0, 0 }, {"verbose", no_argument, 0, 0 }, {"create", required_argument, 0, 'c'}, {"file", required_argument, 0, 0 }, {0, 0, 0, 0 } }; c = getopt_long(argc, argv, "abc:d:012", long_options, &option_index); if (c == -1) break; switch (c) { case 0: printf("option %s", long_options[option_index].name); if (optarg) printf(" with arg %s", optarg); printf("\n"); break; case '0': case '1': case '2': if (digit_optind != 0 && digit_optind != this_option_optind) printf("digits occur in two different argv-elements.\n"); digit_optind = this_option_optind; printf("option %c\n", c); break; case 'a': printf("option a\n"); break; case 'b': printf("option b\n"); break; case 'c': printf("option c with value '%s'\n", optarg); break; case 'd': printf("option d with value '%s'\n", optarg); break; case '?': break; default: printf("?? getopt returned character code 0%o ??\n", c); } } if (optind < argc) { printf("non-option ARGV-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); } if (verbose_flag) printf ("verbose flag is set\n"); return 0; } void main() { char *argv[6]={0}; int argc = 6; argv[0] = "prog1"; argv[1] = "--add"; argv[2] = "AA"; argv[3] = "-d"; argv[4] = "BB"; argv[5] = "--verbose"; parse(argc, argv); optind = 1; char *argv2[4]={0}; int argc2 = 4; argv2[0] = "prog2"; argv2[1] = "--file"; argv2[2] = "CC"; argv2[3] = "--verbose"; //argv2[3] = "--delete"; //argv2[4] = "DD"; parse(argc2, argv2); return ; }
#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>

static int verbose_flag;

int
parse(int argc, char **argv)
{
    int c;
    int digit_optind = 0;

    while (1) {
        int this_option_optind = optind ? optind : 1;
        int option_index = 0;
        static struct option long_options[] = {
            {"verbose", no_argument,       &verbose_flag, 1},
            {"add",     required_argument, 0,  0 },
            {"append",  no_argument,       0,  0 },
            {"delete",  required_argument, 0,  0 },
            {"verbose", no_argument,       0,  0 },
            {"create",  required_argument, 0, 'c'},
            {"file",    required_argument, 0,  0 },
            {0,         0,                 0,  0 }
        };

       c = getopt_long(argc, argv, "abc:d:012",
                 long_options, &option_index);
        if (c == -1)
            break;

       switch (c) {
        case 0:
            printf("option %s", long_options[option_index].name);
            if (optarg)
                printf(" with arg %s", optarg);
            printf("\n");
            break;

        case '0':
        case '1':
        case '2':
            if (digit_optind != 0 && digit_optind != this_option_optind)
              printf("digits occur in two different argv-elements.\n");
            digit_optind = this_option_optind;
            printf("option %c\n", c);
            break;

       case 'a':
            printf("option a\n");
            break;

       case 'b':
            printf("option b\n");
            break;

       case 'c':
            printf("option c with value '%s'\n", optarg);
            break;

       case 'd':
            printf("option d with value '%s'\n", optarg);
            break;

       case '?':
            break;

       default:
            printf("?? getopt returned character code 0%o ??\n", c);
        }
    }

    if (optind < argc) {
        printf("non-option ARGV-elements: ");
        while (optind < argc)
            printf("%s ", argv[optind++]);
        printf("\n");
    }

    if (verbose_flag)
        printf ("verbose flag is set\n");

    return 0;
}

void
main()
{
        char *argv[6]={0};
        int argc = 6;
        argv[0] = "prog1";
        argv[1] = "--add";
        argv[2] = "AA";
        argv[3] = "-d";
        argv[4] = "BB";
        argv[5] = "--verbose";

        parse(argc, argv);

        optind = 1;

        char *argv2[4]={0};
        int argc2 = 4;
        argv2[0] = "prog2";
        argv2[1] = "--file";
        argv2[2] = "CC";
        argv2[3] = "--verbose";
        //argv2[3] = "--delete";
        //argv2[4] = "DD";

        parse(argc2, argv2);
        return ;
}

 

getopt可以解析一个字母的命令行参数, “-d”, getopt_long则可以解析更长名称的命令行参数, 以”–“开头, 如例子中的”–file”.

参数比getopt多了两个, 函数原型如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#include <getopt.h>
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
#include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; #include <getopt.h> int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex); int getopt_long_only(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
#include <unistd.h>

int getopt(int argc, char * const argv[],
           const char *optstring);

extern char *optarg;
extern int optind, opterr, optopt;

#include <getopt.h>

int getopt_long(int argc, char * const argv[],
           const char *optstring,
           const struct option *longopts, int *longindex);

int getopt_long_only(int argc, char * const argv[],
           const char *optstring,
           const struct option *longopts, int *longindex);

 

另外注意函数依赖的定义, 编译时可以指定

getopt(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE
getopt_long
(), getopt_long_only(): _GNU_SOURCE

getopt_long 参数结构体相对复杂一些

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
struct option { const char *name; int has_arg; int *flag; int val; };
struct option {
    const char *name;
    int         has_arg;
    int        *flag;
    int         val;
};


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
static struct option long_options[] = {
{"verbose", no_argument, &verbose_flag, 1},
{"add", required_argument, 0, 0 },
{"append", no_argument, 0, 0 },
{"delete", required_argument, 0, 0 },
{"verbose", no_argument, 0, 0 },
{"create", required_argument, 0, 'c'},
{"file", required_argument, 0, 0 },
{0, 0, 0, 0 }
};
static struct option long_options[] = { {"verbose", no_argument, &verbose_flag, 1}, {"add", required_argument, 0, 0 }, {"append", no_argument, 0, 0 }, {"delete", required_argument, 0, 0 }, {"verbose", no_argument, 0, 0 }, {"create", required_argument, 0, 'c'}, {"file", required_argument, 0, 0 }, {0, 0, 0, 0 } };
static struct option long_options[] = {
    {"verbose", no_argument,       &verbose_flag, 1},
    {"add",     required_argument, 0,  0 },
    {"append",  no_argument,       0,  0 },
    {"delete",  required_argument, 0,  0 },
    {"verbose", no_argument,       0,  0 },
    {"create",  required_argument, 0, 'c'},
    {"file",    required_argument, 0,  0 },
    {0,         0,                 0,  0 }
};

 

如果设置了flag不为null, 那么将val值复制给flag指针指向变量。

例子中是将verbose_flag设置为 1.

如果设置了flag为null, 那么val作为返回值, 这个返回值可以是设置为含义相同的短类型参数的标签,

例子中的c和create是相同的含义, 一个是长类型参数, 一个是短类型的参数。

 

另外还有个比较重要的全局变量:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
extern char *optarg;
extern int optind, opterr, optopt;
extern char *optarg; extern int optind, opterr, optopt;
extern char *optarg; 
extern int optind, opterr, optopt;

如果 getopt() 无法识别选项字符,它将向 stderr 打印一条错误消息,将该字符存储在 optopt 中,并返回“?”。调用程序可以通过将 opterr 设置为 0 来防止出现错误消息。

因为optind存储的下一个要解析的参数, 如果是getopt_long, 可以保留optind位置, 解析出错时显示

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <getopt.h>
static int verbose_flag;
int
parse(int argc, char **argv)
{
...
case '?':
printf("unkown option '%s'\n", argv[old_optnd]);
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
old_optnd = optind;
}
...
}
void
main()
{
opterr = 0;
char *argv[8]={0};
int argc = 8;
argv[0] = "prog1";
argv[1] = "--add";
argv[2] = "AA";
argv[3] = "-d";
argv[4] = "BB";
argv[5] = "--verbose";
argv[6] = "--error";
argv[7] = "ddd";
parse(argc, argv);
optind = 1;
char *argv2[4]={0};
int argc2 = 4;
argv2[0] = "prog2";
argv2[1] = "--file";
argv2[2] = "CC";
argv2[3] = "--verbose";
//argv2[3] = "--delete";
//argv2[4] = "DD";
parse(argc2, argv2);
return ;
}
#include <stdio.h> /* for printf */ #include <stdlib.h> /* for exit */ #include <getopt.h> static int verbose_flag; int parse(int argc, char **argv) { ... case '?': printf("unkown option '%s'\n", argv[old_optnd]); break; default: printf("?? getopt returned character code 0%o ??\n", c); } old_optnd = optind; } ... } void main() { opterr = 0; char *argv[8]={0}; int argc = 8; argv[0] = "prog1"; argv[1] = "--add"; argv[2] = "AA"; argv[3] = "-d"; argv[4] = "BB"; argv[5] = "--verbose"; argv[6] = "--error"; argv[7] = "ddd"; parse(argc, argv); optind = 1; char *argv2[4]={0}; int argc2 = 4; argv2[0] = "prog2"; argv2[1] = "--file"; argv2[2] = "CC"; argv2[3] = "--verbose"; //argv2[3] = "--delete"; //argv2[4] = "DD"; parse(argc2, argv2); return ; }
#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>

static int verbose_flag;

int
parse(int argc, char **argv)
{

       ...

       case '?':
            printf("unkown option  '%s'\n", argv[old_optnd]);
            break;

       default:
            printf("?? getopt returned character code 0%o ??\n", c);
        }
       old_optnd = optind;
    }
    ...
}

void
main()
{
        opterr = 0;
        char *argv[8]={0};
        int argc = 8;
        argv[0] = "prog1";
        argv[1] = "--add";
        argv[2] = "AA";
        argv[3] = "-d";
        argv[4] = "BB";
        argv[5] = "--verbose";
        argv[6] = "--error";
        argv[7] = "ddd";

        parse(argc, argv);

        optind = 1;

        char *argv2[4]={0};
        int argc2 = 4;
        argv2[0] = "prog2";
        argv2[1] = "--file";
        argv2[2] = "CC";
        argv2[3] = "--verbose";
        //argv2[3] = "--delete";
        //argv2[4] = "DD";

        parse(argc2, argv2);
        return ;
}

 

如果不设置 opterr, 会有错误输出 prog1: unrecognized option ‘–error’

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
garlic@garlic:~/getoption$ ./test
option add with arg AA
option d with value 'BB'
option verbose
prog1: unrecognized option '--error'
unkown option '--error'
non-option ARGV-elements: ddd
verbose flag is set
option file with arg CC
option verbose
verbose flag is set
garlic@garlic:~/getoption$ ./test option add with arg AA option d with value 'BB' option verbose prog1: unrecognized option '--error' unkown option '--error' non-option ARGV-elements: ddd verbose flag is set option file with arg CC option verbose verbose flag is set
garlic@garlic:~/getoption$ ./test
option add with arg AA
option d with value 'BB'
option verbose
prog1: unrecognized option '--error'
unkown option  '--error'
non-option ARGV-elements: ddd
verbose flag is set
option file with arg CC
option verbose
verbose flag is set

opterr设置为1 则没有相关输出。

解析完毕后记得要重新optind, 因为这个变量使用后不会重置。会出现 Segment Fault

 

 

参考及引用

https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Option-Example.html

https://linux.die.net/man/3/getopt_long

https://cplusplus.com/forum/unices/104742/ optind问题

图片from陳為

 

Comments are closed.