相关库函数 #include <getopt.h> int getopt(int argc, char * const argv[],const char *optstring); extern char *optarg; extern int optind, opterr, optopt; 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);
eg1:
#include <stdio.h> #include <getopt.h> int main(int argc,char *argv[]) { int opt; char *optstring = "a:b:c:de"; // 分别代表-a,-b,-c,-d,-e命令行参数,其中带:的表示参数可以指定值,故de不能指定值 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; } #./test_arg -a 1 -b 2 -c 3 -d -e opt = a optarg = 1 optind = 3 argv[optind-1] = 1 opt = b optarg = 2 optind = 5 argv[optind-1] = 2 opt = c optarg = 3 optind = 7 argv[optind-1] = 3 opt = d optarg = (null) optind = 8 argv[optind-1] = -d opt = e optarg = (null) optind = 9 argv[optind-1] = -e
getopt_long函数在getopt函数的基础上,增加了处理长选项(--)的功能。 因此多了2个参数:const struct option *longopts, int *longindex struct option { const char *name; /* has_arg can't be an enum because some compilers complain about type mismatches in all the code that assumes it is an int. */ int has_arg; int *flag; int val; }; /* Names for the values of the `has_arg' field of `struct option'. */ # define no_argument 0 # define required_argument 1 # define optional_argument 2 has_arg 指明是否带参数值,其数值可选: no_argument (即 0) 表明这个长参数不带参数(即不带数值,如:--name) required_argument (即 1) 表明这个长参数必须带参数(即必须带数值,如:--name Bob) optional_argument(即2)表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可) longindex为longopts对应的下标 #include <stdio.h> #include <getopt.h> //#include <unistd.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]); // // } const char *short_options = "a:b:c:d"; static struct option long_options[] ={ {"no_arg",no_argument,NULL,'n'}, {"required_arg",required_argument,NULL,'r'}, {"optional_arg",optional_argument,NULL,'o'}, {"test",1,NULL,'t'} }; int opt = -1; int option_index = -1; while((opt = getopt_long(argc,argv,short_options,long_options,&option_index)) != -1){ printf("opt = %c,%d\n",opt,opt); printf("optarg = %s\n",optarg); printf("optind = %d\n",optind); printf("argv[optind-1]=%s(short_arg)\n",argv[optind-1]); printf("long_option_index=%d\n\n",option_index); } return 0; } eg2: [root@localhost C]# ./test_arg -a 1 -b 2 -c 3 -d --no_arg --required_arg 1 --optional_arg opt = a,97 optarg = 1 optind = 3 argv[optind-1]=1(short_arg) long_option_index=-1 opt = b,98 optarg = 2 optind = 5 argv[optind-1]=2(short_arg) long_option_index=-1 opt = c,99 optarg = 3 optind = 7 argv[optind-1]=3(short_arg) long_option_index=-1 opt = d,100 optarg = (null) optind = 8 argv[optind-1]=-d(short_arg) long_option_index=-1 opt = n,110 optarg = (null) optind = 9 argv[optind-1]=--no_arg(short_arg) long_option_index=0 opt = r,114 optarg = 1 optind = 11 argv[optind-1]=1(short_arg) long_option_index=1 opt = o,111 optarg = (null) optind = 12 argv[optind-1]=--optional_arg(short_arg) long_option_index=2 最后说说getopt_long_only函数,它与getopt_long函数使用相同的参数表,在功能上基本一致,只是getopt_long只将--name当作长参数,但getopt_long_only会将--name和-name两种选项都当作长参数来匹配