getopt被用来解析命令行选项参数,就不用自己处理argv了。
头文件:#include
原型:int getopt(int argc, char * const argv[], const char *optstring);
参数:optstring - 选项字符串
单个字符,表示选项;单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg;单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。
例1:optstring为"a:b:cd::e"。对应到命令行就是-a ,-b ,-c ,-d, -e ;其中a b d后面有冒号,则后面必须跟一个参数;而c e后面没有冒号,所有不用跟参数。比如-a 11或-a11,11表示a的参数。
头文件与之相关变量(不用自己定义,直接使用):
extern char *optarg; //选项的参数指针
extern int optind, //下一个检索位置:下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
extern int opterr, //是否将错误信息输出到stderr:当opterr=0时,getopt不向stderr输出错误信息。
extern int optopt; //不在选项字符串optstring中的选项:当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt中,getop()返回'?’、
例1:
#include#include int main(int argc, char **argv) { int ch; while( (ch = getopt(argc, argv, "ab:c::")) != -1 ) { switch(ch) { case 'a': printf("option = a, optopt = %c, optarg = %sn", optopt, optarg); break; case 'b': printf("option = b, optopt = %c, optarg = %sn", optopt, optarg); break; case 'c': printf("option = c, optopt = %c, optarg = %sn", optopt, optarg); break; case '?': printf("option =? , optopt = %c, optarg = %sn", optopt, optarg); break; default: printf("default, ch = %cn",ch); break; } } return 0; }
例2:
#include#include int main(int argc, char **argv) { int ch; printf("optind:%d,opterr:%dn",optind,opterr); while( (ch = getopt(argc, argv, "ab:c::")) != -1 ) { printf("optind: %dn", optind); switch(ch) { case 'a': printf("option = a, optopt = %c, optarg = %sn", optopt, optarg); break; case 'b': printf("option = b, optopt = %c, optarg = %sn", optopt, optarg); break; case 'c': printf("option = c, optopt = %c, optarg = %sn", optopt, optarg); break; case '?': printf("option =? , optopt = %c, optarg = %sn", optopt, optarg); break; default: printf("default, ch = %cn",ch); break; } } return 0; }
又上面可以看出:optind和opterr的初始值都为1,opterr非零表示产生的错误要输出到stderr上。optind的初值为1是因为,main函数的那两个参数了,argc表示参数的个数,argv[]表示每个参数字符串,./test -a -b hello -cwlord,实际上真正的参数是用第二个-a 开始,也就是argv[1],所以optind的初始值为1;
2 getopt_long()头文件:#include
函数原型:int getopt_long(int argc, char * const argv[],const char *optstring, const struct option *longopts,int *longindex)
参数:optstring - 选项字符串
longopts - 是一个结构的实例
struct option { const char *name; int has_arg; int *flag; int val; }longindex - 如果longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值
#include#include #include static struct option long_options[] = { {"add", no_argument, NULL, 'a'}, {"back", required_argument, NULL, 'b'}, {"create", required_argument, NULL, 'c'}, {0, 0, 0, 0}, }; int main(int argc, char **argv) { int ch; int option_index = 0; printf("optind:%d,opterr:%dn",optind,opterr); while( (ch = getopt_long(argc, argv, "ab:c::",long_options, &option_index)) != -1 ) { printf("optind: %dn", optind); switch(ch) { case 'a': printf("option = a, optopt = %c, optarg = %sn", optopt, optarg); break; case 'b': printf("option = b, optopt = %c, optarg = %sn", optopt, optarg); break; case 'c': printf("option = c, optopt = %c, optarg = %sn", optopt, optarg); break; case '?': printf("option =? , optopt = %c, optarg = %sn", optopt, optarg); break; default: printf("default, ch = %cn",ch); break; } printf ("option %sn", long_options[option_index].name); } return 0; }



