get_opt
2015-11-04 09:23
253 查看
getopt()函数声明如下:
该函数的argc和argv参数通常直接从main()的参数直接传递而来。optstring是选项字母组成的字串。如果该字串里的任一字符后面有冒号,那么这个选项就要求有选项参数。
当给定getopt()命令参数的数量 (
int opterr——这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
(一)
在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如:
./a.out -a1234 -b432 -c -d
程序会根据读取的参数执行相应的操作,在C语言中,这个功能一般是靠getopt()这个函数,结合switch语句来完成的,首先来看下面的代码:
#include <stdio.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int ch;
opterr=0;
while((ch=getopt(argc,argv,"a:b::cde"))!=-1)
{
printf("optind:%d\n",optind);
printf("optarg:%s\n",optarg);
printf("ch:%c\n",ch);
switch(ch)
{
case 'a':
printf("option a:'%s'\n",optarg);
break;
case 'b':
printf("option b:'%s'\n",optarg);
break;
case 'c':
printf("option c\n");
break;
case 'd':
printf("option d\n");
break;
case 'e':
printf("option e\n");
break;
default:
printf("other option:%c\n",ch);
}
printf("optopt+%c\n",optopt);
}
}
用gcc编译后,在终端行执行以上的命令:
./a.out -a1234 -b432 -c -d
则会有如下的输出:
optind:2
optarg:1234
ch:a
option a:'1234'
optopt+
optind:3
optarg:432
ch:b
option b:'432'
optopt+
optind:4
optarg:(null)
ch:c
option c
optopt+
optind:5
optarg:(null)
ch:d
option d
optopt+
要理解getopt()函数的作用,首先要清楚带参数的main()函数的使用:
main(int argc,char *argv[])中的argc是一个整型,argv是一个指针数组,argc记录argv的大小。上面的例子中。
argc=5;
argv[0]=./a.out
argv[1]=-a1234
argv[2]=-b432
argv[3]=-c
argv[4]=-d
getopt()函数的原型为getopt(int argc,char *const argv[],const char *optstring)。
其中argc和argv一般就将main函数的那两个参数原样传入。
optstring是一段自己规定的选项串,例如本例中的"a:b::cde",表示可以有,-a,-b,-c,-d,-e这几个参数。
“:”表示必须该选项带有额外的参数,全域变量optarg会指向此额外参数,“::”标识该额外的参数可选(有些Uinx可能不支持“::”)。
全域变量optind指示下一个要读取的参数在argv中的位置。
如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符。
如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
(二)
#include <unistd.h>
extern char *optarg; //选项的参数指针
extern int optind, //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
extern int opterr, //当opterr=0时,getopt不向stderr输出错误信息。
extern int optopt; //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt中,getopt返回'?’、
int getopt(int argc, char * const argv[], const char *optstring);
调用一次,返回一个选项。 在命令行选项参数再也检查不到optstring中包含的选项时,返回-1,同时optind储存第一个不包含选项的命令行参数。
首先说一下什么是选项,什么是参数。
字符串optstring可以下列元素,
1.单个字符,表示选项,
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
3 单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
getopt处理以'-’开头的命令行参数,如optstring="ab:c::d::",命令行为getopt.exe -a -b host -ckeke -d haha
在这个命令行参数中,-a和-h就是选项元素,去掉'-',a,b,c就是选项。host是b的参数,keke是c的参数。但haha并不是d的参数,因为它们中间有空格隔开。
还要注意的是默认情况下getopt会重新排列命令行参数的顺序,所以到最后所有不包含选项的命令行参数都排到最后。
如getopt.exe -a ima -b host -ckeke -d haha, 都最后命令行参数的顺序是: -a -b host -ckeke -d ima haha
如果optstring中的字符串以'+'加号开头或者环境变量POSIXLY_CORRE被设置。那么一遇到不包含选项的命令行参数,getopt就会停止,返回-1。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int result;
opterr = 0; //使getopt不行stderr输出错误信息
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里有个 optind 变量,每次getopt后,这个索引指向argv里当前分析的字符串的下一个索引,因此
argv[optind]就能得到下个字符串,通过判断是否以 '-'开头就可。下面是个测试程序
#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();
}
函数说明 getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
返回值 如果找到符合的参数则返回此参数字母,如果参数不包含在参数optstring 的选项字母则返回“?”字符,分析结束则返回-1。
#include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; |
当给定getopt()命令参数的数量 (
argc)、指向这些参数的数组 (
argv) 和选项字串 (
optstring) 后,
getopt()将返回第一个选项,并设置一些全局变量。使用相同的参数再次调用该函数时,它将返回下一个选项,并设置相应的全局变量。如果不再有可识别的选项,将返回
-1,此任务就完成了。
getopt()所设置的全局变量包括:
char *optarg——当前选项参数字串(如果有)。
int optind——argv的当前索引值。当getopt()在while循环中使用时,循环结束后,剩下的字串视为操作数,在argv[optind]至argv[argc-1]中可以找到。
int opterr——这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
int optopt——当发现无效选项字符之时,getopt()函数或返回'?'字符,或返回':'字符,并且optopt包含了所发现的无效选项字符。
(一)
在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如:
./a.out -a1234 -b432 -c -d
程序会根据读取的参数执行相应的操作,在C语言中,这个功能一般是靠getopt()这个函数,结合switch语句来完成的,首先来看下面的代码:
#include <stdio.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int ch;
opterr=0;
while((ch=getopt(argc,argv,"a:b::cde"))!=-1)
{
printf("optind:%d\n",optind);
printf("optarg:%s\n",optarg);
printf("ch:%c\n",ch);
switch(ch)
{
case 'a':
printf("option a:'%s'\n",optarg);
break;
case 'b':
printf("option b:'%s'\n",optarg);
break;
case 'c':
printf("option c\n");
break;
case 'd':
printf("option d\n");
break;
case 'e':
printf("option e\n");
break;
default:
printf("other option:%c\n",ch);
}
printf("optopt+%c\n",optopt);
}
}
用gcc编译后,在终端行执行以上的命令:
./a.out -a1234 -b432 -c -d
则会有如下的输出:
optind:2
optarg:1234
ch:a
option a:'1234'
optopt+
optind:3
optarg:432
ch:b
option b:'432'
optopt+
optind:4
optarg:(null)
ch:c
option c
optopt+
optind:5
optarg:(null)
ch:d
option d
optopt+
要理解getopt()函数的作用,首先要清楚带参数的main()函数的使用:
main(int argc,char *argv[])中的argc是一个整型,argv是一个指针数组,argc记录argv的大小。上面的例子中。
argc=5;
argv[0]=./a.out
argv[1]=-a1234
argv[2]=-b432
argv[3]=-c
argv[4]=-d
getopt()函数的原型为getopt(int argc,char *const argv[],const char *optstring)。
其中argc和argv一般就将main函数的那两个参数原样传入。
optstring是一段自己规定的选项串,例如本例中的"a:b::cde",表示可以有,-a,-b,-c,-d,-e这几个参数。
“:”表示必须该选项带有额外的参数,全域变量optarg会指向此额外参数,“::”标识该额外的参数可选(有些Uinx可能不支持“::”)。
全域变量optind指示下一个要读取的参数在argv中的位置。
如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符。
如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
(二)
#include <unistd.h>
extern char *optarg; //选项的参数指针
extern int optind, //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
extern int opterr, //当opterr=0时,getopt不向stderr输出错误信息。
extern int optopt; //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt中,getopt返回'?’、
int getopt(int argc, char * const argv[], const char *optstring);
调用一次,返回一个选项。 在命令行选项参数再也检查不到optstring中包含的选项时,返回-1,同时optind储存第一个不包含选项的命令行参数。
首先说一下什么是选项,什么是参数。
字符串optstring可以下列元素,
1.单个字符,表示选项,
2.单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
3 单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩张)。
getopt处理以'-’开头的命令行参数,如optstring="ab:c::d::",命令行为getopt.exe -a -b host -ckeke -d haha
在这个命令行参数中,-a和-h就是选项元素,去掉'-',a,b,c就是选项。host是b的参数,keke是c的参数。但haha并不是d的参数,因为它们中间有空格隔开。
还要注意的是默认情况下getopt会重新排列命令行参数的顺序,所以到最后所有不包含选项的命令行参数都排到最后。
如getopt.exe -a ima -b host -ckeke -d haha, 都最后命令行参数的顺序是: -a -b host -ckeke -d ima haha
如果optstring中的字符串以'+'加号开头或者环境变量POSIXLY_CORRE被设置。那么一遇到不包含选项的命令行参数,getopt就会停止,返回-1。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int result;
opterr = 0; //使getopt不行stderr输出错误信息
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里有个 optind 变量,每次getopt后,这个索引指向argv里当前分析的字符串的下一个索引,因此
argv[optind]就能得到下个字符串,通过判断是否以 '-'开头就可。下面是个测试程序
#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();
}
函数说明 getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
返回值 如果找到符合的参数则返回此参数字母,如果参数不包含在参数optstring 的选项字母则返回“?”字符,分析结束则返回-1。
相关文章推荐
- CentOS 6.5下Redmine的安装配置
- Basic Operation about Linux
- Linux文件管理命令
- Linux线程池C++封装实现
- Docker常用指令及使用场景(不定时补充)
- LInux : du命令
- 每天一个linux命令(34):du 命令
- 浅析.NET逻辑分层架构
- .net 分布式架构之任务调度平台
- Linux中的free命令
- 在CentOS中,yum install 时候出现Another app is currently holding the yum lock解决方法
- eclipse下的servers中的tomcat消失
- 各大招聘网站信息实时查询浏览【二】
- Recover the home data frome Linux
- 错误现象:Provider org.apache.xalan.processor.TransformerFactoryImpl not found解决方案包
- ubuntu13.4安装openssh-server
- Linux-awk学习笔记
- Linux安装ss5详细步骤
- EC读书笔记系列之6:条款11 在operator=中处理自我赋值
- 几条shell命令