在實際程序之中我們經常要對命令行參數進行分析. 比如我們有一個程序a可以接受許多參數.一個可能的情況是
a -d print --option1 hello --option2 world
那么我們如何對這個命令的參數進行分析了?.經常用函數是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的擴展.getopt接受的命令行參數只可以是以(-)開頭,而getopt_long還可以接受(--)開頭的參數.一般以(-)開頭的參數的標志只有一個字母,而以(--)開頭的參數可以是一個字符串.如上面的 -d,--option1選項.
argc,和argv參數是main函數的參數.optstring指出了我們可以接受的參數.其一般的形式為:參數1[:]參數2[:].... 其中參數是我們可以接受的參數,如果后面的冒號沒有省略,那么表示這個參數出現時后面必需要帶參數值. 比如一個optstring為abc:d:表示這個參數選項可以為a,b,c,d其中c,d出現時候必須要有參數值.如果我們輸入了一個我們沒有提供的參數選項.系統(tǒng)將會說 不認識的 選項. getopt返回我們指定的參數選項.同時將參數值保存在optarg中,如果已經分析完成所有的參數函數返回-1.這個時候optind指出非可選參數的開始位置.
#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復雜一點,不過用途要比getopt廣泛.struct option 指出我們可以接受的附加參數選項.
name:指出長選項的名稱(如我們的option1)
has_flag:為0時表示沒有參數值,當為1的時候表明這個參數選項要接受一個參數值.為2時表示參數值可以有也可以沒有.
指出函數的返回值.如果為NULL,那么返回val,否則返回0.并將longindex賦值為選項所在數組(longopts)的位置.
a -d print --option1 hello --option2 world
那么我們如何對這個命令的參數進行分析了?.經常用函數是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的擴展.getopt接受的命令行參數只可以是以(-)開頭,而getopt_long還可以接受(--)開頭的參數.一般以(-)開頭的參數的標志只有一個字母,而以(--)開頭的參數可以是一個字符串.如上面的 -d,--option1選項.
argc,和argv參數是main函數的參數.optstring指出了我們可以接受的參數.其一般的形式為:參數1[:]參數2[:].... 其中參數是我們可以接受的參數,如果后面的冒號沒有省略,那么表示這個參數出現時后面必需要帶參數值. 比如一個optstring為abc:d:表示這個參數選項可以為a,b,c,d其中c,d出現時候必須要有參數值.如果我們輸入了一個我們沒有提供的參數選項.系統(tǒng)將會說 不認識的 選項. getopt返回我們指定的參數選項.同時將參數值保存在optarg中,如果已經分析完成所有的參數函數返回-1.這個時候optind指出非可選參數的開始位置.
#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復雜一點,不過用途要比getopt廣泛.struct option 指出我們可以接受的附加參數選項.
name:指出長選項的名稱(如我們的option1)
has_flag:為0時表示沒有參數值,當為1的時候表明這個參數選項要接受一個參數值.為2時表示參數值可以有也可以沒有.
指出函數的返回值.如果為NULL,那么返回val,否則返回0.并將longindex賦值為選項所在數組(longopts)的位置.

