我使用 getopt 编写了一个简单的代码来理解透视图。
#include <stdio.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
/* Here since c is followed with colon, so 'c' takes an argument */
const char *optstring = "abc:d";
int ret;
while((ret=getopt(argc, argv, optstring))!= -1)
{
switch(ret)
{
case 'a':
printf("Option found: a, optind = %d\n",optind);
break;
case 'b':
printf("Option found: b, optind = %d\n",optind);
break;
case 'c':
printf("Option found: c, optind = %d\n",optind);
printf("Option argument: %s\n",optarg);
break;
case 'd':
printf("Option found: d, optind = %d\n",optind);
break;
case ':':
printf("The option takes an argument which is missing");
break;
//case '?':
// printf("Didn't you enter an invalid option?");
// break;
}
}
}
问题是:
(1)案例1:
如果case '?'
评论了,那么:
[root@dhcppc0 getopt]# ./a.out -a -b -c
Option found: a, optind = 2
Option found: b, optind = 3
./a.out: option requires an argument -- c
因此,正如您所看到的,case ':'
没有生效,因为通常我们期望缺少的参数通过 getopt 返回一个 ':'(冒号)。
(2) 情况 2:
并且,如果我取消注释它,然后运行程序,它会遇到case '?
缺少参数的偶数。
enter code here
[root@dhcppc0 getopt]# ./a.out -a -b -c
Option found: a, optind = 2
Option found: b, optind = 3
./a.out: option requires an argument -- c
Didn't you enter an invalid option?
我在这里想念什么?
稍后添加:
另外为什么会./a.out: option requires an argument -- c
出现默认错误?如何处理它,因为我已经在 中照顾它case ':'
,并且不想要默认错误消息?
再次添加:正如答案中所建议的,我在 optstring - 的开头使用了冒号const char *optstring = ":abc:d"
,那么为什么会发生这种情况?
./a.out -a -b -c -d returns -d as the argument to c?? -d is a separate optional character and not any argument