Linux C getopt_long
#include <stdio.h>
#include <getopt.h>
char *l_opt_arg;
char* const short_options = "c:";
struct option long_options[] = {
{ "aa", 0, NULL, 0 },
{ "bb", 0, NULL, 0 },
{ "cc", required_argument, NULL, 'c' },
{ 0, 0, 0, 0},
};
int main(int argc, char *argv[])
{
int c;
int index = 0;
while((c = getopt_long (argc, argv, short_options, long_options, &index)) != -1)
{
switch (c)
{
case 0:
switch (index)
{
case 0:
printf("is aa : c = %d, index = %d\n", c, index);
break;
case 1:
printf("is bb : c = %d, index = %d\n", c, index);
break;
default:
printf("no one : c= %d, index = %d\n", c, index);
break;
}
break;
case 'c':
l_opt_arg = optarg;
printf("cc is %s(optarg), c = %c, index = %d!\n", l_opt_arg, c, index);
break;
}
}
return 0;
}
posted on 2015-06-24 11:49 DayAfterDay 阅读(223) 评论(0) 编辑 收藏 举报