[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 ...
随机推荐
- javascript第二课练习
<script type="text/javascript"> window.onload=function() //网页全部加载完后执行 { va ...
- BZOJ 1877 晨跑
http://www.lydsy.com/JudgeOnline/problem.php?id=1877 思路:拆点费用流,答案就是最大流量和最小费用. #include<algorithm&g ...
- poj3537--Crosses and Crosses
题意:有个一维棋盘,两人轮流下棋,然后谁连成三个谁赢 记得去年fj夏令营有见过这题,但是太弱了, 不会做. 记忆化搜索,如果n<=3肯定先手必胜,递推即可. #include<iostre ...
- [Android开发常见问题-18] Connection to https://dl-ssl.google.com refused
在使用Android SDK Manager的时候,有时候会遇到上述问题,其实原因很简单,属于本地配置的问题. 解决方法: 1.以文本文件的方式打开“C:\Windows\System32\drive ...
- 【转】Gedit中文乱码
原文网址:http://wiki.ubuntu.org.cn/Gedit%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81#.E5.91.BD.E4.BB.A4.E6.96.B9 ...
- Python 入门教程 9 ---- A Day at the Supermarket
第一节 1 介绍了for循环的用法 for variable in values: statement 2 for循环打印出列表的每一项 for item in [1 , 2 , 3]: print ...
- Direct3D 纹理映射
纹理映射是将2D的图片映射到一个3D物体上面,物体上漂亮图案被称为纹理贴图, 一个表面可以支持多张贴图等等,下面简单介绍下纹理贴图 纹理贴图UV: 贴图是一个个像素点组成,每一个像素点都由一个坐标最后 ...
- DoTween学习笔记(二) UGUI结合使用(实现一些简单效果)
UGUI官方实例中是使用Animation来控制UI的移动,放大缩小动画等等, Animation来控制UI的动画工作量实在是太多了, 所以我们一般使用itween,DoTween. 来控制动画, 这 ...
- Unity Navigation自动寻路
NavMesh(导航网格) 是3D游戏世界中主动寻路的一种技术,如果你想让游戏人物能自动绕开障碍物到达目的地.那你就来学习下 Navigation导航技术吧O(∩_∩)O~ 首先创建一个项目创建Pan ...
- qt简单界面更新代码(菜鸟级)(部分代码)
qt简单界面更新代码(菜鸟级)(部分代码)self.timers_1=QtCore.QTimer(self)self.timers_1.timeout.connect(self.min_1)self. ...