歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux選項-getopt/getopt_long

Linux選項-getopt/getopt_long

日期:2017/2/28 16:07:40   编辑:Linux教程

一、命令行簡介

解釋分析命令行通常是所以程序的第一個任務,C語言通過argc和argv參數來訪問它的命令行參數。

最簡單的命令行處理技術可以通過if判斷來表示,如下例:

  1. if(argc>1 && argv[1][0] =='-' && argv[1][1] =='h') //判斷命令行參數是否為-h
  2. {
  3. do _ some thing();
  4. }

這樣處理簡單有序的命令行還可以,對於復雜的命令行處理顯得有心無力,於是GNU提供兩個函數專門用來處理命令行參數:

getopt 和getopt_long

二、getopt函數

getopt() 函數聲明如下:

  1. #include <unistd.h>
  2. int getopt(int argc, char *const argv[], const char *optstring);
  3. extern char *optarg;
  4. extern int optind, opterr, optopt;

說 明:

函數的argc和argv參數通常直接從main()的參數直接傳遞而來。optstring是選項字母組成的字串。如果該字串裡的任一字符後面有冒號,那麼這個選項就要求有選項參數。

當給定getopt()命令參數的數量 (argc)、指向這些參數的數組 (argv) 和選項字串 (optstring) 後,getopt() 將返回第一個選項,並設置一些全局變量。使用相同的參數再次調用該函數時,它將返回下一個選項,並設置相應的全局變量。如果不再有可識別的選項,將返回 -1,此任務就完成了。

getopt()所設置的全家變量包括:

optarg ---- 當前選項參數字符(如果有的話)

optind ---- argv的當前索引值。當getopt()在while循環中使用時,循環結束後,剩下的字串視為操作數,在
argv[optind]至argv[argc-1]中可以找到。

optopt ---- 用於當發現無效選項字符的時候,getopt函數或者返回 "?" 或者返回 ":" 字符,並且optopt包含了
所發現的無效選項字符,或者當輸入非法參數的時候,由optopt帶回輸入的非法參數(字符)

opterr ---- 這個變量非零時,getopt()函數為“無效選項”和“缺少參數選項,並輸出其錯誤信息。

另外:

如果optstring參數的第一個字符是冒號,那麼getopt會根據錯誤情況返回不同的字符:

(1):當錯誤是無效選項,getopt返回 "?"

(2):當錯誤是缺少選項參數,getopt返回 ":"

(3): 無錯誤發生時,正常返回的是對應的字符!

注:GNU getopt()第三個特點是optstring中的選項字符後面接兩個冒號,就允許該選項有可選的選項參數。在選項參數不存在的情況下,GNU getopt()返回選項字符並將optarg設置為NULL。

例子:

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<getopt.h>
  4. /*the variables bellow was define in getopt.h
  5. extern char *optarg;
  6. extern int optind, opterr, optopt;
  7. */
  8. char* para = ":ab:c";
  9. void print_extern_val(void)
  10. {
  11. printf("optarg=%s.optind=%d.opterr=%d.optopt=%d=%c[END]/n",
  12. optarg, optind, opterr, optopt, optopt);
  13. }
  14. int main(int argc,char* argv[])
  15. {
  16. int oc = -1;
  17. char* b_input = NULL;
  18. while((oc = getopt(argc,argv,para)) != -1)
  19. {
  20. switch(oc)
  21. {
  22. case 'a':
  23. printf("input para is a/n");
  24. print_extern_val();
  25. break;
  26. case 'b':
  27. b_input = optarg;
  28. printf("input para is b,and optarg is %s/n",b_input);
  29. print_extern_val();
  30. break;
  31. case 'c':
  32. printf("input para is c/n");
  33. print_extern_val();
  34. break;
  35. case ':':
  36. printf("option %c requires an argument/n",optopt);
  37. print_extern_val();
  38. break;
  39. case '?':
  40. default:
  41. printf("option %c is invalid:ignored/n",optopt);
  42. print_extern_val();
  43. break;
  44. }
  45. }
  46. return 0;
  47. }

結果:

[root@localhost opts]# ./getopt -a

input para is a

optarg=(null).optind=2.opterr=1.optopt=0=[END]

[root@localhost opts]# ./getopt -a -b

input para is a

optarg=(null).optind=2.opterr=1.optopt=0=[END]

option b requires an argument

optarg=(null).optind=3.opterr=1.optopt=98=b[END]

[root@localhost opts]# ./getopt -a -b mingming

input para is a

optarg=(null).optind=2.opterr=1.optopt=0=[END]

input para is b,and optarg is mingming

optarg=mingming.optind=4.opterr=1.optopt=0=[END]

[root@localhost opts]# ./getopt -a -d

input para is a

optarg=(null).optind=2.opterr=1.optopt=0=[END]

option d is invalid:ignored

optarg=(null).optind=3.opterr=1.optopt=100=d[END]

Copyright © Linux教程網 All Rights Reserved