Linux中getopt函數用法

來源:互聯網
上載者:User

   最近做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;

  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.