getopt_long 未按预期工作
getopt_long not working as expected
我有以下代码
#include <stdio.h>
#include <getopt.h>
int main(int argc, char* argv[]){
const struct option longopts[]={
{"one", required_argument, 0, '1'},
{"two", required_argument, 0, '2'},
{"three", required_argument, 0, '3'},
{"four", required_argument, 0, '4'},
{"five", required_argument, 0, '5'},
{0,0,0,0}
};
const char* shortopts="1:2:3:4:5:";
int c;
c = -1;
for(;;){
int optind = 0;
c = getopt_long(argc, argv, shortopts, longopts, &optind);
if(c<0)
break;
switch(c){
case 0:
case '1':
case '2':
case '3':
case '4':
case '5':
fprintf(stdout, "----------------------------------------\n");
fprintf(stdout, "c = %c\n", c);
fprintf(stdout, "optindd = %d\n", optind);
fprintf(stdout, "val = %c, \n", longopts[optind].val);
fprintf(stdout, "name = %s\n", longopts[optind].name);
fprintf(stdout, "optarg = %s\n", optarg);
break;
}
}
}
输入:
./a.out --one 1 -2 two --three 3 -4 four --five 5
预期输出:
我想在遇到对应的 shortopt/longopt 时打印结构选项的成员(name 和 val)。
上面的代码打印了一些意外的输出:
----------------------------------------
c = 1
optindd = 0
val = 1,
name = one
optarg = 1
----------------------------------------
c = 2
optindd = 0 // expected 1
val = 1, // expected 2
name = one // expected two
optarg = two
----------------------------------------
c = 3
optindd = 2
val = 3,
name = three
optarg = 3
----------------------------------------
c = 4
optindd = 0 // expected 3
val = 1, // expected 4
name = one // expected four
val = four
----------------------------------------
c = 5
optindd = 4
val = 5,
name = five
val = 5
我正在使用 Ubuntu 14.04.
getopt_long
的 longindex
return 参数仅适用于长期权,不适用于短期权。 getopt_long
无法知道哪个多头选项对应于给定的空头选项(尽管对应关系对您来说似乎很明显)。如果找到短选项,longindex
变量不变,因此您应该初始化为您可以识别的值(如 -1),而不是将其初始化为 0。
顺便说一下,optind
是由 getopt
维护的全局变量,您将需要它以便在完成标志后处理位置参数。用一个同名的局部变量覆盖这个变量是不明智的(虽然合法);当您需要值时,它既会让读者感到困惑,也会让您感到尴尬。
None 可帮助您识别与给定空头期权相对应的多头期权。如果您觉得需要该信息,那完全是您的责任。例如,您可以在 longopts 结构中搜索与找到的空头选项相对应的 val
;然后,您将不得不处理给定空头期权在该结构中出现零次或多次的可能性。
作为一个简单的例子,而不是:
int optindex = 0;
c = getopt_long(argc, argv, shortopts, longopts, &optindex);
你可以这样做:
int optindex = -1;
c = getopt_long(argc, argv, shortopts, longopts, &optindex);
if (optindex == -1) {
for (optindex = 0; longopts[optindex].name, ++optindex) {
if (longopts[optindex].val == c) break;
}
if (longopts[optindex].name == NULL) {
// the short option was not found; do something
}
}
我有以下代码
#include <stdio.h>
#include <getopt.h>
int main(int argc, char* argv[]){
const struct option longopts[]={
{"one", required_argument, 0, '1'},
{"two", required_argument, 0, '2'},
{"three", required_argument, 0, '3'},
{"four", required_argument, 0, '4'},
{"five", required_argument, 0, '5'},
{0,0,0,0}
};
const char* shortopts="1:2:3:4:5:";
int c;
c = -1;
for(;;){
int optind = 0;
c = getopt_long(argc, argv, shortopts, longopts, &optind);
if(c<0)
break;
switch(c){
case 0:
case '1':
case '2':
case '3':
case '4':
case '5':
fprintf(stdout, "----------------------------------------\n");
fprintf(stdout, "c = %c\n", c);
fprintf(stdout, "optindd = %d\n", optind);
fprintf(stdout, "val = %c, \n", longopts[optind].val);
fprintf(stdout, "name = %s\n", longopts[optind].name);
fprintf(stdout, "optarg = %s\n", optarg);
break;
}
}
}
输入:
./a.out --one 1 -2 two --three 3 -4 four --five 5
预期输出:
我想在遇到对应的 shortopt/longopt 时打印结构选项的成员(name 和 val)。
上面的代码打印了一些意外的输出:
----------------------------------------
c = 1
optindd = 0
val = 1,
name = one
optarg = 1
----------------------------------------
c = 2
optindd = 0 // expected 1
val = 1, // expected 2
name = one // expected two
optarg = two
----------------------------------------
c = 3
optindd = 2
val = 3,
name = three
optarg = 3
----------------------------------------
c = 4
optindd = 0 // expected 3
val = 1, // expected 4
name = one // expected four
val = four
----------------------------------------
c = 5
optindd = 4
val = 5,
name = five
val = 5
我正在使用 Ubuntu 14.04.
getopt_long
的 longindex
return 参数仅适用于长期权,不适用于短期权。 getopt_long
无法知道哪个多头选项对应于给定的空头选项(尽管对应关系对您来说似乎很明显)。如果找到短选项,longindex
变量不变,因此您应该初始化为您可以识别的值(如 -1),而不是将其初始化为 0。
顺便说一下,optind
是由 getopt
维护的全局变量,您将需要它以便在完成标志后处理位置参数。用一个同名的局部变量覆盖这个变量是不明智的(虽然合法);当您需要值时,它既会让读者感到困惑,也会让您感到尴尬。
None 可帮助您识别与给定空头期权相对应的多头期权。如果您觉得需要该信息,那完全是您的责任。例如,您可以在 longopts 结构中搜索与找到的空头选项相对应的 val
;然后,您将不得不处理给定空头期权在该结构中出现零次或多次的可能性。
作为一个简单的例子,而不是:
int optindex = 0;
c = getopt_long(argc, argv, shortopts, longopts, &optindex);
你可以这样做:
int optindex = -1;
c = getopt_long(argc, argv, shortopts, longopts, &optindex);
if (optindex == -1) {
for (optindex = 0; longopts[optindex].name, ++optindex) {
if (longopts[optindex].val == c) break;
}
if (longopts[optindex].name == NULL) {
// the short option was not found; do something
}
}