在實(shí)際程序之中我們經(jīng)常要對(duì)命令行參數(shù)進(jìn)行分析. 比如我們有一個(gè)程序a可以接受許多參數(shù).一個(gè)可能的情況是
a -d print --option1 hello --option2 world
那么我們?nèi)绾螌?duì)這個(gè)命令的參數(shù)進(jìn)行分析了?.經(jīng)常用函數(shù)是getopt和getopt_long.
#include
#include
int getopt(int argc,char const **argv, const char *optstring);
int getopt_long(int argc,char const **argc,
const char *optstring,const struct option *longopts,
int *longindex);
extern char *optarg;
extern int optind,opterr,optopt;
struct option {
char *name;
int has_flag;
int *flag;
int value;
};
getopt_long是getopt的擴(kuò)展.getopt接受的命令行參數(shù)只可以是以(-)開(kāi)頭,而getopt_long還可以接受(--)開(kāi)頭的參數(shù).一般以(-)開(kāi)頭的參數(shù)的標(biāo)志只有一個(gè)字母,而以(--)開(kāi)頭的參數(shù)可以是一個(gè)字符串.如上面的 -d,--option1選項(xiàng).
argc,和argv參數(shù)是main函數(shù)的參數(shù).optstring指出了我們可以接受的參數(shù).其一般的形式為:參數(shù)1[:]參數(shù)2[:].... 其中參數(shù)是我們可以接受的參數(shù),如果后面的冒號(hào)沒(méi)有省略,那么表示這個(gè)參數(shù)出現(xiàn)時(shí)后面必需要帶參數(shù)值. 比如一個(gè)optstring為abc:d:表示這個(gè)參數(shù)選項(xiàng)可以為a,b,c,d其中c,d出現(xiàn)時(shí)候必須要有參數(shù)值.如果我們輸入了一個(gè)我們沒(méi)有提供的參數(shù)選項(xiàng).系統(tǒng)將會(huì)說(shuō) 不認(rèn)識(shí)的 選項(xiàng). getopt返回我們指定的參數(shù)選項(xiàng).同時(shí)將參數(shù)值保存在optarg中,如果已經(jīng)分析完成所有的參數(shù)函數(shù)返回-1.這個(gè)時(shí)候optind指出非可選參數(shù)的開(kāi)始位置.
#include
#include
int main(int argc,char **argv)
{
int is_a,is_b,is_c,is_d,i;
char *a_value,*b_value,*c_value,temp;
is_a=is_b=is_c=is_d=0;
a_value=b_value=c_value=NULL;
if(argc==1)
{
fprintf(stderr,"Usage:%s [-a value] [-b value] [-c value] [-d] arglist ...\n",
argv[0]);
exit(1);
}
while((temp=getopt(argc,argv,"a:b:c:d"))!=-1)
{
switch (temp)
{
case 'a':
is_a=1;
a_value=optarg;
break;
case 'b':
is_b=1;
b_value=optarg;
break;
case 'c':
is_c=1;
c_value=optarg;
break;
case 'd':
is_d=1;
break;
}
}
printf("Option has a:%s with value:%s\n",is_a?"YES":"NO",a_value);
printf("Option has b:%s with value:%s\n",is_b?"YES":"NO",b_value);
printf("Option has c:%s with value:%s\n",is_c?"YES":"NO",c_value);
printf("OPtion has d:%s\n",is_d?"YES":"NO");
i=optind;
while(argv[i]) printf(" with arg:%s\n",argv[i++]);
exit(0);
}
getopt_long比getopt復(fù)雜一點(diǎn),不過(guò)用途要比getopt廣泛.struct option 指出我們可以接受的附加參數(shù)選項(xiàng).
name:指出長(zhǎng)選項(xiàng)的名稱(chēng)(如我們的option1)
has_flag:為0時(shí)表示沒(méi)有參數(shù)值,當(dāng)為1的時(shí)候表明這個(gè)參數(shù)選項(xiàng)要接受一個(gè)參數(shù)值.為2時(shí)表示參數(shù)值可以有也可以沒(méi)有.
指出函數(shù)的返回值.如果為NULL,那么返回val,否則返回0.并將longindex賦值為選項(xiàng)所在數(shù)組(longopts)的位置.
a -d print --option1 hello --option2 world
那么我們?nèi)绾螌?duì)這個(gè)命令的參數(shù)進(jìn)行分析了?.經(jīng)常用函數(shù)是getopt和getopt_long.
#include
#include
int getopt(int argc,char const **argv, const char *optstring);
int getopt_long(int argc,char const **argc,
const char *optstring,const struct option *longopts,
int *longindex);
extern char *optarg;
extern int optind,opterr,optopt;
struct option {
char *name;
int has_flag;
int *flag;
int value;
};
getopt_long是getopt的擴(kuò)展.getopt接受的命令行參數(shù)只可以是以(-)開(kāi)頭,而getopt_long還可以接受(--)開(kāi)頭的參數(shù).一般以(-)開(kāi)頭的參數(shù)的標(biāo)志只有一個(gè)字母,而以(--)開(kāi)頭的參數(shù)可以是一個(gè)字符串.如上面的 -d,--option1選項(xiàng).
argc,和argv參數(shù)是main函數(shù)的參數(shù).optstring指出了我們可以接受的參數(shù).其一般的形式為:參數(shù)1[:]參數(shù)2[:].... 其中參數(shù)是我們可以接受的參數(shù),如果后面的冒號(hào)沒(méi)有省略,那么表示這個(gè)參數(shù)出現(xiàn)時(shí)后面必需要帶參數(shù)值. 比如一個(gè)optstring為abc:d:表示這個(gè)參數(shù)選項(xiàng)可以為a,b,c,d其中c,d出現(xiàn)時(shí)候必須要有參數(shù)值.如果我們輸入了一個(gè)我們沒(méi)有提供的參數(shù)選項(xiàng).系統(tǒng)將會(huì)說(shuō) 不認(rèn)識(shí)的 選項(xiàng). getopt返回我們指定的參數(shù)選項(xiàng).同時(shí)將參數(shù)值保存在optarg中,如果已經(jīng)分析完成所有的參數(shù)函數(shù)返回-1.這個(gè)時(shí)候optind指出非可選參數(shù)的開(kāi)始位置.
#include
#include
int main(int argc,char **argv)
{
int is_a,is_b,is_c,is_d,i;
char *a_value,*b_value,*c_value,temp;
is_a=is_b=is_c=is_d=0;
a_value=b_value=c_value=NULL;
if(argc==1)
{
fprintf(stderr,"Usage:%s [-a value] [-b value] [-c value] [-d] arglist ...\n",
argv[0]);
exit(1);
}
while((temp=getopt(argc,argv,"a:b:c:d"))!=-1)
{
switch (temp)
{
case 'a':
is_a=1;
a_value=optarg;
break;
case 'b':
is_b=1;
b_value=optarg;
break;
case 'c':
is_c=1;
c_value=optarg;
break;
case 'd':
is_d=1;
break;
}
}
printf("Option has a:%s with value:%s\n",is_a?"YES":"NO",a_value);
printf("Option has b:%s with value:%s\n",is_b?"YES":"NO",b_value);
printf("Option has c:%s with value:%s\n",is_c?"YES":"NO",c_value);
printf("OPtion has d:%s\n",is_d?"YES":"NO");
i=optind;
while(argv[i]) printf(" with arg:%s\n",argv[i++]);
exit(0);
}
getopt_long比getopt復(fù)雜一點(diǎn),不過(guò)用途要比getopt廣泛.struct option 指出我們可以接受的附加參數(shù)選項(xiàng).
name:指出長(zhǎng)選項(xiàng)的名稱(chēng)(如我們的option1)
has_flag:為0時(shí)表示沒(méi)有參數(shù)值,當(dāng)為1的時(shí)候表明這個(gè)參數(shù)選項(xiàng)要接受一個(gè)參數(shù)值.為2時(shí)表示參數(shù)值可以有也可以沒(méi)有.
指出函數(shù)的返回值.如果為NULL,那么返回val,否則返回0.并將longindex賦值為選項(xiàng)所在數(shù)組(longopts)的位置.