Linux命令選項及參數解析 getopt() getopt_long() 函數

來源:互聯網
上載者:User

假如程式命令列啟動時,需要指定一系列參數,那麼,getopt()與getopt_long()是你的不二選擇。

作為曾經還在手寫的孩紙,我發現這個之後,淚流滿面。。

 

1. int getopt(int argc, char * const argv[], const char *optstring)

若選項在optstring中,返回選項字元,否則返回-1;與該選項對應的參數儲存在變數optarg中

包含在unistd.h,argc和argv與main(int argc, char *argv[])的參數相對應,

optstring是選項字元集,表現為在啟動命令列中'-'後面的首個字元,例如:ls -l -a, 'a'和'l'即在optstring中。

optstring的格式規範如下:

1) 單個字元為參數選項

2) 選項字元後面跟':',表示該選項必須具備參數,參數與選項之間以空格分隔,例如:start -f flile

3) 選項字元後面跟'::', 表示該選項必須具備參數,參數緊跟選項,二者串連在一起,此為GNU的擴充,例如:start -ffile

 

2. int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);

若選項在optstring中,返回選項字元,否則返回-1;與該選項對應的參數儲存在變數optarg中

包含在getopt.h中,getopt_long()可以看成是支援長選項的getopt(),argc和argv與main(int argc, char *argv[])的參數相對應,

長選項是以"--"開頭的,舉個例子:"-h"與"--help"

20世紀90年代,Unix應用程式開始支援長選項,Linux是類Unix系統,因此相容長選項

getopt_long()的前3個參數與getopt()相同,第4個參數是指向option結構的數組,option結構被稱為“長選項表”。longindex參數如果沒有設定為NULL,那麼它就指向一個變數,這個變數會被賦值為尋找到的長選項在longopts中的索引值,這可以用於錯誤診斷。

option結構在getopt.h中的聲明如下:
struct option {
  const char *name;
  int has_args;
  int *flag;
  int val;
}; 

name:選項字串

has_args:三種參數類型,no_argument表示無參數(0),required_argument表示需要參數(1),optional_argument表示參數可選(2)

flag:如果為NULL,getopt_long()返回該結構val欄位中的數值;如果不為NULL,getopt_long()會使得它所指向的變數中填入val欄位中的數值,並且getopt_long()返回0;通常flag設定為NULL,val設定為與該長選項對應的短選項

val:發現了長選項時的傳回值,或者flag不是NULL時載入*flag中的值。典型情況下,若flag不是NULL,那麼val是個真/假值,譬如1 或0;另一方面,如果flag是NULL,那麼val通常是字元常量,若長選項與短選項一致,那麼該字元常量應該與optstring中出現的這個選項的參數相同。

 

以下是個樣本:

 1 #include <unistd.h>
2 #include <getopt.h>
3
4 struct option opts[] = {
5 {"config", required_argument, NULL, 'f'},
6 {"help", no_argument, NULL, 'h'},
7 {"version", no_argument, NULL, 'v'}
8 };
9
10 int32_t main(int32_t argc, char **argv) {
11 char *configfile = NULL;
12 int32_t opt = 0;
13 //while ((opt = getopt(argc, argv, "f:hv")) != -1) {
14 while ((opt = getopt_long(argc, argv, "f:hv", opts, NULL)) != -1) {
15 switch (opt) {
16 case 'f':
17 configfile = strdup(optarg);
18 break;
19 case 'h':
20 LOG1("Usage: ./chatserver -hv | [-f configure_file]");
21 LOG1(" -f --config configure file");
22 LOG1(" -h --help help information");
23 LOG1(" -v --version help information");
24 return 0;
25 case 'v':
26 LOG1("version 1.0.0");
27 return 0;
28 default:
29 LOG1("Usage: ./chatserver -hv | [-f configure_file]");
30 LOG1(" -f --config configure file");
31 LOG1(" -h --help help information");
32 LOG1(" -v --version help information");
33 return -1;
34 }
35 }
36 return 0;
37 }

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.