Chinaunix首页 | 论坛 | 博客
  • 博客访问: 457470
  • 博文数量: 42
  • 博客积分: 1325
  • 博客等级: 中尉
  • 技术积分: 1312
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-13 18:00
个人简介

呵~~~呵~~~

文章分类

全部博文(42)

文章存档

2016年(3)

2015年(1)

2014年(2)

2013年(2)

2012年(7)

2011年(11)

2010年(3)

2009年(13)

我的朋友

分类: LINUX

2011-09-06 15:50:43

一、短参数

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "unistd.h"  //for getopt()

  4. int main(int argc, char *argv[])
  5. {
  6.     //for argument
  7.     extern int optind;
  8.     extern int opterr;
  9.     extern int optopt;
  10.     extern char *optarg;
  11.     char arg = 0x0;

  12.     char *wdpath = NULL;
  13.     char *wdtable = NULL;
  14.     char *shost = NULL;
  15.     char *sport = NULL;

  16.     //init argument
  17.     while((arg = getopt(argc, argv, "h:p:d:t:")) != EOF)
  18.     {
  19.         switch(arg)
  20.         {
  21.             case 'h':
  22.                 shost = optarg;
  23.                 printf("host: %s\n", shost);
  24.                 break;
  25.             case 'p':
  26.                 sport = optarg;
  27.                 printf("port: %s\n", sport);
  28.                 break;
  29.             case 'd':
  30.                 wdpath = optarg;
  31.                 break;
  32.             case 't':
  33.                 wdtable = optarg;
  34.                 break;
  35.             default:
  36.                 printf("Fault argument.\n");
  37.                 break;
  38.         }
  39.     }

  40.     //dosomething.

  41. }

 

二、长参数

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"

  4. #include "unistd.h"
  5. #include "getopt.h"

  6. char *gs_storepath;
  7. int gi_minimumspeed;
  8. int gi_checkinterval;

  9. int main(int argc, char *argv[])
  10. {
  11.     //for argument
  12.     extern int optind;
  13.     extern int opterr;
  14.     extern int optopt;
  15.     extern char *optarg;

  16.     char arg = 0x0;

  17.     struct option larg[]={
  18.         {"storepath", required_argument, NULL, 'p'},
  19.         {"minimumspeed", required_argument, NULL, 's'},
  20.         {"checkinterval", required_argument, NULL, 'i'},
  21.         {"help", no_argument, NULL, 'h'}
  22.     };
  23.     //init argument
  24.     while((arg = getopt_long(argc, argv, "p:s:i:h", larg, NULL)) != EOF)
  25.     {
  26.         switch(arg)
  27.         {
  28.             case 'p':
  29.                 gs_storepath = optarg;
  30.                 printf("storepath: %s\n", gs_storepath);
  31.                 break;
  32.             case 's':
  33.                 gi_minimumspeed = atoi(optarg);
  34.                 printf("lowestspeed: %d\n", gi_minimumspeed);
  35.                 break;
  36.             case 'i':
  37.                 gi_checkinterval = atoi(optarg);
  38.                 printf("interval: %d\n", gi_checkinterval);
  39.                 break;
  40.             default:
  41.                 printf("Fault argument.\n");
  42.                 break;
  43.         }
  44.     }



  45.     //dosomething.

  46. }


 

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