欢迎登陆真网站,您的到来是我们的荣幸。 登陆 注册 忘记密码? ☆设为首页 △加入收藏
欢迎加入真幸福QQ群
电脑知识: 基础知识 网络技术 操作系统 办公软件 电脑维修 电脑安全 windows7 windows8 windows10 服务器教程 平板电脑 视频播放教程 网络应用 互联网 工具软件 浏览器教程 QQ技巧 输入法教程 影视制作 YY教程 wps教程 word教程 Excel教程 PowerPoint
云南西双版纳特产小花糯玉米真空包装


Linux多线程编程
MAC解决Spotlight 的‘索引中问题
mac操作系统sudo的时候不用输入密码
XP中根据端口号或PID查看进程
2004无法打开服务器服务。服务器性能数据将不会被返回
DM硬盘分区使用教程
win客户端和linux客户端的区别
unix/linux内核在系统里扮演什么
Linux如何清空所有iptables规则
快速扫除windows注册表垃圾的方案
Linux中getopt函数用法
【 来源:网络 】【 点击:1 】 【 发布时间:2017_03_03 08:59:59 】

   最近做cache lab 用到了getopt函数, 用man 3 getopt查看了下用法, 做个总结.

  描述:getopt函数是用来解析命令行参数的, 以‘-’或‘--’开头的参数为选项元素,除去‘-’或‘--’的选项元素

  为选项字符。如果getopt函数被重复调用,则它将会依次返回每个选项元素中的选项字符。

  使用getopt函数需要包含以下头文件:

  #include

  #include

  有几个全局变量与getopt函数解析参数有关:

  optind: int型, 指示下一个要解析的参数位置,初始时为1.

  optarg: char *, 必须接参数的选项元素的参数, 如上面的-nxzz, optarg 就指向"xzz"字符串.

  opterr: int 型, 设为0将不打印错误信息.

  函数原型为: int getopt(int argc, char * const argv[], const char *optstring);

  参数说明: 前两个参数与main函数参数相同, argc保存参数个数,argv[]保存参数数组,第三个参数optstring是你定义

  的选项字符组成的字符串, 如"abc",表示该命令有三个选项元素 -a, -b, -c, 选项字符后面如果有一个冒号说

  明该选项元素一定有一个参数, 且该参数保存在optarg中, 如"n:t",

  表示选项元素n后要接参数, 选项元素t后

  不接参数,如 -n xzz -t 或 -nxzz t,有两个冒号说明该选项可接可选参数, 但可选参数不保存在optarg中.

  返回值: 如果当前处理的参数为选项元素,且该选项字符在optstring字符串中, 即为你定义的选项, 则返回该

  选项字符,如果该选项字符不是你定义的, 那么返回字符'?', 并更新全局变量optind, 指向argc数组中的下一

  个参数. 如果当前处理的参数不是选项元素, 则optind偏移向下一个参数, 直到找到第一个选项元素为止, 然后

  再按之前描述的操作,如果找不到选项元素, 说明解析结束, 则返回-1.

  下面看例子:

  #include

  #include

  int main(int argc, char * const argv[])

  {

  int opt;

  while ((opt = getopt(argc, argv, "nb:o::t")) != -1) {

  printf("opt = %c, optarg = %s, optind = %d, argv[%d] = %sn",

  opt, optarg, optind, optind, argv[optind]);

  }

  return 0;

  }

  假设编译好的可执行文件名为test, test有3个有效参数-n, -b, -t, 其中-n, -t后不接参数, -b后一定要接参数, -o后接可选参数.

  # ./test -x -y -z <------ 无效参数, 会打印错误信息, 并返回字符'?'

  输出:

  ./getopt: invalid option -- 'x'

  opt = ?, optarg = (null), optind = 2, argv[2] = -y

  ./getopt: invalid option -- 'y'

  opt = ?, optarg = (null), optind = 3, argv[3] = -z

  ./getopt: invalid option -- 'z'

  opt = ?, optarg = (null), optind = 4, argv[4] = (null)

  # ./test -n -b xzz -t

  opt = n, optarg = (null), optind = 2, argv[2] = -b

  opt = b, optarg = xzz, optind = 4, argv[4] = -t <----------- optarg 指向选项元素的参数, 并且optind跳过了该参数, 直接指向了-t参数

  opt = t, optarg = (null), optind = 5, argv[5] = (null)

  # ./test -n -bxzz -t <------------- 也可将选项参数与其接的参数写在一起

  opt = n, optarg = (null), optind = 2, argv[2] = -bxzz

  opt = b, optarg = xzz, optind = 3, argv[3] = -t <----------- optind 同样将指向下一个参数-t

  opt = t, optarg = (null), optind = 4, argv[4] = (null)

  # ./test -b -t

  opt = b, optarg = -t, optind = 3, argv[3] = (null) <----------- 将-t当成了选项元素-b的参数, 之后就不再解析-t, optind为3

  # ./test -t -b <---- -b缺少参数

  opt = t, optarg = (null), optind = 2, argv[2] = -b

  ./getopt: option requires an argument -- 'b'

  opt = ?, optarg = (null), optind = 3, argv[3] = (null)

  # ./test -t a -b xzz <------- 命令行中有一个无用参数 a, 解析时被忽略了, 因为-t不需要接参数

  opt = t, optarg = (null), optind = 2, argv[2] = a

  opt = b, optarg = xzz, optind = 5, argv[5] = (null)

  # ./test -ntb xzz <--------- 还可以把参数连在一起写

  opt = n, optarg = (null), optind = 1, argv[1] = -ntb

  opt = t, optarg = (null), optind = 1, argv[1] = -ntb

  opt = b, optarg = xzz, optind = 3, argv[3] = (null)

  # ./test -t -o -b xzz <----- -o选项不接参数

  opt = t, optarg = (null), optind = 2, argv[2] = -o

  opt = o, optarg = (null), optind = 3, argv[3] = -b

  opt = b, optarg = xzz, optind = 5, argv[5] = (null)

  # ./test -t -o 10 -b xzz <------ -o选项接参数10

  opt = t, optarg = (null), optind = 2, argv[2] = -o

  opt = o, optarg = (null), optind = 3, argv[3] = 10 <------------------ 可以看到10并没有保存在optarg中

  opt = b, optarg = xzz, optind = 6, argv[6] = (null) <----------------- 而-b的参数则保存在optarg中了

  常用用法:

  一般是用while循环配合switch语句来使用getopt函数解析参数, 如下:

  /*

  * 得到参数信息

  */

  int getinfo(int argc, char * const argv[], int *ps, int *pE, int *pb, char *trace_name, int *vflag)

  {

  int opt;

  int count_arg = 0;

  opterr = 0;

  while ((opt = getopt(argc, argv, "vs:E:b:t:")) != -1) {

  switch (opt) {

  case 'v':

  *vflag = 1;

  break;

  case 's':

  ++count_arg;

  *ps = atoi(optarg);

  break;

  case 'E':

  ++count_arg;

  *pE = atoi(optarg);

  break;

  case 'b':

  ++count_arg;

  *pb = atoi(optarg);

  break;

  case 't':

  ++count_arg;

  strcpy(trace_name, optarg);

  break;

  default: /* '?' */

  Usage();

  exit(-1);

  break;

  }

  }

  if (count_arg < 4) {

  Usage();

  exit(-1);

  }

  return 0;

  }

本网站由川南居提供技术支持,fkzxf版权所有 浙ICP备12031891号
淳安分站 淳安分站