Chinaunix首页 | 论坛 | 博客
  • 博客访问: 956742
  • 博文数量: 120
  • 博客积分: 6454
  • 博客等级: 准将
  • 技术积分: 1739
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-28 17:45
文章分类

全部博文(120)

文章存档

2014年(1)

2013年(1)

2012年(11)

2011年(16)

2010年(6)

2009年(11)

2008年(30)

2007年(44)

分类: C/C++

2012-07-18 11:27:09

在GNU下有getopt类的函数可以轻松解析参数列表,省去很多烦恼,定义如下:

点击(此处)折叠或打开

  1.        #include <unistd.h>

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

  4.        extern char *optarg;
  5.        extern int optind, opterr, optopt;

  6.        #define _GNU_SOURCE
  7.        #include <getopt.h>

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

  11.        int getopt_long_only(int argc, char * const argv[],
  12.                   const char *optstring,
  13.                   const struct option *longopts, int *longindex);
需要对长参数(非一个字符)进行解析的时候要用到getopt_long(),这个函数有个参数struct option的定义为:

点击(此处)折叠或打开

  1. struct option {
  2.               const char *name;
  3.               int has_arg;
  4.               int *flag;
  5.               int val;
  6.           };
这个结构体定义属于头文件
对与这个结构体的详细解释为:

点击(此处)折叠或打开

  1. struct option {
  2.               const char *name; // 名字
  3.               int has_arg;        // 0 没有参数, 1 必须有参数, 2 可选参数
  4.               int *flag;        // 如果这个指针为NULL,那么getopt_long()返回该结构val字段中的数值。如果该指针不为NULL,getopt_long()会使得它所指向的变量中填入val字段中的数值,并且getopt_long()返回0。如果flag不是NULL,但未发现长选项,那么它所指向的变量的数值不变。
  5.               int val;            // 发现了长选项时的返回值,或者flag不是 NULL时载入*flag中的值。典型情况下,若flag不是NULL,那么val是个真/假值,譬如1 或0;另一方面,如果flag是NULL,那么val通常是字符常量,若长选项与短选项一致,那么该字符常量应该与optstring中出现的这个选项的参数相同。
  6.           };// 此数据类型的数据最后一个元素必须是0
下面是一个例子:

点击(此处)折叠或打开

  1. #include <stdio.h> /* for printf */
  2. #include <stdlib.h> /* for exit */
  3. #include <getopt.h>

  4. // 定义长参数
  5. static struct option long_options[] = {
  6.     {"addsg", 0, 0, '1'},
  7.     {"rmsg", 0, 0, '2'},
  8.     {"addnode", 0, 0, '3'},
  9.     {"rmnode", 0, 0, '4'},
  10.     {"from", 1, 0, 'f'},
  11.     {"to", 1, 0, 't'},
  12.     {"help", 0, 0, 'h'},
  13.     {"version", 0, 0, 'v'},
  14.     {"reflush", 0, 0, 'r'},
  15.     {0, 0, 0, 0}
  16. };

  17. // 定义短参数
  18. char* const short_options = "1234f:t:hvr";


  19. // 循环解析参数,并进行处理
  20. int get_options (int argc, char **argv)
  21. {
  22.     int c;

  23.     while (1) {
  24.         int this_option_optind = optind ? optind : 1;
  25.         int option_index = 0;
  26.         
  27.         c = getopt_long (argc, argv, short_options, long_options, &option_index);
  28.         if (c == -1)
  29.             break;

  30.         switch (c) {
  31.         case 0:
  32.             printf ("option %s", long_options[option_index].name);
  33.             if (optarg)
  34.                 printf (" with arg %s", optarg);
  35.             printf ("\n");
  36.             break;

  37.         case '1':
  38.             printf ("start addnode function\n");
  39.             break;

  40.         case '2':
  41.             printf ("start removesg function\n");
  42.             break;

  43.         case '3':
  44.             printf ("start addnode function\n");
  45.             break;

  46.         case '4':
  47.             printf ("start removenode function\n");
  48.             break;

  49.         case 'h':
  50.             printf ("show help\n");
  51.             break;
  52.             
  53.         case 'v':
  54.             printf("show version\n");
  55.             break;

  56.         case 'f':
  57.             printf ("src ip is : '%s'\n", optarg);
  58.             break;

  59.         case 't':
  60.             printf ("dst ip is : '%s'\n", optarg);
  61.             break;
  62.             
  63.         case 'r':
  64.             printf ("should reflush data\n");
  65.             break;

  66.         case '?':
  67.             break;

  68.         default:
  69.             printf ("?? getopt returned character code 0%o ??\n", c);
  70.         }
  71.     }

  72.     if (optind < argc) {
  73.         printf ("non-option ARGV-elements: ");
  74.         while (optind < argc)
  75.             printf ("%s ", argv[optind++]);
  76.         printf ("\n");
  77.     }

  78.     return 0;
  79. }

  80. int main (int argc, char **argv)
  81. {
  82.     printf("main function\n");
  83.     get_options(argc, argv);     // 将程序的参数直接传给函数,用以解析
  84.     return 0;
  85. }




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