#include <stdio.h>
#include <getopt.h>
char *l_opt_arg;
char* const short_options = "nl:b";
//我设置l是要接参数的。所以l后面有个冒号
//l: means after l there should hava an argument
struct option long_options[] = {
{ "name", 0, NULL, 'n' },
{ "bf_name", 0, NULL, 'b' },
{ "love", 1, NULL, 'l' },
//1表示require argument ,l后面需要接参数
{ 0, 0, 0, 0},
};
int main(int argc, char *argv[])
{
int c;
while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)
{
switch (c)
{
case 'n':
printf("My name is AA.\n");
break;
case 'b':
printf("Her name is BB.\n");
break;
case 'l':
l_opt_arg = optarg;
printf("Our love is %s!\n", l_opt_arg);
//l_opt_arg为l后面输入的参数
break;
}
}
return 0;
}
实际效果: