码迷,mamicode.com
首页 > 其他好文 > 详细

getopt_long

时间:2015-06-24 12:31:35      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <getopt.h>

char *l_opt_arg;
char* const short_options = "c:";


struct option long_options[] = {
  { "aa", 0, NULL, 0 },
  { "bb", 0, NULL, 0 },
  { "cc", required_argument, NULL, ‘c‘ },
  { 0, 0, 0, 0},
};


int main(int argc, char *argv[])
{
        int c;
        int index = 0;
        while((c = getopt_long (argc, argv, short_options, long_options, &index)) != -1)
        {
                switch (c)
                {
                        case 0:
                                switch (index)
                                {
                                        case 0:
                                                printf("is aa : c = %d, index = %d\n", c, index);
                                                break;
                                        case 1:
                                                printf("is bb : c = %d, index = %d\n", c, index);
                                                break;
                                        default:
                                                printf("no one : c= %d, index = %d\n", c, index);

                                                break;

                                  }
            break;
        case ‘c‘:
            l_opt_arg = optarg;
            printf("cc is %s(optarg), c = %c, index = %d!\n", l_opt_arg, c, index);
            break;
      }
  }
  return 0;
}

 

getopt_long

标签:

原文地址:http://www.cnblogs.com/banwhui/p/4597318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!