linux选项解释-getopt和getopt_long函数...
时间:2010-08-15 来源:dongliqiang2006
Linux选项解释-getopt和getopt_long函数
一、命令行简介
解释分析命令行通常是所以程序的第一个任务,C语言通过argc和argv参数来访问它的命令行参数。
最简单的命令行处理技术可以通过if判断来表示,如下例:
if(argc>1 &&argv[1][0] == ‘ - ‘ &&argv[1][1] == ‘ h ’ ) //判断命令行参数是否为-n
{
do _ some thing();
}
这样处理简单有序的命令行还可以,对于复杂的命令行处理显得有心无力,于是GNU提供两个函数专门用来处理命令行参数:getopt和getopt_long。
二、getopt函数
getopt()函数声明如下:
C代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://lhg803.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://lhg803.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%23include%20%3Cunistd.h%3E%0A%0AInt%20getopt(int%20argc%2C%20char%20*const%20argv%5B%5D%2C%20const%20char%20*optstring)%3B%0A%0Aextern%20char%20*optarg%3B%0A%0Aextern%20int%20optind%2C%20opterr%2C%20optopt%3B%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">- #include <unistd.h>
- Int getopt(int argc, char * const argv[], const char *optstring);
- extern char *optarg;
- extern int optind, opterr, optopt;
#include <unistd.h> Int getopt(int argc, char *const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt;
说 明:函数中的argc和argv通常直接从main()到两个参数传递而来。optsting是选项参数组成的字符串,如果该字符串里任一字母后有冒号, 那么这个选项就要求有参数,optarg就是选项参数。optind是当前索引,optopt用于当发现无效选项字符的时候,getopt函数或者返回 “?”或者返回“:”字符,并且optopt包含了所发现的无效选项字符。
如果optstring参数的第一个字符是冒号,那么getopt会根据错误情况返回不同的字符,当错误是无效选项,getopt返回“?”,当错误是缺少选项参数,getopt返回“:”。
注:GNU getopt()第三个特点是optstring中的选项字符后面接两个冒号,就允许该选项有可选的选项参数。在选项参数不存在的情况下,GNU getopt()返回选项字符并将optarg设置为NULL。
例子:
C代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://lhg803.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://lhg803.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%23include%20%3Cstdio.h%3E%0A%0A%23include%20%3Cunistd.h%3E%0A%0A%23include%20%3Cgetopt.h%3E%0A%0Achar%20*para%20%3D%20%22%3Aab%3Ac%22%3B%0A%0Aint%20main(int%20argc%2C%20char%20*argv%5B%5D)%0A%0A%7B%0A%0A%20%20%20%20%20int%20oc%20%3D%20-1%3B%0A%0A%20%20%20%20%20char%20*b_input%20%3D%20NULL%3B%0A%0A%20%20%20%20%20while((oc%20%3D%20getopt(argc%2C%20argv%2C%20para))%20!%3D%20-1)%0A%0A%20%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20switch(oc)%0A%0A%20%20%20%20%20%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%20case%20'a'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22input%20para%20is%20a%5Cn%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%20case%20'b'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20b_input%20%3D%20optarg%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22input%20para%20is%20b%2Cand%20optarg%20is%20%25s%5Cn%22%2C%20b_input)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%20case%20'c'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22input%20para%20is%20c%5Cn%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%20case%20'%3A'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22option%20%25c%20requires%20an%20argument%5Cn%22%2Coptopt)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%20case%20'%3F'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20default%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22option%20%25c%20is%20invalid%3Aignored%5Cn%22%2Coptopt)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%7D%0A%0A%20%20%20%20%20return%200%3B%0A%0A%7D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">- #include <stdio.h>
- #include <unistd.h>
- #include <getopt.h>
- char *para = ":ab:c" ;
- int main( int argc, char *argv[])
- {
- int oc = -1;
- char *b_input = NULL;
- while ((oc = getopt(argc, argv, para)) != -1)
- {
- switch (oc)
- {
- case 'a' :
- printf("input para is a\n" );
- break ;
- case 'b' :
- b_input = optarg;
- printf("input para is b,and optarg is %s\n" , b_input);
- break ;
- case 'c' :
- printf("input para is c\n" );
- break ;
- case ':' :
- printf("option %c requires an argument\n" ,optopt);
- break ;
- case '?' :
- default :
- printf("option %c is invalid:ignored\n" ,optopt);
- break ;
- }
- }
- return 0;
- }
#include <stdio.h> #include <unistd.h> #include <getopt.h> char *para = ":ab:c"; int main(int argc, char *argv[]) { int oc = -1; char *b_input = NULL; while((oc = getopt(argc, argv, para)) != -1) { switch(oc) { case 'a': printf("input para is a\n"); break; case 'b': b_input = optarg; printf("input para is b,and optarg is %s\n", b_input); break; case 'c': printf("input para is c\n"); break; case ':': printf("option %c requires an argument\n",optopt); break; case '?': default: printf("option %c is invalid:ignored\n",optopt); break; } } return 0; }
编译:
[root@heguangwu projects]# gcc -o getopt_ex getopt_ex.c
运行:
[root@heguangwu projects]# ./getopt_ex -a
input para is a
[root@heguangwu projects]# ./getopt_ex -a -b
input para is a
option b requires an argument
[root@heguangwu projects]# ./getopt_ex -d
option d is invalid:ignored
三、getopt_long函数
getopt_long用来处理长选项,使用 man 3 getopt_long ,得到其声明如下:
C代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://lhg803.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://lhg803.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%23include%20%3Cgetopt.h%3E%0A%0Aint%20getopt_long(int%20argc%2C%20char%20*%20const%20argv%5B%5D%2C%20const%20char%20*optstring%2C%20%0A%0Aconst%20struct%20option%20*longopts%2C%20int%20*longindex)%3B%0A%0Aint%20getopt_long_only(int%20argc%2C%20char%20*%20const%20argv%5B%5D%2C%20const%20char%20*optstring%2C%20%0A%0Aconst%20struct%20option%20*longopts%2C%20int%20*longindex)%3B%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">- #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);
#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相同,下一个参数是指向数组的指针,这个数组是option结构数组,option结构称为长选项表,其声明如下:
C代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://lhg803.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://lhg803.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=struct%20option%0A%20%7B%0A%0A%20%20%20%20const%20char%20*name%3B%0A%0A%20%20%20%20int%20%20has_arg%3B%0A%0A%20%20%20%20int%20%20*flag%3B%0A%0A%20%20%20%20int%20%20val%3B%0A%0A%7D%3B%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">- struct option
- {
- const char *name;
- int has_arg;
- int *flag;
- int val;
- };
struct option { const char *name; int has_arg; int *flag; int val; };
结构中的元素解释如下:
const char *name :选项名,前面没有短横线
int has_arg:描述长选项是否有参数,其值见下表
符号常量 |
数值 |
含义 |
no_argument required_argument optional_argument |
0 1 2 |
选项没有参数 选项需要参数 选项参数是可选的 |
int *flag:
如果该指针为NULL,那么getopt_long返回val字段的值;
如果该指针不为NULL,那么会使得它所指向的结构填入val字段的值,同时getopt_long返回0
int val:
如果flag是NULL,那么val通常是个字符常量,如果短选项和长选项一致,那么该字符就应该与optstring中出现的这个选项的参数相同;
C代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://lhg803.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://lhg803.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%23include%20%3Cstdio.h%3E%0A%0A%23include%20%3Cunistd.h%3E%0A%0A%23include%20%3Cgetopt.h%3E%0A%0Achar%20*para%20%3D%20%22%3Aab%3Acf%3Av%22%3B%0A%0Aint%20do_all%20%3D%200%3B%0A%0Aint%20do_help%20%3D%200%3B%20%0A%0Aint%20do_version%20%3D%200%3B%0A%0Achar%20*file%20%3D%20NULL%3B%0A%0Astruct%20option%20longopt%5B%5D%20%3D%20%0A%0A%7B%0A%0A%20%20%20%20%20%7B%22all%22%2C%20no_argument%2C%20%26do_all%2C%201%7D%2C%0A%0A%20%20%20%20%20%7B%22file%22%2C%20required_argument%2C%20NULL%2C%20'f'%7D%2C%0A%0A%20%20%20%20%20%7B%22help%22%2C%20no_argument%2C%20%26do_help%2C%201%7D%2C%0A%0A%20%20%20%20%20%7B%22version%22%2C%20no_argument%2C%20%26do_version%2C%201%7D%2C%0A%0A%20%20%20%20%20%7B%22bob%22%2C%20required_argument%2C%20NULL%2C%20'b'%7D%2C%0A%0A%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%2C%0A%0A%7D%3B%0A%0Aint%20main(int%20argc%2C%20char%20*argv%5B%5D)%0A%0A%7B%0A%0A%20%20%20%20int%20oc%20%3D%20-1%3B%0A%0A%20%20%20%20char%20*b_input%20%3D%20NULL%3B%0A%0A%20%20%20%20while((oc%20%3D%20getopt_long(argc%2C%20argv%2C%20para%2C%20longopt%2C%20NULL))%20!%3D%20-1)%0A%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20switch(oc)%0A%0A%20%20%20%20%20%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20case%20'a'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22input%20para%20is%20a%5Cn%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20case%20'b'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20b_input%20%3D%20optarg%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22input%20para%20is%20b%2Cand%20optarg%20is%20%25s%5Cn%22%2C%20b_input)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20case%20'c'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22input%20para%20is%20c%5Cn%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20case%20'v'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22input%20para%20is%20v%5Cn%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20case%20'f'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22input%20para%20is%20f%5Cn%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20file%20%3D%20%22hello%20world%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20case%200%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20case%20'%3A'%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22option%20%25c%20requires%20an%20argument%5Cn%22%2Coptopt)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20case%20'%3F'%3A%0A%0A%20%20%20%20%20%20%20%20%20default%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22option%20%25c%20is%20invalid%3Aignored%5Cn%22%2Coptopt)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%7D%0A%0A%20%20%20%20%20printf(%22do_all%20is%20%25d%5Cn%22%2Cdo_all)%3B%0A%0A%20%20%20%20%20printf(%22do_help%20is%20%25d%5Cn%22%2Cdo_help)%3B%0A%0A%20%20%20%20%20printf(%22do_version%20is%20%25d%5Cn%22%2Cdo_version)%3B%0A%0A%20%20%20%20%20printf(%22do_file%20is%20%25s%5Cn%22%2Cfile)%3B%0A%0A%20%20%20%20%20printf(%22bob%20is%20%25s%5Cn%22%2C%20b_input)%3B%0A%0A%20%20%20%20%20return%200%3B%0A%0A%7D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
- #include <stdio.h>
- #include <unistd.h>
- #include <getopt.h>
- char *para = ":ab:cf:v" ;
- int do_all = 0;
- int do_help = 0;
- int do_version = 0;
- char *file = NULL;
- struct option longopt[] =
- {
- {"all" , no_argument, &do_all, 1},
- {"file" , required_argument, NULL, 'f' },
- {"help" , no_argument, &do_help, 1},
- {"version" , no_argument, &do_version, 1},
- {"bob" , required_argument, NULL, 'b' },
- {0, 0, 0, 0},
- };
- int main( int argc, char *argv[])
- {
- int oc = -1;
- char *b_input = NULL;
- while ((oc = getopt_long(argc, argv, para, longopt, NULL)) != -1)
- {
- switch (oc)
- {
- case 'a' :
- printf("input para is a\n" );
- break ;
- case 'b' :
- b_input = optarg;
- printf("input para is b,and optarg is %s\n" , b_input);
- break ;
- case 'c' :
- printf("input para is c\n" );
- break ;
- case 'v' :
- printf("input para is v\n" );
- break ;
- case 'f' :
- printf("input para is f\n" );
- file = "hello world" ;
- break ;
- case 0:
- break ;
- case ':' :
- printf("option %c requires an argument\n" ,optopt);
- break ;
- case '?' :
- default :
- printf("option %c is invalid:ignored\n" ,optopt);
- break ;
- }
- }
- printf("do_all is %d\n" ,do_all);
- printf("do_help is %d\n" ,do_help);
- printf("do_version is %d\n" ,do_version);
- printf("do_file is %s\n" ,file);
- printf("bob is %s\n" , b_input);
- return 0;
- }
#include <stdio.h> #include <unistd.h> #include <getopt.h> char *para = ":ab:cf:v"; int do_all = 0; int do_help = 0; int do_version = 0; char *file = NULL; struct option longopt[] = { {"all", no_argument, &do_all, 1}, {"file", required_argument, NULL, 'f'}, {"help", no_argument, &do_help, 1}, {"version", no_argument, &do_version, 1}, {"bob", required_argument, NULL, 'b'}, {0, 0, 0, 0}, }; int main(int argc, char *argv[]) { int oc = -1; char *b_input = NULL; while((oc = getopt_long(argc, argv, para, longopt, NULL)) != -1) { switch(oc) { case 'a': printf("input para is a\n"); break; case 'b': b_input = optarg; printf("input para is b,and optarg is %s\n", b_input); break; case 'c': printf("input para is c\n"); break; case 'v': printf("input para is v\n"); break; case 'f': printf("input para is f\n"); file = "hello world"; break; case 0: break; case ':': printf("option %c requires an argument\n",optopt); break; case '?': default: printf("option %c is invalid:ignored\n",optopt); break; } } printf("do_all is %d\n",do_all); printf("do_help is %d\n",do_help); printf("do_version is %d\n",do_version); printf("do_file is %s\n",file); printf("bob is %s\n", b_input); return 0; }
执行的结果: 只显示关键结果
[root@heguangwu projects]# ./opt_ex2 -a
input para is a
[root@heguangwu projects]# ./opt_ex2 --all
do_all is 1
[root@heguangwu projects]# ./opt_ex2 -f h
input para is f
do_file is hello world
[root@heguangwu projects]# ./opt_ex2 --bob aa
input para is b,and optarg is aa
bob is aa
[root@heguangwu projects]# ./opt_ex2 -b aa
input para is b,and optarg is aa