首页 > Web开发 > 详细

【OpenSource】源码--Web Bench 1.5

时间:2016-01-11 21:39:37      阅读:236      评论:0      收藏:0      [点我收藏+]

头文件:

include<getopt.h>//功能:主要用来处理接受的命令行参数argc,argv[]。函数主要包括getopt()和getopt_long(),不用自己处理用户输入的命令行参数了。

  [指令]webbench -c 10 -t 20 http://www.baidu.com/  其中argc==6从1开始计数,argv[0-5]分别==webbench -c 10 -t 20 http://...从0开始计数

 getopt()函数原型:int getopt(int argc,char *const argv[],const char *optstring);

      全局变量 extern char *optarg;        extern int optind, opterr, optopt; optarg选项的参数指针,optind这个索引指向argv里当前分析的字符串的下一个索引。

  getopt()处理以-开头的命令行参数,如optstring="912Vfrt:p:c:?h"表示-9 -V -f -p -c -t

   调用该函数一次返回一个选项,直到结束返回-1。

 getopt_long()函数原型:int getopt_long(int argc,char *const argv[],const char *optstring,const struct option *longopts,

int *longindex);

while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1) 

webbench中为:

while((opt=getopt_long(argc,argv,"912Vfrt:p:c:?h",long_options,&options_index))!=EOF )//:后代表需要参数

 struct option 类型数组
       该数据结构中的每个元素对应了一个长选项,并且每个元素是由四个域组成。通常情况下,可以按以下规则使用。第一个元素,描述长选项的名称;第二个选项,代表 该选项是否需要跟着参数,需要参数则为1,反之为0。no_argument 0 选项没有参数 required_argument 1 选项需要参数 optional_argument 2 选项参数是可选的;第三个选项,可以赋为NULL;第四个选项,是该长选项对应的短选项名称。另外,数据结构的最后一个元素,要求所有域的内容均为0,即{NULL,0,NULL,0}。

 

   longopts是一个struct结构体实例,webbench中为:

static const struct option long_options[]=
{
 {"force",no_argument,&force,1},
 {"reload",no_argument,&force_reload,1},
 {"time",required_argument,NULL,‘t‘},
 {"help",no_argument,NULL,‘?‘},
 {"http09",no_argument,NULL,‘9‘},
 {"http10",no_argument,NULL,‘1‘},
 {"http11",no_argument,NULL,‘2‘},
 {"get",no_argument,&method,METHOD_GET},
 {"head",no_argument,&method,METHOD_HEAD},
 {"options",no_argument,&method,METHOD_OPTIONS},
 {"trace",no_argument,&method,METHOD_TRACE},
 {"version",no_argument,NULL,‘V‘},
 {"proxy",required_argument,NULL,‘p‘},
 {"clients",required_argument,NULL,‘c‘},
 {NULL,0,NULL,0}
};

main函数中,int options_index=0;
 

 

 

 

 

【OpenSource】源码--Web Bench 1.5

原文:http://www.cnblogs.com/gjbmxy/p/5122373.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!