Chinaunix首页 | 论坛 | 博客
  • 博客访问: 109314
  • 博文数量: 27
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 360
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-07 00:14
文章分类

全部博文(27)

文章存档

2015年(1)

2014年(20)

2013年(6)

我的朋友

分类: C/C++

2014-01-01 23:20:42

1 全局变量 optind 的作用
       它的值表示将要分析argv中的,如果要重新扫描一个命令行参数选项,那么将给值设置为1(设置为0 不起作用)
2 getopt 的返回值
       如果能找到一个选项,并且该选项值出现在optstr字符串内,则返回该选项的字符
       如果所有的选项都处理完了,则返回-1
       如果能找到一个选项,但是该选项没有出现在optstr字符串内,则返回'?'字符
       如果没有给出选项的参数,那么当 optstr字符串的第一个字符为':', 则返回‘:', 否则返回'?'字符


3 optopt 变量(类型 int)
    保存没有在optstr字符串上的选项

4 struct option {
          const char *name; // 长选项的名字
          int has_arg; // 是否带有选项参数, no_argument, required_argument
          int *flag; //  一般都设置为 NULL, 则函数返回 val值
          int val;
   }

   struct option options[] = {
         {"name", required_argument, NULL, 'n'},
         {"age", required_argument, NULL, 'a' },
         {0,0,0.0}
    }

实例代码
 

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <getopt.h>

  5. int main(int argc ,char *argv[])
  6. {
  7.     int c;

  8.     while ((c = getopt(argc, argv, ":a:b")) != -1)
  9.     {
  10.         switch (c)
  11.         {
  12.             case 'a':
  13.                 fprintf(stderr, "[INFO]###### a option is given, option argument is :%s\n", optarg);
  14.                 break;

  15.             case 'b':
  16.                 fprintf(stderr, "[INFO]###### b option is given\n");
  17.                 break;

  18.             case '?':
  19.                 fprintf(stderr, "[ERROR]###### not such %c option in the optstr \n", optopt);

  20.             case ':':
  21.                 fprintf(stderr, "[ERROR]###### you must give option %c an argument .\n", optopt);
  22.                 break;
  23.         }
  24.     }
  25.     return (0);
  26. }


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #include <getopt.h>
  4. #include <string.h>
  5. #include <errno.h>

  6. int main(int argc, char *argv[])
  7. {
  8.     char command[] = "vde2-net 21749 2.4 42.8 11769352 10595956 ? Sl May24 69:26 /usr/local/infobright-4.0.7-x86_64/bin/mysqld --defaults-file=/etc/my-ib.cnf --basedir=/usr/local/infobright-4.0.7-x86_64 --datadir=/home/ib_iee/data --user=mysql --log-queries-not-using-indexes --log-error=/home/ib_iee/data/bh.err --pid-file=/home/ib_iee/data/debRDTest1.pid --socket=/tmp/mysql-ib.sock --port=5029";

  9.     char *ib_argv[1024] = { 0 };
  10.     int ib_argc = 0;
  11.     char *saveptr ;
  12.     char *str = command;
  13.     int i;

  14.     opterr = 0; // prevent the error message
  15.     while (1)
  16.     {
  17.         char *a_argv = strtok_r(str, " ", &saveptr);
  18.         if (a_argv == NULL)
  19.         {
  20.             break;
  21.         }
  22.         ib_argv[ib_argc] = malloc(1024);
  23.         strncpy(ib_argv[ib_argc], a_argv, 1024);
  24.         ib_argc++;
  25.         str = NULL;
  26.     }
  27.     

  28.     const char *ib_base_dir = NULL;
  29.     const char *ib_data_dir = NULL;

  30.     
  31.     while (1)
  32.     {
  33.         static struct option options [] = {
  34.             {"basedir", required_argument, NULL, 'b'},
  35.             {"datadir", required_argument, NULL, 'd'},
  36.             {NULL, 0, NULL, 0}
  37.         };

  38.         int c ;
  39.         int longindex;
  40.         c = getopt_long_only(ib_argc, ib_argv, "", options, &longindex);
  41.         if (c == -1)
  42.         {
  43.             break;
  44.         }

  45.         switch (c)
  46.         {
  47.             case 'd':
  48.                 ib_data_dir = optarg;
  49.                 break;

  50.             case 'b':
  51.                 ib_base_dir = optarg;
  52.                 break;
  53.         }
  54.     }

  55.     if (!((int)ib_data_dir & (int)ib_base_dir))
  56.     {
  57.         fprintf(stderr, "[ERROR]#################### service is not running .!\n");
  58.         exit(-1);
  59.     }

  60.     fprintf(stderr, "[INFO] ################# basedir = %s \n", ib_base_dir);
  61.     fprintf(stderr, "[INFO] ################# datadir = %s \n", ib_data_dir);

  62. cleanup:
  63.     for (i = 0; i < ib_argc; ++i)
  64.     {
  65.         free(ib_argv[i]);
  66.     }
  67.     return 0;
  68. }


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