[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 ...
随机推荐
- Makefile的几个赋值运算符(转:笔记)
http://www.cnblogs.com/pengdonglin137/p/3801060.html 在Makefile中我们经常看到 = := ?= +=这几个赋值运算符,那么他们有什么区别呢? ...
- Hdu1108(最小公倍数)
#include <stdio.h> int main() { int Num1,Num2; while(scanf("%d %d",&Num1,&Nu ...
- Bootstrap 实例 - 模态框(Modal)插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 使用Hexo搭建GitPage
资料: hexo官方文档:https://hexo.io/zh-cn/docs/ jekyll官方文档:http://jekyll.com.cn/docs/home/ 简介: 使用hexo和jekyl ...
- C语言复习--实现栈
C #include <stdio.h> #include <stdlib.h> #define STACK_SIZE 100 typedef char TYPE; typed ...
- iotop
iotop命令是专门显示硬盘IO的命令,界面风格类似top命令.这个命令只有在kernelv2.6.20及以后的版本中才有. 1.直接yum安装,rh6的光盘里有包. yum install io ...
- hdu 3191 How Many Paths Are There
http://acm.hdu.edu.cn/showproblem.php?pid=3191 这道题求次短路经和路径数 #include <cstdio> #include <cst ...
- C++初始化列表问题
C++初始化列表问题,类中有一个对象类型的数组成员变量,在初始化列表中初始化时报错“[]”操作符语法错误 class ClassA{private ClassB arrayOfObjectClassB ...
- hdu 5256 序列变换(LIS最长上升子序列)
Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需要修改多 ...
- 创建UILabel
UILabelCreate.h #import <UIKit/UIKit.h> @interface UILabelCreate : UILabel /** * 创建UILabel 初始化 ...