您的位置:首页 > 运维架构

struct option 的使用

2016-03-23 11:38 309 查看
#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;
}


实际效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: