getopt函数解析
2015-10-22 09:21
218 查看
getopt(分析命令行参数)
相关函数表头文件
#include<unistd.h>
定义函数
int getopt(int argc,char * const argv[ ],const char * optstring);
函数说明
getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
短参数的定义
getopt()使用optstring所指的字串作为短参数列表,象"1ac:d::"就是一个短参数列表。短参数的定义是一个'-'后面跟一个字母或数字,象-a, -b就是一个短参数。每个数字或字母定义一个参数。
其中短参数在getopt定义里分为三种:
1. 不带值的参数,它的定义即是参数本身。
2. 必须带值的参数,它的定义是在参数本身后面再加一个冒号。
3. 可选值的参数,它的定义是在参数本身后面加两个冒号 。
在这里拿上面的"1ac:d::"作为样例进行说明,其中的1,a就是不带值的参数,c是必须带值的参数,d是可选值的参数。
在实际调用中,'-1 -a -c cvalue -d', '-1 -a -c cvalue -ddvalue', '-1a -ddvalue -c cvalue'都是合法的。这里需要注意三点:
1. 不带值的参数可以连写,象1和a是不带值的参数,它们可以-1 -a分开写,也可以-1a或-a1连写。
2. 参数不分先后顺序,'-1a -c cvalue -ddvalue'和'-d -c cvalue -a1'的解析结果是一样的。
3. 要注意可选值的参数的值与参数之间不能有空格,必须写成-ddvalue这样的格式,如果写成-d dvalue这样的格式就会解析错误。
返回值
getopt()每次调用会逐次返回命令行传入的参数。
当没有参数的最后的一次调用时,getopt()将返回-1。
当解析到一个不在optstring里面的参数,或者一个必选值参数不带值时,返回'?'。
当optstring是以':'开头时,缺值参数的情况下会返回':',而不是'?' 。
例子:
#include <stdio.h>
#include <unistd.h>
int main(int argc, int *argv[])
{
int ch;
opterr = 0;
while ((ch = getopt(argc,argv,"a:bcde"))!=-1)
{
switch(ch)
{
case 'a':
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);
}
执行结果:
$ ./getopt -a
other option :?
optopt +a
$ ./getopt -b
option b :b
optopt +
$ ./getopt -c
other option :c
optopt +
$ ./getopt -d
other option :d
optopt +
$ ./getopt -abcd
option a:'bcd'
optopt +
$ ./getopt -bcd
option b :b
other option :c
other option :d
optopt +
$ ./getopt -bcde
option b :b
other option :c
other option :d
other option :e
optopt +
$ ./getopt -bcdef
option b :b
other option :c
other option :d
other option :e
other option :?
optopt +f
相关函数表头文件
#include<unistd.h>
定义函数
int getopt(int argc,char * const argv[ ],const char * optstring);
函数说明
getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
短参数的定义
getopt()使用optstring所指的字串作为短参数列表,象"1ac:d::"就是一个短参数列表。短参数的定义是一个'-'后面跟一个字母或数字,象-a, -b就是一个短参数。每个数字或字母定义一个参数。
其中短参数在getopt定义里分为三种:
1. 不带值的参数,它的定义即是参数本身。
2. 必须带值的参数,它的定义是在参数本身后面再加一个冒号。
3. 可选值的参数,它的定义是在参数本身后面加两个冒号 。
在这里拿上面的"1ac:d::"作为样例进行说明,其中的1,a就是不带值的参数,c是必须带值的参数,d是可选值的参数。
在实际调用中,'-1 -a -c cvalue -d', '-1 -a -c cvalue -ddvalue', '-1a -ddvalue -c cvalue'都是合法的。这里需要注意三点:
1. 不带值的参数可以连写,象1和a是不带值的参数,它们可以-1 -a分开写,也可以-1a或-a1连写。
2. 参数不分先后顺序,'-1a -c cvalue -ddvalue'和'-d -c cvalue -a1'的解析结果是一样的。
3. 要注意可选值的参数的值与参数之间不能有空格,必须写成-ddvalue这样的格式,如果写成-d dvalue这样的格式就会解析错误。
返回值
getopt()每次调用会逐次返回命令行传入的参数。
当没有参数的最后的一次调用时,getopt()将返回-1。
当解析到一个不在optstring里面的参数,或者一个必选值参数不带值时,返回'?'。
当optstring是以':'开头时,缺值参数的情况下会返回':',而不是'?' 。
例子:
#include <stdio.h>
#include <unistd.h>
int main(int argc, int *argv[])
{
int ch;
opterr = 0;
while ((ch = getopt(argc,argv,"a:bcde"))!=-1)
{
switch(ch)
{
case 'a':
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);
}
执行结果:
$ ./getopt -a
other option :?
optopt +a
$ ./getopt -b
option b :b
optopt +
$ ./getopt -c
other option :c
optopt +
$ ./getopt -d
other option :d
optopt +
$ ./getopt -abcd
option a:'bcd'
optopt +
$ ./getopt -bcd
option b :b
other option :c
other option :d
optopt +
$ ./getopt -bcde
option b :b
other option :c
other option :d
other option :e
optopt +
$ ./getopt -bcdef
option b :b
other option :c
other option :d
other option :e
other option :?
optopt +f
相关文章推荐
- linux逻辑卷管理LVM
- 如何在Eclipse中添加Tomcat的jar包
- 通过IIS发布网站
- Linux 有问必答:如何在 Linux 中永久修改 USB 设备权限
- linux find grep组合使用
- (转)大型网站架构演化发展历程
- Apache Rewrite如何传递GET参数的问题
- nginx.conf简单配置
- Weka报错"Unknow data type INT. Add entry in weka/experiment/DatabaseUtils.props"
- fatal error C1083: Cannot open type library file: 'swpublished.tlb': No such file or directory 解决办法
- linux的free命令
- 开发 linux内核的技术路线
- 每天一个linux命令(17):whereis 命令
- 在 Linux 下使用 RAID(八):当软件 RAID 故障时如何恢复和重建数据
- 在 Linux 下使用 RAID(八):当软件 RAID 故障时如何恢复和重建数据
- ShopEX 4.8.5.81822 前台Getshell
- PXE自动安装CentOS7详解
- Linux系统使用date命令修改系统时间
- shell Unversal framework修复方案
- Linux和windows 的启动流程比较