getopt()
没有像我对空头期权的预期那样表现。
例如:使用缺少的参数调用以下程序:
有效案例:testopt -d dir -a action -b build
错误案例:testopt -d -a action -b build
这并没有引发任何错误,因为我期待一个错误消息操作数丢失-d
- 这是一个已知的错误?
- 如果是这样,是否有可用的标准修复程序?
我的代码:
#include <unistd.h>
/* testopt.c */
/* Test program for testing getopt */
int main(int argc, char **argv)
{
int chr;
while ( ( chr = getopt(argc, argv, ":d:a:b:") ) != -1 )
{
switch(chr)
{
case 'a':
printf("Got a...\n");
break;
case 'b':
printf("Got b...\n");
break;
case 'd':
printf("Got d...\n");
break;
case ':':
printf("Missing operand for %c\n", optopt);
break;
case '?':
printf("Unknown option %c\n", optopt);
break;
}
}
printf("execution over\n");
return 0;
}