Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1069974
  • 博文数量: 169
  • 博客积分: 12306
  • 博客等级: 上将
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-29 14:55
文章分类

全部博文(169)

文章存档

2012年(18)

2011年(78)

2010年(15)

2009年(1)

2008年(11)

2007年(39)

2006年(7)

我的朋友

分类: LINUX

2011-01-18 11:33:32

getopt函数只能解析短选项,getopt_long除了接受短选项,还能解析长选项。因此,getopt_long函数在linux命令行程序中使用得最广泛。

 

 1. 函数原型    

  #include

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

 

2. 参数

(1)argc

(2)argv

(3)optstring:短选项字符集合。若只有长选项,则置为空字符串(“”)

(4)longopts:指向struct option数组的第一个元素,结构体option定义如下

struct option

{

  const char *name;  //长选项名称

  int has_arg; //是否有参数no_argument(0)  required_argument(1)  optional_argument(2)

  int *flag; //指定长选项的返回结果:NULL 返回val;非空返回0

  int val; //返回的值或装载flag指向的变量

};

注:struct option 数组最后一个元素用0填充

(5)longindex 非空的话,指向长选项的索引

 

3. 返回值

对短选项,返回对应的选项字符;

对长选项,若flag为NULL,返回val;否则返回0

 

4. 测试

这里只测试长选项,至于短选项的处理则和getopt函数相同

mygetoptlong.c

 
#include
#include

void usage(const char *p);

int main(int argc, char *argv[])
{
    int ch = 0, longindex = 0;
    struct option myoption[] = {
                                 {"input", 1, NULL, 'i'},
                                 {"all", 0, NULL, 'a'},
                                 {"output", 1, NULL, 'o'},
                                 {"version", 0, NULL, 'v'},
                                 {"help", 0, NULL, 'h'},
                                 {NULL, 0, NULL, 0}
                               };
    opterr = 0; // prevent error information to print for unrecognized options
    while( ( ch = getopt_long(argc, argv, "", myoption, &longindex ) ) != -1 )
     {
        switch(ch)
        {
           case 'i':
           case 'o':
                printf("option \"%s\": ", myoption[longindex].name);
                printf("%s\n\n", optarg);
                break;
           case 'a':
           case 'v':
                printf("option \"%s\": \n\n", myoption[longindex].name);
                break;
           case 'h':
                printf("option \"%s\": \n\n", myoption[longindex].name);
                usage(argv[0]);
                break;
           default:
                printf("uncognized option: %c\n\n", optopt);
                usage(argv[0]);
        }
     }
if ( optind < argc )
       {
          printf("\nnon-opotion arguments below:\n");
          while( optind < argc )
            {
                printf("%s\n", argv[optind++]);
            }
       }
    return 0;
}
void usage(const char *p)
{
   printf("Usage: %s [--input infile] [--all] [--output outfile] [--version] [--help] [file]\n\n", p);
}
 

 

执行结果:

$ ./mygetoptlong --input infile --all --output outfile --version --help file

option "input": infile

 

option "all":

 

option "output": outfile

 

option "version":

 

option "help":

 

Usage: ./mygetoptlong [--input infile] [--all] [--output outfile] [--version] [--help] [file]

 

 

non-opotion arguments below:

file

 

阅读(727) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-07 08:31:53

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com

chinaunix网友2011-03-07 08:31:53

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com