getopt()/getopt_long()/getopt_long_only()

来源:互联网 发布:mac双系统win8 编辑:程序博客网 时间:2024/06/05 11:12

getopt_long()

头文件:

#include <getopt.h>

原型:

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

参数:

argc和argv :通常直接从main()的两个参数传递而来

optstring :

        1.单个字符,表示选项,

        2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。

        3 单个字符后跟两个冒号,表示该选项后可以有参数也可以没有参数。如果有参数,参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给              optarg。

        optstring是一个字符串,表示可以接受的参数。例如,"a:b:c:d:",表示可以接受的参数是a,b,c,d,其中,a和b参数后面跟有更多的参数值

        (例如:-a host -b name)

longopts:

           struct option {
                        const char *name;
                        int has_arg;
                        int *flag;
                        int val;
           };
           结构中的元素解释如下:
           const char *name: 选项名,前面没有短横线。譬如"help"、"verbose"之类。
           int has_arg:          描述长选项是否有选项参数,如果有,是哪种类型的参数,其值见下表:
                    符号常量                      数值            含义
                    no_argument               0             选项没有参数
                    required_argument      1             选项需要参数   参数输入格式为:--参数 值 或者 --参数=值
                    optional_argument       2             选项参数是可选的  参数输入格式只能为:--参数=值
          int *flag:
            如果该指针为NULL,那么getopt_long返回val字段的值;
            如果flag不是NULL,则将val值赋予flag所指向的内存,并且返回值设置为0。
          int val: 如果flag是NULL,那么val通常是个字符常量,如果短选项和长选项一致,那么该字符就应该与optstring中出现的这个选项的参数相同;

longindex:

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


optarg 全局变量

getopt.h中已经有定义

例:

#include <stdio.h>#include <getopt.h>char *l_opt_arg;char* const short_options = "nbl:";struct option long_options[] = {{ "name", 0, NULL, 'n' },{ "bf_name", 0, NULL, 'b' },{ "love", 1, NULL, 'l' },{ 0, 0, 0, 0},};int main(int argc, char *argv[]){int c;while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1){switch (c){case 'n':printf("My name is XL.\n");break;case 'b':printf("His name is ST.\n");break;case 'l':l_opt_arg = optarg;printf("Our love is %s!\n", l_opt_arg);break;default :printf("out:%c\n",c);break;}}return 0;}


$ ./getopt-long --nameMy name is XL.$ ./getopt-long --df_name./getopt-long: unrecognized option '--df_name'out:?$ ./getopt-long --bf_nameHis name is ST.$ ./getopt-long --love./getopt-long: option '--love' requires an argumentout:?$ ./getopt-long --love 1234Our love is 1234!$ ./getopt-long$ ./getopt-long -0./getopt-long: invalid option -- '0'

getopt()
头文件 
#include<unistd.h>
原型:

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

参数:argc和argv :通常直接从main()的两个参数传递而来

optstring:

中的指定的内容的意义(例如getopt(argc, argv, "ab:c:de::");)
1.单个字符,表示选项(如下例中的abcde各为一个选项)。
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg(如下例中的b:c:)。
3 单个字符后跟两个冒号,表示该选项后可以跟一个参数,也可以不跟。如果跟一个参数,参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(如上例中的e::,如果没有跟参数,则optarg = NULL)


getopt_long_only()

这个用法和上面的getopt_long完全一样,就是在输入长选项的时候可以不用输入--而使用-



原创粉丝点击