Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48841
  • 博文数量: 17
  • 博客积分: 74
  • 博客等级: 民兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-07 23:31
文章分类
文章存档

2012年(12)

2011年(5)

我的朋友
最近访客

分类:

2011-12-07 23:40:05

                                                               获取用户命令行参数Option   
1. 说明
bash# ls -al
其中 a 和 l 叫做命令行参数,而且是短的,
bash# XXX --param=xxxx
其中的param也叫命令行参数,但是是长的。
区别在于一个 - 还是 --。

2. 函数和数据结构
man手册上抄来的。
#include
int getopt_long(int argc, char * const argv[],
                  const char *optstring,
                  const struct option *longopts, int *longindex);

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

name  
               is the name of the long option.
has_arg
              is: no_argument (or 0) if the option does not take an  argument;  required_argument  (or  1)  if  the  option
              requires an argument; or optional_argument (or 2) if the option takes an optional argument.

flag   
              specifies how results are returned for a long option.  If flag is NULL, then getopt_long() returns val.                     (For example, the calling program may set val to the equivalent short option character.)  Otherwise,                       getopt_long()  returns  0,  and  flag points to a variable which is set to val if the option is found, but left                 unchanged if  the option is not found.

val    
              is the value to return, or to load into the variable pointed to by flag.
一般情况下,flag为0,val是一个能代表这个option的字符,用于返回值。




这里还有几个extern的全局变量,其中最有用处的就是
extern char *optarg
              当我们的option后面需要跟一个值得时候(例如: --file="/dev/NULL"),optarg就是这项这个字符串的指针(也就是:optarg就是指向"/dev/NULL的指针")。

NOTE:
使用getopt_long()的时候,第三个参数也是需要的。


3. 例子:
  1. #include <stdio.h>
  2. #include <getopt.h>
  3. #include <unistd.h>
  4. /**
  5.  * @val short_opts
  6.  * This is short option string, and the : after d
  7.  * means option d should have value like d=xxx.
  8.  * 这里,如果option后面需要跟一个参数,即使这个参数是optional的,
  9.  * 在那个代表option的字母后面跟一个冒号':'
  10.  */
  11. static const char short_opts[] = "d:hmru";

  12. /**
  13.  * @val lng_opts
  14.  * This is for long options, and the last one
  15.  * in the array must be all ZERO.
  16.  */
  17. static const struct option lng_opts[] = {
  18.     {"device", required_argument, NULL, 'd'},
  19.     {"help", no_argument, NULL, 'h'},
  20.     {"mmap", no_argument, NULL, 'm'},
  21.     {"read", no_argument, NULL, 'r'},
  22.     {"usrptr", no_argument, NULL, 'u'},
  23.     {NULL, 0, NULL, 0}
  24. };
  25. extern char *optarg;
  26. int get_usr_opt(int argc, char *argv[]);

  27. int main(int argc, char *argv[]) {
  28.     int ret = get_usr_opt(argc, argv);

  29.     switch (ret) {
  30.     case 0:
  31.         printf("set as user option.\n");
  32.         break;
  33.     case -1:
  34.         printf("argv is NULL.\n");
  35.         break;
  36.     case -2:
  37.         printf("no user options at all.\n");
  38.         break;
  39.     case -3:
  40.         printf("unknown user option.\n");
  41.         break;
  42.     default:
  43.         perror("UnKnown error!\n");
  44.     }

  45.     return 0;
  46. }

  47. /**
  48.  * @func int get_usr_opt(int argc, char *argv[])
  49.  * get user setted options from command line.
  50.  * @param argc number of user options, the same as main.
  51.  * @param argv user input string, the sameas main.
  52.  * @return
  53.  * 0 set as user option.
  54.  * -1 argv is NULL.
  55.  * -2 no user options at all.
  56.  * -3 unknown user option.
  57.  */
  58. int get_usr_opt(int argc, char *argv[])
  59. {
  60.     if (NULL == argv)
  61.         return -1;

  62.     if (argc < 2) {
  63.         return -2; /**< no user option, give default*/
  64.     }

  65.     int idx = 0, opt = 0;
  66.     while (1) {
  67.         opt = getopt_long(argc, argv, short_opts, lng_opts, &idx);
  68.         /**< get the last*/
  69.         if (-1 == opt)
  70.             break;

  71.         switch (opt) {
  72.         case '?':
  73.             perror("Unknown Option!\n");
  74.             return -3;
  75.         case 'd':
  76.             printf("d is chosen, option value is %s\n", optarg);
  77.             break;
  78.         case 'h':
  79.             printf("h is chosen\n");
  80.             break;
  81.         case 'm':
  82.             printf("m is chosen\n");
  83.             break;
  84.         case 'r':
  85.             printf("r is chosen\n");
  86.             break;
  87.         case 'u':
  88.             printf("u is chosen\n");
  89.             break;
  90.         default:
  91.             perror("Unknown Option!\n");
  92.             return -3;
  93.         }
  94.     }

  95.     return 0;
  96. }

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