【发布时间】:2014-10-15 10:34:09
【问题描述】:
我有一个关于 C 中 getopt 错误处理的问题:
#include <unistd.h>
#include <getopt.h>
void showFunction()
{
printf("show function\n");
}
void printHelp()
{
printf("print help info\n");
}
#define HELP 1
#define SHOW_OPTION 2
int main(int argc, char *argv[])
{
const struct option long_opts[] = {{"help", no_argument, NULL, HELP},
{"show", no_argument ,NULL, SHOW_OPTION},
{NULL, 0, NULL, 0}};
int opt;
while((opt = getopt_long_only(argc, argv, "", long_opts, NULL)) != -1)
{
switch(opt) {
case HELP:
printHelp();
break;
case SHOW_OPTION:
showFunction();
break;
case '?':
printHelp();
break;
default:
printf("type base --help for details\n");
}
}
return 0;
}
这部分会处理一些错误:
case '?':
printHelp();
break;
但是如果我输入./base --或./base -或./base sdfs或./base -- fsfs,它无法处理所有这些无效输入,那么如何处理上面的输入呢?有人可以帮忙吗?
【问题讨论】:
标签: c getopt getopt-long