getopt和getopt_long函数
来源:互联网 发布:管家婆 远程数据库 编辑:程序博客网 时间:2024/05/16 11:08
http://blog.csdn.net/cashey1991/article/details/7942809
平时在写程序时常常需要对命令行参数进行处理,当命令行参数个数较多时,如果按照顺序一个一个定义参数含义很容易造成混乱,而且如果程序只按顺序处理参数的话,一些“可选参数”的功能将很难实现。
在Linux中,我们可以使用getopt、getopt_long、getopt_long_only来对这个问题进行处理。
- #include <unistd.h>
- int getopt(int argc, char * const argv[],
- const char *optstring);
- extern char *optarg;
- extern int optind, opterr, optopt;
- #include <getopt.h>
- 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);
从最简单的getopt讲起,getopt函数的前两个参数,就是main函数的argc和argv,这两者直接传入即可,要考虑的就只剩下第三个参数。
optstring的格式举例说明比较方便,例如:
char *optstring = "abcd:";
上面这个optstring在传入之后,getopt函数将依次检查命令行是否指定了 -a, -b, -c及 -d(这需要多次调用getopt函数,直到其返回-1),当检查到上面某一个参数被指定时,函数会返回被指定的参数名称(即该字母)
最后一个参数d后面带有冒号,: 表示参数d是可以指定值的,如 -d 100 或 -d user。
optind表示的是下一个将被处理到的参数在argv中的下标值。
如果指定opterr = 0的话,在getopt、getopt_long、getopt_long_only遇到错误将不会输出错误信息到标准输出流。
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- int main(int argc, char *argv[])
- {
- int opt;
- char *optstring = "a:b:c:d";
- while ((opt = getopt(argc, argv, optstring)) != -1)
- {
- printf("opt = %c\n", opt);
- printf("optarg = %s\n", optarg);
- printf("optind = %d\n", optind);
- printf("argv[optind - 1] = %s\n\n", argv[optind - 1]);
- }
- return 0;
- }
- cashey@ubuntu:~/Desktop/getopt$ ./test_getopt -a 100 -b 200 -c admin -d
- opt = a
- optarg = 100
- optind = 3
- argv[optind - 1] = 100
- opt = b
- optarg = 200
- optind = 5
- argv[optind - 1] = 200
- opt = c
- optarg = admin
- optind = 7
- argv[optind - 1] = admin
- opt = d
- optarg = (null)
- optind = 8
- argv[optind - 1] = -d
int getopt(int argc, char * const argv[],
const char *optstring);
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
在这里,longopts指向的是一个由option结构体组成的数组,那个数组的每个元素,指明了一个“长参数”(即形如--name的参数)名称和性质:
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
name 是参数的名称
has_arg 指明是否带参数值,其数值可选:
no_argument (即 0) 表明这个长参数不带参数(即不带数值,如:--name)
required_argument (即 1) 表明这个长参数必须带参数(即必须带数值,如:--name Bob)
optional_argument(即2)表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
flag 当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0
val 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值。
另一个参数longindex,如果longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值。
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <getopt.h>
- int
- main(int argc, char **argv)
- {
- int opt;
- int digit_optind = 0;
- int option_index = 0;
- char *optstring = "a:b:c:d";
- static struct option long_options[] = {
- {"reqarg", required_argument, NULL, 'r'},
- {"noarg", no_argument, NULL, 'n'},
- {"optarg", optional_argument, NULL, 'o'},
- {0, 0, 0, 0}
- };
- while ( (opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1)
- {
- printf("opt = %c\n", opt);
- printf("optarg = %s\n", optarg);
- printf("optind = %d\n", optind);
- printf("argv[optind - 1] = %s\n", argv[optind - 1]);
- printf("option_index = %d\n", option_index);
- }
- return 0;
- }
编译运行以上程序并运行,可以得到以下结果:
- cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long -a 100 --reqarg 100 --nonarg
- opt = a
- optarg = 100
- optind = 3
- argv[optind - 1] = 100
- option_index = 0
- opt = r
- optarg = 100
- optind = 5
- argv[optind - 1] = 100
- option_index = 0
- ./test_getopt_long: unrecognized option '--nonarg'
- opt = ?
- optarg = (null)
- optind = 6
- argv[optind - 1] = --nonarg
- option_index = 0
当所给的参数存在问题时,opt(即函数返回值是'?'),如:
- cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long -a
- ./test_getopt_long: option requires an argument -- 'a'
- opt = ?
- optarg = (null)
- optind = 2
- argv[optind - 1] = -a
- option_index = 0
- cashey@ubuntu:~/Desktop/getopt$ ./test_getopt_long --reqarg
- ./test_getopt_long: option '--reqarg' requires an argument
- opt = ?
- optarg = (null)
- optind = 2
- argv[optind - 1] = --reqarg
最后说说getopt_long_only函数,它与getopt_long函数使用相同的参数表,在功能上基本一致,只是getopt_long只将--name当作长参数,但getopt_long_only会将--name和-name两种选项都当作长参数来匹配。在getopt_long在遇到-name时,会拆解成-n -a -m -e到optstring中进行匹配,而getopt_long_only只在-name不能在longopts中匹配时才将其拆解成-n -a -m -e这样的参数到optstring中进行匹配。
http://blog.csdn.net/mamong/article/details/20648575
getopt被用来解析命令行选项参数。
#include <unistd.h>
extern char *optarg; //选项的参数指针
extern int optind, //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
extern int opterr, //当opterr=0时,getopt不向stderr输出错误信息。
extern int optopt; //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt中,getopt返回'?’、
int getopt(int argc, char * const argv[], const char *optstring);
调用一次,返回一个选项。 在命令行选项参数再也检查不到optstring中包含的选项时,返回-1,同时optind储存第一个不包含选项的命令行参数。
首先说一下什么是选项,什么是参数。
字符串optstring可以下列元素,
1.单个字符,表示选项,
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
3 单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
getopt处理以'-’开头的命令行参数,如optstring="ab:c::d::",命令行为getopt.exe -a -b host -ckeke -d haha
在这个命令行参数中,-a和-h就是选项元素,去掉'-',a,b,c就是选项。host是b的参数,keke是c的参数。但haha并不是d的参数,因为它们中间有空格隔开。
还要注意的是默认情况下getopt会重新排列命令行参数的顺序,所以到最后所有不包含选项的命令行参数都排到最后。
如getopt.exe -a ima -b host -ckeke -d haha, 都最后命令行参数的顺序是: -a -b host -ckeke -d ima haha
如果optstring中的字符串以'+'加号开头或者环境变量POSIXLY_CORRE被设置。那么一遇到不包含选项的命令行参数,getopt就会停止,返回-1。
#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(int argc, char **argv){int result; opterr = 0; //使getopt不行stderr输出错误信息 while( (result = getopt(argc, argv, "ab:c::")) != -1 ){switch(result){case 'a':printf("option=a, optopt=%c, optarg=%s\n", optopt, optarg);break;case 'b':printf("option=b, optopt=%c, optarg=%s\n", optopt, optarg);break;case 'c':printf("option=c, optopt=%c, optarg=%s\n", optopt, optarg);break;case '?':printf("result=?, optopt=%c, optarg=%s\n", optopt, optarg);break;default:printf("default, result=%c\n",result);break;}printf("argv[%d]=%s\n", optind, argv[optind]);}printf("result=-1, optind=%d\n", optind); //看看最后optind的位置 for(result = optind; result < argc; result++)printf("-----argv[%d]=%s\n", result, argv[result]); //看看最后的命令行参数,看顺序是否改变了哈。for(result = 1; result < argc; result++)printf("\nat the end-----argv[%d]=%s\n", result, argv[result]);return 0;}
unistd里有个 optind 变量,每次getopt后,这个索引指向argv里当前分析的字符串的下一个索引,因此
argv[optind]就能得到下个字符串,通过判断是否以 '-'开头就可。下面是个测试程序
#include <stdio.h>#include <unistd.h>int main(int argc, char* argv[]){int tmp = 4;while( (tmp = getopt(argc, argv, "abck")) != -1 ){printf("-%c\t", tmp);int opt = optind ;while( opt < argc ){if ( argv[opt][0] != '-' ){printf("%s\t", argv[opt]);opt ++;}elsebreak;}printf("\n");}getchar();}
函数说明 getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错 信息,则只要将全域变量opterr设为0即可。
返回值 如果找到符合的参数则返回此参数字母,如果参数不包含在参数optstring 的选项字母则返回“?”字符,分析结束则返回-1。
范例
#include<stdio.h>#include<unistd.h>int main(int argc,char **argv){int ch;opterr = 0;while((ch = getopt(argc,argv,”a:bcde”))!= -1)switch(ch){case ‘a’:printf(“option a:’%s’\n”,optarg);break;case ‘b’:printf(“option b :b\n”);break;default:printf(“other option :%c\n”,ch);}printf(“optopt +%c\n”,optopt);}
执行 $./getopt –b
option b:b
$./getopt –c
other option:c
$./getopt –a
other option :?
$./getopt –a12345
option a:’12345’
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt和getopt_long函数
- getopt 和 getopt_long 函数
- SpringMVC上传文件MultipartFile
- 程序员面试金典 1.4 空格替换
- 杭电2122Ice_cream’s world III(kruskal算法与prim算法)
- Android开发学习之路--传感器之初体验
- Solr索引库l配置文件schema.xm要点
- getopt和getopt_long函数
- EasyDarwin开源流媒体服务器高性能设计之无锁队列
- JavaScript之面向对象的继承浅析2
- 最小生成树-克鲁斯卡尔方法(Kruskal)
- poj1463树形dp
- 编译android源码3---ubuntu安装jdk6
- 最详细的制作正式版10.11 OS X El Capitan 安装U盘的方法
- android webview 访问https页面 SslError 处理
- 【Linux 基础篇】之 rsync