getopt使用例子
2015-10-22 10:04
267 查看
getopt是linux下获取程序启动参数的函数 #include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;使用了全局参数optarg, optind,opterr, optopt这几个,其中optarg用来保存解析出来的选项参数下面是一个例子#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main(int argc, char** argv)
{
int opt = 0;
while(-1 != (opt = getopt(argc, argv, "n:t:kl")))
{
switch(opt)
{
case 'n':
fprintf(stdout, "n:%s\n", optarg);
break;
case 't':
fprintf(stdout, "t:%s\n", optarg);
break;
case 'k':
fprintf(stdout, "k option get\n");
break;
case 'l':
fprintf(stdout, "l option get\n");
break;
}
}
return 0;
}这段代码意图解析带有参数和不带有参数的命令选项,其中n和t和带有参数的,k和l是不带有参数的,getopt通过识别第三个参数的格式来区分,n和t后面有冒号,表示选项带有参数,k和l没有。测试一下这段代码对符合要求的命令行的解析和不符合要求的命令行的解析smi /project/bm3.5/smi/test/zd/self>tg -n 100 -t vi -k 30 -l 80
n:100
t:vi
k option get
l option get
smi /project/bm3.5/smi/test/zd/self>tg -n 100 -t vi -k -l
n:100
t:vi
k option get
l option get
smi /project/bm3.5/smi/test/zd/self>tg -n 100 -t -k -l
n:100
t:-k
l option get
smi /project/bm3.5/smi/test/zd/self>tg -n
tg: option requires an argument -- n
smi /project/bm3.5/smi/test/zd/self>tg -n -t
n:-tsmi /project/bm3.5/smi/test/zd/self>tg -o -n 100
tg: invalid option -- o
n:100可以看到,解析不尽如人意,在k和l带有多余参数的时候没有报错,这个还可以理解,但在n和t命令少带参数的时候就应该报错,而不是把后面的命令作为参数给解析了。多余的o命令报错还是处理正确的。这个命令使用比较方便,但有一定的局限性。
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;使用了全局参数optarg, optind,opterr, optopt这几个,其中optarg用来保存解析出来的选项参数下面是一个例子#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main(int argc, char** argv)
{
int opt = 0;
while(-1 != (opt = getopt(argc, argv, "n:t:kl")))
{
switch(opt)
{
case 'n':
fprintf(stdout, "n:%s\n", optarg);
break;
case 't':
fprintf(stdout, "t:%s\n", optarg);
break;
case 'k':
fprintf(stdout, "k option get\n");
break;
case 'l':
fprintf(stdout, "l option get\n");
break;
}
}
return 0;
}这段代码意图解析带有参数和不带有参数的命令选项,其中n和t和带有参数的,k和l是不带有参数的,getopt通过识别第三个参数的格式来区分,n和t后面有冒号,表示选项带有参数,k和l没有。测试一下这段代码对符合要求的命令行的解析和不符合要求的命令行的解析smi /project/bm3.5/smi/test/zd/self>tg -n 100 -t vi -k 30 -l 80
n:100
t:vi
k option get
l option get
smi /project/bm3.5/smi/test/zd/self>tg -n 100 -t vi -k -l
n:100
t:vi
k option get
l option get
smi /project/bm3.5/smi/test/zd/self>tg -n 100 -t -k -l
n:100
t:-k
l option get
smi /project/bm3.5/smi/test/zd/self>tg -n
tg: option requires an argument -- n
smi /project/bm3.5/smi/test/zd/self>tg -n -t
n:-tsmi /project/bm3.5/smi/test/zd/self>tg -o -n 100
tg: invalid option -- o
n:100可以看到,解析不尽如人意,在k和l带有多余参数的时候没有报错,这个还可以理解,但在n和t命令少带参数的时候就应该报错,而不是把后面的命令作为参数给解析了。多余的o命令报错还是处理正确的。这个命令使用比较方便,但有一定的局限性。
相关文章推荐
- Optimistic and Pessimistic locking
- Linux逐行读取文件内容
- 大型网站技术架构演化笔记
- Nginx基础入门之nginx部署前准备工作
- hasOwnProperty()与for in的区别
- Linux基础学习4
- Linux下scp命令的用法
- linux的cgroup控制
- opencv2-XML和YAML文件的写入
- Hash/index nest loop/merge sort join key简介
- 64位ubuntu15.04升级Linux内核到linux4.1.0
- Zero Copy初探
- 提升网站性能开发的10个技巧
- 卸载linux订阅包
- Linux rpm 命令参数使用详解[介绍和应用]
- Python+Apache+django配置
- 如何使用Weave以及Docker搭建Nginx反向代理/负载均衡服务器
- 最小化安装linux CentOS-6.6后 部署fastdfs +下载地址 很干很干的干货
- 信号发送函数(2)
- 一个很好的源码下载学习网站