网站内容排版设计模板,专门做珠宝的网站,做游戏 网站,wordpress手机网站模板上午在看源码项目 webbench 时#xff0c;刚开始就被一个似乎挺陌生函数 getopt_long() 给卡住了#xff0c;说实话这函数没怎么见过#xff0c;自然不知道这哥们是干什么的。于是乎百度了一番#xff0c;原来是处理命令行选项参数的#xff0c;的确#xff0c;正规点的大…上午在看源码项目 webbench 时刚开始就被一个似乎挺陌生函数 getopt_long() 给卡住了说实话这函数没怎么见过自然不知道这哥们是干什么的。于是乎百度了一番原来是处理命令行选项参数的的确正规点的大型程序一般第一步就是处理命令行参数的接着才是主干程序。在百度和 man 的帮助下找到了具体使用方法和解释二话不说赶紧学习一下并总结出文档记录一下。平时在写程序时常常需要对命令行参数进行处理因为参数少自己解析就可以搞定如果命令行个数比较多时如果按照顺序一个一个定义参数含义很容易造成混乱而且如果程序只按顺序处理参数的话一些“可选参数”的功能将很难实现这个问题在 linux 中用 getopt 等函数可以优雅地解决。一、查询linux命令手册#includeunistd.h
#includegetopt.h /*所在头文件 */
int getopt(intargc, char * const argv[], const char *optstring);
int getopt_long(int argc, char * const argv[], const char *optstring,const struct option *longopts, int*longindex);
int getopt_long_only(int argc, char * const argv[],const char *optstring,const struct option *longopts, int*longindex);
extern char *optarg; /*系统声明的全局变量 */
extern int optind, opterr, optopt;先拿最简单的 getopt 函数开刀getopt_long 只是前者的增强版功能多点而已。二、getopt函数1、定义int getopt(int argc, char * const argv[], const char *optstring);2、描述getopt是用来解析命令行选项参数的但是只能解析短选项: -d 100,不能解析长选项--prefix3、参数argcmain()函数传递过来的参数的个数
argvmain()函数传递过来的参数的字符串指针数组
optstring选项字符串告知 getopt()可以处理哪个选项以及哪个选项需要参数4、返回如果选项成功找到返回选项字母如果所有命令行选项都解析完毕返回 -1如果遇到选项字符不在 optstring 中返回字符 ?如果遇到丢失参数那么返回值依赖于 optstring 中第一个字符如果第一个字符是 : 则返回:否则返回?并提示出错误信息。5、下边重点举例说明optstring的格式意义char*optstring “ab:c::”;
单个字符a 表示选项a没有参数 格式-a即可不加参数
单字符加冒号b: 表示选项b有且必须加参数 格式-b 100或-b100,但-b100错
单字符加2冒号c:: 表示选项c可以有也可以无 格式-c200其它格式错误上面这个 optstring 在传入之后getopt 函数将依次检查命令行是否指定了 -a -b -c(这需要多次调用 getopt 函数直到其返回-1)当检查到上面某一个参数被指定时函数会返回被指定的参数名称(即该字母)optarg —— 指向当前选项参数(如果有)的指针。
optind —— 再次调用 getopt() 时的下一个 argv指针的索引。
optopt —— 最后一个未知选项。
opterr —— 如果不希望getopt()打印出错信息则只要将全域变量opterr设为0即可。以上描述的并不生动下边结合实例来理解6、实例#includestdio.h
#includeunistd.h
#includegetopt.h
int main(intargc, char *argv[])
{int opt;char *string a::b:c:d;while ((opt getopt(argc, argv, string))! -1){ printf(opt %c\t\t, opt);printf(optarg %s\t\t,optarg);printf(optind %d\t\t,optind);printf(argv[optind] %s\n,argv[optind]);}
}编译上述程序并执行结果输入选项及参数正确的情况dzlab:~/test/test#./opt -a100 -b 200 -c 300 -d
opt a optarg 100 optind 2 argv[optind] -b
opt b optarg 200 optind 4 argv[optind] -c
opt c optarg 300 optind 6 argv[optind] -d
opt d optarg (null) optind 7 argv[optind] (null)或者这样的选项格式(注意区别)dzlab:~/test/test#./opt -a100 -b200 -c300 -d
opt a optarg 100 optind 2 argv[optind] -b200
opt b optarg 200 optind 3 argv[optind] -c300
opt c optarg 300 optind 4 argv[optind] -d
opt d optarg (null) optind 5 argv[optind] (null)选项a是可选参数这里不带参数也是正确的dzlab:~/test/test#./opt -a -b 200 -c 300 -d
opt a optarg (null) optind 2 argv[optind] -b
opt b optarg 200 optind 4 argv[optind] -c
opt c optarg 300 optind 6 argv[optind] -d
opt d optarg (null) optind 7 argv[optind] (null)输入选项参数错误的情况dzlab:~/test/test#./opt -a 100 -b 200 -c 300 -d
opt a optarg (null) optind 2 argv[optind] 100
opt b optarg 200 optind 5 argv[optind] -c
opt c optarg 300 optind 7 argv[optind] -d
opt d optarg (null) optind 8 argv[optind] (null)导致解析错误第一个 optarg null实际输入参数 100由于格式不正确造成的(可选参数格式固定)参数丢失也会导致错误c选项是必须有参数的不加参数提示错误如下dzlab:~/test/test#./opt -a -b 200 -c
opt a optarg (null) optind 2 argv[optind] -b
opt b optarg 200 optind 4 argv[optind] -c
./opt: optionrequires an argument -- c
opt ? optarg (null) optind 5 argv[optind] (null)这种情况optstring 中第一个字母不是:如果在 optstring 中第一个字母加:则最后丢失参数的那个选项 opt 返回的是:不是?并且没有提示错误信息这里不再列出。命令行选项未定义-e选项未在optstring中定义会报错dzlab:~/test/test#./opt -a -b 200 -e
opt a optarg (null) optind 2 argv[optind] -b
opt b optarg 200 optind 4 argv[optind] -e
./opt: invalidoption -- e
opt ? optarg (null) optind 5 argv[optind] (null)到这里应该已经把getopt函数的功能讲解清楚了吧下边来说说 getopt_long 函数getopt_long 函数包含了 getopt 函数的功能并且还可以指定长参数(或者说长选项)与 getopt 函数对比getopt_long 比其多了两个参数三、getopt_long函数1、定义int getopt_long(int argc, char * const argv[], const char *optstring,const struct option *longopts,int *longindex);2、描述包含 getopt 功能增加了解析长选项的功能如--prefix --help3、参数longopts 指明了长参数的名称和属性
longindex 如果longindex非空它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述即是longopts的下标值4、返回对于短选项返回值同getopt函数对于长选项如果flag是NULL返回val否则返回0对于错误情况返回值同getopt函数5、struct optionstruct option {
const char *name; /* 参数名称 */
int has_arg; /* 指明是否带有参数 */
int *flag; /* flagNULL时,返回value;不为空时,*flagval,返回0 */
int val; /* 用于指定函数找到选项的返回值或flag非空时指定*flag的值 */
}; 6、参数说明has_arg 指明是否带参数值其数值可选
no_argument 表明长选项不带参数如--name, --help
required_argument 表明长选项必须带参数如--prefix /root或 --prefix/root
optional_argument 表明长选项的参数是可选的如--help或 –prefix/root其它都是错误接着看一下实例操作会更加深刻地理解7、实例int main(intargc, char *argv[])
{int opt;int digit_optind 0;int option_index 0;char *string a::b:c:d;static struct option long_options[] { {reqarg, required_argument,NULL, r},{optarg, optional_argument,NULL, o},{noarg, no_argument, NULL,n},{NULL, 0, NULL, 0},}; while((opt getopt_long_only(argc,argv,string,long_options,option_index))! -1){ printf(opt %c\t\t, opt);printf(optarg %s\t\t,optarg);printf(optind %d\t\t,optind);printf(argv[optind] %s\t\t, argv[optind]);printf(option_index %d\n,option_index);}
}编译上述程序并执行结果正确输入长选项的情况dzlab:~/test/test#./long --reqarg 100 --optarg200 --noarg
opt r optarg 100 optind 3 argv[optind] --optarg200 option_index 0
opt o optarg 200 optind 4 argv[optind] --noarg option_index 1
opt n optarg (null) optind 5 argv[optind] (null) option_index 2或者这种方式dzlab:~/test/test#./long –reqarg100 --optarg200 --noarg
opt r optarg 100 optind 2 argv[optind] --optarg200 option_index 0
opt o optarg 200 optind 3 argv[optind] --noarg option_index 1
opt n optarg (null) optind 4 argv[optind] (null) option_index 2可选选项可以不给参数dzlab:~/test/test#./long --reqarg 100 --optarg --noarg
opt r optarg 100 optind 3 argv[optind] --optarg option_index 0
opt o optarg (null) optind 4 argv[optind] --noarg option_index 1
opt n optarg (null) optind 5 argv[optind] (null) option_index 2输入长选项错误的情况dzlab:~/test/test#./long --reqarg 100 --optarg 200 --noarg
opt r optarg 100 optind 3 argv[optind] --optarg option_index 0
opt o optarg (null) optind 4 argv[optind] 200 option_index 1
opt n optarg (null) optind 6 argv[optind] (null) option_index 2这时虽然没有报错但是第二项中 optarg 参数没有正确解析出来(格式应该是 —optarg200)必须指定参数的选项如果不给参数同样解析错误如下dzlab:~/test/test#./long --reqarg --optarg200 --noarg
opt r optarg --optarg200 optind 3 argv[optind] --noarg option_index 0
opt n optarg (null) optind 4 argv[optind] (null) option_index 2长选项的举例说明暂且就这么多吧其它如选项错误、缺参数、格式不正确的情况自己再试验一下。四、getopt_long_only函数getopt_long_only 函数与 getopt_long 函数使用相同的参数表在功能上基本一致只是 getopt_long 只将 --name 当作长参数但 getopt_long_only 会将 --name 和 -name 两种选项都当作长参数来匹配。getopt_long_only 如果选项 -name 不能在 longopts 中匹配但能匹配一个短选项它就会解析为短选项。