技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁(yè) > 教程 > 服務(wù)器類(lèi) >

Linux中g(shù)etopt函數(shù)用法

來(lái)源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2018-07-18 00:11┆點(diǎn)擊:

  最近做cache lab 用到了getopt函數(shù), 用man 3 getopt查看了下用法, 做個(gè)總結(jié).

  描述:getopt函數(shù)是用來(lái)解析命令行參數(shù)的, 以‘-’或‘--’開(kāi)頭的參數(shù)為選項(xiàng)元素,除去‘-’或‘--’的選項(xiàng)元素

  為選項(xiàng)字符。如果getopt函數(shù)被重復(fù)調(diào)用,則它將會(huì)依次返回每個(gè)選項(xiàng)元素中的選項(xiàng)字符。

  使用getopt函數(shù)需要包含以下頭文件:

  #include

  #include

  有幾個(gè)全局變量與getopt函數(shù)解析參數(shù)有關(guān):

  optind: int型, 指示下一個(gè)要解析的參數(shù)位置,初始時(shí)為1.

  optarg: char *, 必須接參數(shù)的選項(xiàng)元素的參數(shù), 如上面的-nxzz, optarg 就指向"xzz"字符串.

  opterr: int 型, 設(shè)為0將不打印錯(cuò)誤信息.

  函數(shù)原型為: int getopt(int argc, char * const argv[], const char *optstring);

  參數(shù)說(shuō)明: 前兩個(gè)參數(shù)與main函數(shù)參數(shù)相同, argc保存參數(shù)個(gè)數(shù),argv[]保存參數(shù)數(shù)組,第三個(gè)參數(shù)optstring是你定義

  的選項(xiàng)字符組成的字符串, 如"abc",表示該命令有三個(gè)選項(xiàng)元素 -a, -b, -c, 選項(xiàng)字符后面如果有一個(gè)冒號(hào)說(shuō)

  明該選項(xiàng)元素一定有一個(gè)參數(shù), 且該參數(shù)保存在optarg中, 如"n:t",

  表示選項(xiàng)元素n后要接參數(shù), 選項(xiàng)元素t后

  不接參數(shù),如 -n xzz -t 或 -nxzz t,有兩個(gè)冒號(hào)說(shuō)明該選項(xiàng)可接可選參數(shù), 但可選參數(shù)不保存在optarg中.

  返回值: 如果當(dāng)前處理的參數(shù)為選項(xiàng)元素,且該選項(xiàng)字符在optstring字符串中, 即為你定義的選項(xiàng), 則返回該

  選項(xiàng)字符,如果該選項(xiàng)字符不是你定義的, 那么返回字符'?', 并更新全局變量optind, 指向argc數(shù)組中的下一

  個(gè)參數(shù). 如果當(dāng)前處理的參數(shù)不是選項(xiàng)元素, 則optind偏移向下一個(gè)參數(shù), 直到找到第一個(gè)選項(xiàng)元素為止, 然后

  再按之前描述的操作,如果找不到選項(xiàng)元素, 說(shuō)明解析結(jié)束, 則返回-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;

  }

  假設(shè)編譯好的可執(zhí)行文件名為test, test有3個(gè)有效參數(shù)-n, -b, -t, 其中-n, -t后不接參數(shù), -b后一定要接參數(shù), -o后接可選參數(shù).

  # ./test -x -y -z <------ 無(wú)效參數(shù), 會(huì)打印錯(cuò)誤信息, 并返回字符'?'

  輸出:

  ./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 指向選項(xiàng)元素的參數(shù), 并且optind跳過(guò)了該參數(shù), 直接指向了-t參數(shù)

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

  # ./test -n -bxzz -t <------------- 也可將選項(xiàng)參數(shù)與其接的參數(shù)寫(xiě)在一起

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

  opt = b, optarg = xzz, optind = 3, argv[3] = -t <----------- optind 同樣將指向下一個(gè)參數(shù)-t

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

  # ./test -b -t

  opt = b, optarg = -t, optind = 3, argv[3] = (null) <----------- 將-t當(dāng)成了選項(xiàng)元素-b的參數(shù), 之后就不再解析-t, optind為3

  # ./test -t -b <---- -b缺少參數(shù)

  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 <------- 命令行中有一個(gè)無(wú)用參數(shù) a, 解析時(shí)被忽略了, 因?yàn)?t不需要接參數(shù)

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

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

  # ./test -ntb xzz <--------- 還可以把參數(shù)連在一起寫(xiě)

  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選項(xiàng)不接參數(shù)

  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選項(xiàng)接參數(shù)10

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

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

  opt = b, optarg = xzz, optind = 6, argv[6] = (null) <----------------- 而-b的參數(shù)則保存在optarg中了

  常用用法:

  一般是用while循環(huán)配合switch語(yǔ)句來(lái)使用getopt函數(shù)解析參數(shù), 如下:

  /*

  * 得到參數(shù)信息

  */

  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) {