头文件:
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,
webbench中为:
while((opt=getopt_long(argc,argv,"912Vfrt:p:c:?h",long_options,&options_index))!=EOF )//:后代表需要参数
struct option 类型数组
longopts是一个struct结构体实例,webbench中为:
原文:http://www.cnblogs.com/gjbmxy/p/5122373.html