linux解析命令列選項getopt

來源:互聯網
上載者:User

   linux解析命令列選項getopt_long用法

  在程式中難免需要使用命令列選項,可以選擇自己解析命令列選項,但是有現成的,何必再造輪子。

  下面介紹使用getopt_long解析命令列選項。

  程式中主要使用:

  短選項 長選項 是否需要參數 備忘

  -v --version 否 查詢版本號碼

  -n --name 是(使用者名稱) 指定使用者

  -d --debug 否 是否已測試

  1、函數出處

  複製代碼

  代碼如下:

  [cpp]

  #include //getopt_long()標頭檔位置

  int getopt_long (int ___argc, char *const *___argv,

  const char *__shortopts,

  const struct option *__longopts, int *__longind);

  int getopt_long_only (int ___argc, char *const *___argv,

  const char *__shortopts,

  const struct option *__longopts, int *__longind);

  2、參數介紹

  argc argv :直接從main函數傳遞而來

  shortopts:短選項字串。如”n:v",這裡需要指出的是,短選項字串不需要‘-’,而且但選項需要傳遞參數時,在短選項後面加上“:”。

  longopts:struct option 數組,用於存放長選項參數。

  longind:用於返回長選項在longopts結構體數組中的索引值,用於調試。一般置為NULL

  下面介紹struct option

  複製代碼

  代碼如下:

  [cpp]

  struct option

  {

  const char *name;//長選項名

  int has_arg;//是否需要參數

  int *flag;

  int val;

  };

  name:長選項名字

  has_arg:是否需要參數。值有三種情況

  複製代碼

  代碼如下:

  [cpp]

  # define no_argument 0 //不需要參數

  # define required_argument 1 //必須指定參數

  # define optional_argument 2 //參數可選

  flag 和val

  flag和val相互依賴,主要分兩種情況:

  (1)、flag為NULL,val值用於確定該長選項,所以需要為長選項指定唯一的val值。這裡也為長選項和短選項建立了橋樑。

  (2)、flag不為NULL,則將val值存放到flag所指向的儲存空間,用於標識該長選項出現過。

  3、傳回值

  程式中使用短選項,則返回短選項字元(如‘n'),當需要參數是,則在返回之前將參數存入到optarg中。

  程式中使用長選項,傳回值根據flag和val確定。當flag為NULL,則返回val值。所以根據val值做不同的處理,這也說明了val必須唯一。當val值等於短選項值,則可以使用短選項解析函數解析長選項;當flag不為NULL,則將val值存入flag所指向的儲存空間,getopt_long返回0

  出現未定義的長選項或者短選項,getopt_long返回?

  解析完畢,getopt_long返回-1

  4、執行個體

  理論要與實際相結合

  複製代碼

  代碼如下:

  [cpp]

  #include

  #include

  #include //getopt_long()標頭檔位置

  int main(int argc, char** argv)

  {

  const char *optstring="n:v";

  int c,deb,index;

  struct option opts[]={{"username",required_argument,NULL,'n'},

  {"version",no_argument,NULL,'v'},

  {"debug",no_argument,&deb,1},

  {0,0,0,0}};

  while((c=getopt_long(argc,argv,optstring,opts,&index))!=-1)

  {

  switch(c)

  {

  case 'n'://-n 或者 --username 指定使用者名稱

  printf("username is %sn",optarg);

  break;

  case 'v'://-v 或者--version,輸出版本號碼

  printf("version is 0.0.1 n");

  break;

  case 0://flag不為NULL

  printf("debug is %dn",deb);

  break;

  case '?'://選項未定義

  printf("?n");

  break;

  default:

  printf("c is %dn",c);

  break;

  }

  }

  return 0;

  }

  運行截圖:

  說明一下:getopt_long_only,這個函數功能與getopt_long相同,只是可以使用’-‘後面跟長選項名,如./main -username jackie

聯繫我們

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