加入收藏 | 设为首页 | 会员中心 | 我要投稿 济南站长网 (https://www.0531zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Linux getopt()函数 getopt_long()函数---转

发布时间:2021-01-25 13:49:06 所属栏目:Linux 来源:网络整理
导读:http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f get_opt()函数: 函数原型:: #include int getopt(int argc,char * const argv[],const char *optstring); extern char *optarg;extern int optind,opterr,optopt; 用法见右边栏 1.参数

http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f

get_opt()函数:

函数原型::

#include int getopt(int argc,char * const argv[],const char *optstring);

extern char *optarg;extern int optind,opterr,optopt;

用法见右边栏

1.参数说明:

optstring:选项字母组成的字串。如果该字串里的任一字符后面有冒号,那么这个选项就要求有选项参数。

char *optarg:当前选项参数字串(如果有)。

int optind:argv的当前索引值。当getopt()在while循环中使用时,循环结束后,剩下的字串视为操作数,在argv[optind]至argv[argc-1]中可以找到。

int opterr:这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。

int optopt:当发现无效选项字符之时,getopt()函数或返回'?'字符,或返回':'字符,并且optopt包含了所发现的无效选项字符。

2.更改getopt()函数的出错信息输出与否:

a. 在调用getopt()之前,将opterr设置为0,这样就可以在getopt()函数发现错误的时候强制它不输出任何消息。b. 在optstring参数的第一个字符前加冒号,那么getopt()函数就会保持沉默,并根据错误情况返回不同字符,如下:“无效选项” —— getopt()返回'?',并且optopt包含了无效选项字符(这是正常的行为)。“缺少选项参数” —— getopt()返回':',如果optstring的第一个字符不是冒号,那么getopt()返回'?',这会使得这种情况不能与无效选项的情况区分开。

/*get_opt function test

huasion 20090920

*/#include #include

int main( int argc,char ** argv){???int i,oc;int res = 0;char *b_opt_arg;char ec;

/*commen print argument*/printf("******************************************************n");?printf("usage: get_opt n ");

printf("argc=%dn",argc);for(i=0;i

?? switch(oc){?? case 'a':??? printf("case a optind = %d.n",optind);??? break;?? case 'b':??? printf("case b have a value,optarg = %s,optind = %d.n",optarg,optind);??? break;?? case 'c':??? printf("case c optind = %d.n",optind);??? break;?? case 'd':??? printf("case d have a value optarg = %s,optind);??? break;?? case '?':??? ec = (char)optopt;?????? printf("option '%c' invalid! optind = %d,.n",ec,optind);????? case ':':??? printf("getopt() return value :,you need a option value n");????? default:??? break;?? }

}printf("it is over,res = %d n",res);

}

**********************************分割线**********************************

getopt_long()函数

函数原型:

int getopt_long(int argc,const char *optstring,const struct option *longopts,int *longindex);

前三个参数的含义和getopt()函数一致,其余意义如下:

longopts: 长选项表地址 ,定义为:

?? struct option{??????????? const char *name;??????????? int has_arg;??????????? int *flag;??????????? int val;???? };

longindex: 如果没有设置为NULL,那么它就指向一个变量,这个变量会被赋值为寻找到的长选项在longopts中的索引值,这可以用于错误诊断。

const char *name: 这是选项名,前面没有短横线。譬如"help"、"verbose"之类。int has_arg:描述了选项是否有选项参数。如果有,是哪种类型的参数,此时,它的值一定是下表中的一个。符号常量 数值 含义?no_argument 0 选项没有参数?required_argument 1 选项需要参数?optional_argument 2 选项参数可选

int *flag如 果这个指针为NULL,那么getopt_long()返回该结构val字段中的数值。如果该指针不为NULL,getopt_long()会使得它所指 向的变量中填入val字段中的数值,并且getopt_long()返回0。如果flag不是NULL,但未发现长选项,那么它所指向的变量的数值不变。int val这 个值是发现了长选项时的返回值,或者flag不是NULL时载入*flag中的值。典型情况下,若flag不是NULL,那么val是个真/假值,譬如1 或0;另一方面,如果flag是NULL,那么val通常是字符常量,若长选项与短选项一致,那么该字符常量应该与optstring中出现的这个选项的 参数相同。每个长选项在长选项表中都有一个单独条目,该条目里需要填入正确的数值。数组中最后的元素的值应该全是0。数组不需要排序,getopt_long()会进行线性搜索。但是,根据长名字来排序会使程序员读起来更容易。

/*get_opt function test

huasion 20090920

*/#include #include #include

int main( int argc,oc;int res = 0;char *b_opt_arg;char ec;

int do_name,do_gf_name;char *l_opt_arg;

struct option longopts[] = {?? { "name",no_argument,&do_name,1 },?? { "gf_name",&do_gf_name,?? { "love",required_argument,NULL,'l' },?? { 0,0},};

/*commen print argument*/printf("******************************************************n");?printf("usage: get_opt n ");

printf("argc=%dn",argv[i]);printf("******************************************************n");/*over*/while((oc = getopt_long(argc,":l:",longopts,NULL)) != -1){switch (oc){case 'l':?? l_opt_arg = optarg;?? printf("Our love is %s!n",l_opt_arg);break;case 0:?? printf("getopt_long()设置变量 : do_name = %dn",do_name);?? printf("getopt_long()设置变量 : do_gf_name = %dn",do_gf_name);break;}}printf("it is over,res);

}

#include #include

int do_name,do_gf_name;char *l_opt_arg;

struct option longopts[] = {{ "name",'n' },{ "gf_name",'g' },{ "love",{ 0,};

int main(int argc,char *argv[]){int c;while((c = getopt_long(argc,":ngl:",NULL)) != -1){

//其中,选项以及其有无参数由":ngl:"决定 而不是在option longopts[] 表中的值决定

switch (c){case 'n':printf("My name is LYR.n");break;case 'g':printf("Her name is BX.n");break;case 'l':l_opt_arg = optarg;printf("Our love is %s!n",l_opt_arg);break;}}return 0;}

(编辑:济南站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读