Linux编程里getopt_long_only函数用法详解
生活随笔
收集整理的這篇文章主要介紹了
Linux编程里getopt_long_only函数用法详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在程序中難免需要使用命令行選項,可以選擇自己解析命令行選項,但是有現成的,何必再造輪子。下面介紹使用getopt_long_only和getopt_long(
兩者用法差不多
)解析命令行選項。
#include?<getopt.h>?//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);???
struct?option???? {???? ??const?char?*name;//長選項名???? ??int?has_arg;//是否需要參數???? ??int?*flag;???? ??int?val;???? };???? name:長選項名字?
has_arg:是否需要參數。值有三種情況
#?define?no_argument????????0????//不需要參數???? #?define?required_argument??1????//必須指定參數???? #?define?optional_argument??2????//參數可選???? flag和val相互依賴,主要分兩種情況: (1)、flag為NULL,val值用于確定該長選項,所以需要為長選項指定唯一的val值。這里也為長選項和短選項建立了橋梁。 ? ? ? ? ? ? 實際就是用于長選項和短選項的對應關系 (2)、flag不為NULL,則將val值存放到flag所指向的存儲空間,用于標識該長選項出現過。
實例一:
#include?<stdio.h>???? #include?<stdlib.h>???? #include?<getopt.h>?//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_only(argc,argv,optstring,opts,&index))!=-1)???? ????{???? ???????????? ????????switch(c)???? ????????{???? ????????case?'n'://-n?或者?--username?指定用戶名???? ????????????printf("username?is?%s\n",optarg);???? ????????????break;???? ????????case?'v'://-v?或者--version,輸出版本號???? ????????????printf("version?is?0.0.1?\n");???? ????????????break;???? ????????case?0://flag不為NULL???? ????????????printf("debug?is?%d\n",deb);???? ????????????break;???? ????????case?'?'://選項未定義???? ????????????printf("?\n");???? ????????????break;???? ????????default:???? ????????????printf("c?is?%d\n",c);???? ????????????break;???? ????????}???? ????}???? ????return?0;???? }????
實例二:
#include?<stdio.h>?? #include?<stdlib.h>?? #include?<getopt.h>?//getopt_long()頭文件位置?? ?? int?main(int?argc,?char**?argv)?? {?? ????const?char?*optstring="n:v";?? ????int?c,deb,index;?? ????struct?option?opts[]={{"username",required_argument,0,0},?? ??????????????????????????{"n",required_argument,0,0},?? ??????????????????????????{"version",no_argument,0,0},?? ??????????????????????????{"v",no_argument,0,0},?? ??????????????????????????{"debug",no_argument,0,0},?? ??????????????????????????{"d",no_argument,0,0},?? ??????????????????????????{"help",no_argument,0,0},?? ??????????????????????????{"h",no_argument,0,0}};?? ????while((c=getopt_long_only(argc,argv,optstring,opts,&index))!=-1)?? ????{?? ????????switch(index){?? ????????????//-n或者--username?? ????????????case?0:?? ????????????case?1:?? ????????????????printf("username:%s\n",optarg);?? ????????????????break;?? ????????????//-v或者--version?? ????????????case?2:?? ????????????case?3:?? ????????????????printf("version:1.0.0\n");?? ????????????????break;?? ????????????//-d?or?--debug?? ????????????case?4:?? ????????????case?5:?? ????????????????printf("debug:yes\n");?? ????????????????break;?? ????????????//-h?or?--help?? ????????????case?6:?? ????????????case?7:?? ????????????????printf("Help:?\n");?? ????????????????break;?? ????????????default:?? ????????????????printf("other:%d\n",index);?? ????????????????break;?? ????????}?? ????}?? ????return?0;?? }??
至于運行結果,我只給出用例,大家有興趣可以試試。
實例1:./test -n boy
實例2:./test -n boy 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
程序中主要使用:
| 短選項 | 長選項 | 是否需要參數 | |
| -n | --username | 是(用戶名) | 指定用戶名 |
| -d | --debug | 否 | 是否已測試 |
1、函數出處?
2、參數介紹
- argc argv :直接從main函數傳遞而來
- shortopts:短選項字符串。如”n:v",這里需要指出的是,短選項字符串不需要‘-’(經測試需要),而且但選項需要傳遞參數時,在短選項后面加上“:”。
- longopts:struct option 數組,用于存放長選項參數。
- longind:用于返回長選項在longopts結構體數組中的索引值,用于調試。一般置為NULL
has_arg:是否需要參數。值有三種情況
3、返回值
- 程序中使用短選項,則返回短選項字符(如‘n'),當需要參數是,則在返回之前將參數存入到optarg中。
- 程序中使用長選項,返回值根據flag和val確定。當flag為NULL,則返回val值。所以根據val值做不同的處理,這也說明了val必須唯一。當val值等于短選項值,則可以使用短選項解析函數解析長選項;當flag不為NULL,則將val值存入flag所指向的存儲空間,getopt_long返回0
- 出現未定義的長選項或者短選項,getopt_long返回?
- 解析完畢,getopt_long返回-1
4、實例
理論要與實際相結合實例一:
實例二:
至于運行結果,我只給出用例,大家有興趣可以試試。
實例1:./test -n boy
實例2:./test -n boy 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的Linux编程里getopt_long_only函数用法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 支付宝和微信的区别,以及各自的优缺点
- 下一篇: 2019年新规11月起施行!与老百姓的生