getopt的用法与optarg
getopt的用法與optarg
2014-03-06 20:14 2084人閱讀 評(píng)論(0) 收藏 舉報(bào) 分類: Mac開發(fā)(29)getopt被用來解析命令行選項(xiàng)參數(shù)。就不用自己寫東東處理argv了。
#include <unistd.h>
extern char *optarg;??//選項(xiàng)的參數(shù)指針
extern int optind, ??//下一次調(diào)用getopt的時(shí),從optind存儲(chǔ)的位置處重新開始檢查選項(xiàng)。?
extern int opterr,??//當(dāng)opterr=0時(shí),getopt不向stderr輸出錯(cuò)誤信息。
extern int optopt;??//當(dāng)命令行選項(xiàng)字符不包括在optstring中或者選項(xiàng)缺少必要的參數(shù)時(shí),該選項(xiàng)存儲(chǔ)在optopt中,getopt返回'?’、
int getopt(int argc, char * const argv[], const char *optstring);
調(diào)用一次,返回一個(gè)選項(xiàng)。 在命令行選項(xiàng)參數(shù)再也檢查不到optstring中包含的選項(xiàng)時(shí),返回-1,同時(shí)optind儲(chǔ)存第一個(gè)不包含選項(xiàng)的命令行參數(shù)。
首先說一下什么是選項(xiàng),什么是參數(shù)。
字符串optstring可以下列元素,
1.單個(gè)字符,表示選項(xiàng),
2.單個(gè)字符后接一個(gè)冒號(hào):表示該選項(xiàng)后必須跟一個(gè)參數(shù)。參數(shù)緊跟在選項(xiàng)后或者以空格隔開。該參數(shù)的指針賦給optarg。
3 單個(gè)字符后跟兩個(gè)冒號(hào),表示該選項(xiàng)后必須跟一個(gè)參數(shù)。參數(shù)必須緊跟在選項(xiàng)后不能以空格隔開。該參數(shù)的指針賦給optarg。(這個(gè)特性是GNU的擴(kuò)張)。
getopt處理以'-’開頭的命令行參數(shù),如optstring="ab:c::d::",命令行為getopt.exe -a -b host -ckeke -d haha
在這個(gè)命令行參數(shù)中,-a和-h就是選項(xiàng)元素,去掉'-',a,b,c就是選項(xiàng)。host是b的參數(shù),keke是c的參數(shù)。但haha并不是d的參數(shù),因?yàn)樗鼈冎虚g有空格隔開。
還要注意的是默認(rèn)情況下getopt會(huì)重新排列命令行參數(shù)的順序,所以到最后所有不包含選項(xiàng)的命令行參數(shù)都排到最后。
如getopt.exe -a ima -b host -ckeke -d haha, 都最后命令行參數(shù)的順序是: -a -b host -ckeke -d ima haha
如果optstring中的字符串以'+'加號(hào)開頭或者環(huán)境變量POSIXLY_CORRE被設(shè)置。那么一遇到不包含選項(xiàng)的命令行參數(shù),getopt就會(huì)停止,返回-1。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int result;
??? opterr = 0;? //使getopt不行stderr輸出錯(cuò)誤信息
??? while( (result = getopt(argc, argv, "ab:c::")) != -1 )
{
switch(result)
{
case 'a':
printf("option=a, optopt=%c, optarg=%s\n", optopt, optarg);
break;
case 'b':
printf("option=b, optopt=%c, optarg=%s\n", optopt, optarg);
break;
case 'c':
printf("option=c, optopt=%c, optarg=%s\n", optopt, optarg);
break;
case '?':
printf("result=?, optopt=%c, optarg=%s\n", optopt, optarg);
break;
default:
printf("default, result=%c\n",result);
break;
}
printf("argv[%d]=%s\n", optind, argv[optind]);
}
printf("result=-1, optind=%d\n", optind);?? //看看最后optind的位置
??? for(result = optind; result < argc; result++)
printf("-----argv[%d]=%s\n", result, argv[result]);
for(result = 1; result < argc; result++)
printf("\nat the end-----argv[%d]=%s\n", result, argv[result]);
return 0;
}
unistd里有個(gè) optind 變量,每次getopt后,這個(gè)索引指向argv里當(dāng)前分析的字符串的下一個(gè)索引,因此
argv[optind]就能得到下個(gè)字符串,通過判斷是否以 '-'開頭就可。下面是個(gè)測(cè)試程序
#include <stdio.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
int tmp = 4;
while( (tmp = getopt(argc, argv, "abck")) != -1??)
{
printf("-%c\t", tmp);
int opt = optind ;
while( opt < argc )
{
if ( argv[opt][0] != '-' )
{
printf("%s\t", argv[opt]);
opt ++;
}
else
break;
}
printf("\n");
}
getchar();
}
函數(shù)說明??getopt()用來分析命令行參數(shù)。參數(shù)argc和argv是由main()傳遞的參數(shù)個(gè)數(shù)和內(nèi)容。參數(shù)optstring 則代表欲處理的選項(xiàng)字符串。此函數(shù)會(huì)返回在argv 中下一個(gè)的選項(xiàng)字母,此字母會(huì)對(duì)應(yīng)參數(shù)optstring 中的字母。如果選項(xiàng)字符串里的字母后接著冒號(hào)“:”,則表示還有相關(guān)的參數(shù),全域變量optarg 即會(huì)指向此額外參數(shù)。如果getopt()找不到符合的參數(shù)則會(huì)印出錯(cuò)信息,并將全域變量optopt設(shè)為“?”字符,如果不希望getopt()印出錯(cuò) 信息,則只要將全域變量opterr設(shè)為0即可。
返回值??如果找到符合的參數(shù)則返回此參數(shù)字母,如果參數(shù)不包含在參數(shù)optstring 的選項(xiàng)字母則返回“?”字符,分析結(jié)束則返回-1。
范例??#include<stdio.h>
#include<unistd.h>
int main(int argc,char **argv)
{
int ch;
opterr = 0;
while((ch = getopt(argc,argv,”a:bcde”))!= -1)
switch(ch)
{
case ‘a(chǎn)’:
printf(“option a:’%s’\n”,optarg);
break;
case ‘b’:
printf(“option b :b\n”);
break;
default:
printf(“other option :%c\n”,ch);
}
printf(“optopt +%c\n”,optopt);
}
執(zhí)行??$./getopt –b
option b:b
$./getopt –c
other option:c
$./getopt –a
other option :?
$./getopt –a12345
option a:’12345’
總結(jié)
以上是生活随笔為你收集整理的getopt的用法与optarg的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell脚本tr
- 下一篇: python scatter 简书_写给