[转] getopt getopt_long函数


转自http://blog.csdn.net/cashey1991/article/details/7942809

0 getopt

#include <unistd.h> 

int getopt(int argc, char * const argv[], 

           const char *optstring); 

extern char *optarg; 

extern int optind, opterr, optopt;

       函数的前面2个参数就是main函数的argc和argv,直接传入即可。Optstring则表示接受参数的格式,1.单个字符,表示选项,(如上例中的abcde各为一个选项);2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。(如上例中的b:c:);3 单个字符后跟两个冒号,表示该选项后可以跟一个参数,也可以不跟。如果跟一个参数,参数必须紧跟在选项后不能以空格隔开。optind表示下一个将被处理的参数argv中的下标。getopt返回值即为采样到的参数的ASCII码。

#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; 

} 

$ ./test_getopt -a 100 -b 200 -c admin -

opt =

optarg = 100 

optind = 3 

argv[optind - 1] = 100 

 

opt =

optarg = 200 

optind = 5 

argv[optind - 1] = 200 

 

opt =

optarg = admin 

optind = 7 

argv[optind - 1] = admin 

 

opt =

optarg = (null) 

optind = 8 

argv[optind - 1] = -

1 getopt_long

所有的GNU/Linux程序都遵循一些命令行参数定义的约定。程序希望出现的参数可以分成两种:选项(options or flags)、其他类型的的参数。Options修饰了程序运行的方式,其他类型的参数则提供了输入(例如,输入文件的名称)。

对于options类型参数可以有两种方式:

    1)短选项(short options):顾名思义,就是短小参数。它们通常包含一个连字号和一个字母(大写或小写字母)。例如:-s,-h等。

    2)长选项(long options):长选项,包含了两个连字号和一些大小写字母组成的单词。例如,--size,--help等。

    *注:一个程序通常会提供包括short options和long options两种参数形式的参数。

       函数原型如下。

#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); 

       longopts指向的是一个由option结构体组成的数组,那个数组的每个元素,指明了一个“长参数”(即形如--name的参数)名称和性质:

struct option {

    const char *name;

    int         has_arg;

    int        *flag;

    int         val;

};

1) name  是参数的名称

2has_arg 指明是否带参数值,其数值可选:no_argument ( 0) 表明这个长参数不带参数(即不带数值,如:--namerequired_argument ( 1) 表明这个长参数必须带参数(即必须带数值,如:--name Boboptional_argument(即2)表明这个长参数后面带的参数是可选的,(即--name--name Bob均可)

3flag 当这个指针为空的时候,函数直接将val的数值从getopt_long的返回值返回出去,当它非空时,val的值会被赋到flag指向的整型数中,而函数返回值为0

4val 用于指定函数找到该选项时的返回值,或者当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; 

} 

./test_getopt_long -a 100 --reqarg 100 --nonarg 

opt =

optarg = 100 

optind = 3 

argv[optind - 1] = 100 

option_index = 0 

opt =

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 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章