Linux getopt()函数 getopt_long()函数---转

来源:互联网 发布:网络解决方案供应商 编辑:程序博客网 时间:2024/05/22 14:18

http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f

Linux getopt()函数 getopt_long()函数

get_opt()函数:

函数原型::

#include <unistd.h>

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

extern char *optarg;
extern int optind, opterr, optopt;

用法见右边栏

1.参数说明:

optstring:选项字母组成的字串。如果该字串里的任一字符后面有冒号,那么这个选项就要求有选项参数。

char *optarg:当前选项参数字串(如果有)。

int optind:argv的当前索引值。当getopt()在while循环中使用时,循环结束后,剩下的字串视为操作数,在argv[optind]至argv[argc-1]中可以找到。

int opterr:这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。


int optopt:当发现无效选项字符之时,getopt()函数或返回'?'字符,或返回':'字符,并且optopt包含了所发现的无效选项字符。

2.更改getopt()函数的出错信息输出与否:

a. 在调用getopt()之前,将opterr设置为0,这样就可以在getopt()函数发现错误的时候强制它不输出任何消息。
b. 在optstring参数的第一个字符前加冒号,那么getopt()函数就会保持沉默,并根据错误情况返回不同字符,如下:
“无效选项” —— getopt()返回'?',并且optopt包含了无效选项字符(这是正常的行为)。
“缺少选项参数” —— getopt()返回':',如果optstring的第一个字符不是冒号,那么getopt()返回'?',这会使得这种情况不能与无效选项的情况区分开。

 

/*get_opt function test

huasion 20090920

*/
#include <stdio.h>
#include <unistd.h>

 

int main( int argc, char ** argv)

  
int i,oc;
int res = 0;
char *b_opt_arg;
char ec;

/*commen print argument*/
printf("******************************************************\n"); 
printf("usage: get_opt \n ");

printf("argc=%d\n", argc);
for(i=0;i<argc;i++)
   printf("argv[%d]:%s\n", i, argv[i]);
printf("******************************************************\n");

/*over*/

while((oc=getopt(argc,argv,":ab:cd:")) != -1){

   switch(oc){
   case 'a':
    printf("case a optind = %d.\n",optind);
    break;
   case 'b':
    printf("case b have a value,optarg = %s ,optind = %d.\n",optarg,optind);
    break;
   case 'c':
    printf("case c optind = %d.\n",optind);
    break;
   case 'd':
    printf("case d have a value optarg = %s ,optind = %d.\n",optarg,optind);
    break;
   case '?':
    ec = (char)optopt;
   
    printf("option \'%c\' invalid! optind = %d, .\n",ec,optarg,optind);
   
   case ':':
    printf("getopt() return value : , you need a option value \n");
   
   default:
    break;
   }


}

printf("it is over, res = %d \n",res);

 

}

**********************************分割线**********************************

getopt_long()函数

函数原型:

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

前三个参数的含义和getopt()函数一致,其余意义如下:

longopts: 长选项表地址 ,定义为:

   struct option{
            const char *name;
            int has_arg;
            int *flag;
            int val;
     };

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

const char *name: 这是选项名,前面没有短横线。譬如"help"、"verbose"之类。
int has_arg:描述了选项是否有选项参数。如果有,是哪种类型的参数,此时,它的值一定是下表中的一个。符号常量 数值 含义 
no_argument 0 选项没有参数 
required_argument 1 选项需要参数 
optional_argument 2 选项参数可选

int *flag
如 果这个指针为NULL,那么getopt_long()返回该结构val字段中的数值。如果该指针不为NULL,getopt_long()会使得它所指 向的变量中填入val字段中的数值,并且getopt_long()返回0。如果flag不是NULL,但未发现长选项,那么它所指向的变量的数值不变。
int val
这 个值是发现了长选项时的返回值,或者flag不是NULL时载入*flag中的值。典型情况下,若flag不是NULL,那么val是个真/假值,譬如1 或0;另一方面,如果flag是NULL,那么val通常是字符常量,若长选项与短选项一致,那么该字符常量应该与optstring中出现的这个选项的 参数相同。

每个长选项在长选项表中都有一个单独条目,该条目里需要填入正确的数值。数组中最后的元素的值应该全是0。数组不需要排序,getopt_long()会进行线性搜索。但是,根据长名字来排序会使程序员读起来更容易。

 

 

/*get_opt function test

huasion 20090920

*/
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>

 


int main( int argc, char ** argv)

  
int i,oc;
int res = 0;
char *b_opt_arg;
char ec;

int do_name, do_gf_name;
char *l_opt_arg;

struct option longopts[] = {
   { "name", no_argument, &do_name, 1 },
   { "gf_name", no_argument, &do_gf_name, 1 },
   { "love", required_argument, NULL, 'l' },
   { 0, 0, 0, 0},
};


/*commen print argument*/
printf("******************************************************\n"); 
printf("usage: get_opt \n ");

printf("argc=%d\n", argc);
for(i=0;i<argc;i++)
   printf("argv[%d]:%s\n", i, argv[i]);
printf("******************************************************\n");

/*over*/

while((oc = getopt_long(argc, argv, ":l:", longopts, NULL)) != -1){
switch (oc){
case 'l':
   l_opt_arg = optarg;
   printf("Our love is %s!\n", l_opt_arg);
break;
case 0:
   printf("getopt_long()设置变量 : do_name = %d\n", do_name);
   printf("getopt_long()设置变量 : do_gf_name = %d\n", do_gf_name);
break;
}
}



printf("it is over, res = %d \n",res);

 

}

 

 

#include <stdio.h>
#include <getopt.h>

int do_name, do_gf_name;
char *l_opt_arg;

struct option longopts[] = {
{ "name", no_argument, NULL, 'n' },
{ "gf_name", no_argument, NULL, 'g' },
{ "love", required_argument, NULL, 'l' },
{ 0, 0, 0, 0},
};

int main(int argc, char *argv[])
{
int c;
while((c = getopt_long(argc, argv, ":ngl:", longopts, NULL)) != -1){

//其中,选项以及其有无参数由":ngl:"决定 而不是在option longopts[] 表中的值决定

switch (c){
case 'n':
printf("My name is LYR.\n");
break;
case 'g':
printf("Her name is BX.\n");
break;
case 'l':
l_opt_arg = optarg;
printf("Our love is %s!\n", l_opt_arg);
break;
}
}
return 0;
}

0 0
原创粉丝点击