Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7559912
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: LINUX

2011-06-12 18:47:52

  1. /*
  2.  *    命令解析
  3.  *    int getopt(int argc,char * const argv[ ],const char * optstring);
  4.  *    Lzy    2011-6-12
  5.  */

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>

  9. int main(int argc, char *argv[])
  10. {
  11.     int ret;
  12.     
  13.     while((ret = getopt(argc, argv,"hf:d")) != -1)        //解析命令
  14.     {
  15.         switch(ret)
  16.         {
  17.             case 'h':
  18.                 printf("help menu\n");
  19.                 break;
  20.                 
  21.             case 'f':
  22.                 printf("filename %s\n",optarg);            //打印选项参数
  23.                 break;
  24.             
  25.             case 'd':
  26.                 printf("Debug mode\n");
  27.                 break;
  28.             
  29.             case '?':                            
  30.                 printf("no option %c\n",optopt);        //无此选项
  31.                 
  32.             default:
  33.                 exit(-1);
  34.         }        
  35.     }
  36.     
  37.     for(ret = optind; ret < argc; ret++)
  38.     {
  39.         printf("Non option %s\n",argv[ret]);        //命令行中还没有被解析的
  40.     }

  41.     return 0;
  42. }

 

#include

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

20 世纪 90 年代,UNIX 应用程序开始支持长选项,即一对短横线、一个描述性选项名称,还可以包含一个使用等号连接到选项的参数。GNU提供了getopt-long() getopt-long-only()函数支持长选项的命令行解析,其中,后者的长选项字串是以一个短横线开始的,而非一对短横线。 getopt_long() 是同时支持长选项和短选项的 getopt() 版本。

 

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