[c language] getopt
定义函数
函数说明
- #include <stdio.h>
- #include <unistd.h>
- int main(int argc, char **argv)
- {
- int ch;
- opterr = 0;
- while ((ch = getopt(argc,argv,"a:bcde::f"))!=-1)
- {
- switch(ch)
- {
- case 'a':
- printf("option a:'%s'\n",optarg);
- break;
- case 'b':
- printf("option b :b\n");
- break;
- case 'e':
- printf("option e:'%s'\n",optarg);
- break;
- default:
- printf("other option :%c\n",ch);
- }
- }
- printf("optopt +%c\n",optopt);
- return 1;
- }
运行结果:
- ~ Home$ ./c -a
- other option :?
- optopt +a
- ~ Home$ ./c -a
- option a:''
- optopt +a
- ~ Home$ ./c -a123
- option a:''
- optopt +a
- ~ Home$ ./c -b
- option b :b
- optopt +b
- ~ Home$ ./c -cde
- other option :c
- other option :d
- other option :?
- optopt +e
- ~ Home$ ./c -e
- other option :?
- optopt +e
- ~ Home$ ./c -e123
- option e:''
- optopt +e
- ~ Home$ ./c -e
- option e:''
- optopt +e
- ~ Home$ ./c -f
- other option :f
- optopt +f
- ~ Home$ ./c -a -bcdf -e
- option a:''
- option b :b
- other option :c
- other option :d
- other option :f
- option e:''
- optopt +e
当optstring(":a:bcde::f")是以':'开头时,缺值参数的情况下会返回':',而不是'?' 。
- 1 $ ./c -a
- 2 other option ::
- 3 optopt +a
可变optstring
- #include <stdio.h>
- #include <unistd.h>
- #ifdef RDP2
- #define VNCOPT "V:Q"
- #else
- #define VNCOPT "V:"
- #endif
- int main(int argc, char **argv)
- {
- int ch;
- opterr = ;
- while ((ch = getopt(argc,argv, VNCOPT ":a:bcde::f"))!=-)
- {
- switch(ch)
- {
- case 'V':
- printf("option V:'%s'\n",optarg);
- break;
- case 'a':
- printf("option a:'%s'\n",optarg);
- break;
- case 'b':
- printf("option b :b\n");
- break;
- case 'e':
- printf("option e:'%s'\n",optarg);
- break;
- default:
- printf("other option :%c\n",ch);
- }
- }
- printf("optopt +%c\n",optopt);
- return ;
- }
reference:
http://vopit.blog.51cto.com/2400931/440453
[c language] getopt的更多相关文章
- [c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释
getopt被用来解析命令行选项参数.#include <unistd.h> extern char *optarg; //选项的参数指针extern int optind, //下一次调 ...
- go语言项目汇总
Horst Rutter edited this page 7 days ago · 529 revisions Indexes and search engines These sites prov ...
- Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目
Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...
- 使用 getopt() 进行命令行处理
引言 在早期的 UNIX® 中,其命令行环境(当时的唯一用户界面)包含着数十种小的文本处理工具.这些工具非常小,通常可很好地完成一项工作.这些工具通过较长的命令管道链接在一起,前面的程序将其输出传递给 ...
- [记录]Shell中的getopts和getopt用法
Shell中的getopts和getopt用法 1.getopts getopts(shell内置命令)不能直接处理长的选项(如:--prefix=/home等),getopts有两个参数,第一个参数 ...
- Java中的GetOpt操作
在shell工具中,有专门的getopt函数,使用方法如下所示: while getopts "d:t:vh" opt; do case "${opt}" in ...
- Shell 参数(2) --解析命令行参数工具:getopts/getopt
getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,geto ...
- getopt使用
参考: http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html http://en.wikipedia.org ...
- getopt,getoptlong学习
getopt和getoptlong被用来解析命令行参数. 一.getopt #include <unistd.h> extern char *optarg; extern i ...
随机推荐
- phpmyadmin开启远程服务器连接
1.修改 braries/config.default.php,将 $cfg['AllowArbitraryServer'] 的值由 false 改成 true. 2.有其他需求的也可以自己在这里修 ...
- 安装python3.4
1.http://www.python.org下载适合自己机型的镜像文件 2.一路“next”到底,安装python到C盘上 3.计算机-属性-高级系统设置-环境变量,将刚刚安装的python路径添加 ...
- 阿铭linux笔记
2015-09-06虚拟机网络设置.wmv: curl 获取在命令行显示的网页 dhclient 分配ip地址 ifdown eth0 关闭网卡eth0 ifup eh0 ...
- JS 没有块级作用域
在函数(方法)中声明的所有变量,他们在整个函数中都有定义 var scope="abc"; function f() { alert(scope); //显示undefine v ...
- nodejs+express Mvc站点
nodejs+express Mvc站点 像asp.net Mvc一样开发nodejs+express Mvc站点 首先,我是个c#码农.从事Mvc开发已然4个年头了,这两年前端MVC的兴起,我也跟风 ...
- Ext4.0.7使用Ext.grid.ColumnModel报错:TypeError: Ext.grid.Model is not a constructor
代码如下: Ext.onReady(function(){ //定义列 var cm = new Ext.grid.ColumnModel([ {header: '编号', dataIndex: 'i ...
- position 为absolute时/float 为right,span为block
元素分为内联元素和区块元素两类(当然也有其它的),在内联元素中有个非常重要的常识,即内两元素是不可以设置区块元素所具有的样式,例如:width | height.relative : 原来是什么类型的 ...
- hdu3410-Passing the Message(RMQ,感觉我写的有点多此一举。。。其实可以用单调栈)
What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...
- HTTP 协议实现
一.超文本传输协议及HTTP包 HTTP协议用于在Internet上发送和接收消息.HTTP协议是一种请求-应答式的协议--客户端发送一个请求,服务器返回该请求的应答,所有的请求与应答都是HTT ...
- Linux下使用JNI的常见问题及解决方案
JNI是java和C/C++混合编程的接口,可以很方便地实现java调用C/C++语言.具体的使用方法,网上有很多教程,在此不做过多介绍.本博客只关注在使用JNI的过程中的常见问题. 1. 生 ...