getopt--parse command line options
getopt解析命令行选项
getopt, getopt_long, getopt_long_only, optarg, optind, opterr, optopt - Parse command-line options
#include <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
其中argc和argv等同于main函数参数。
命令行选项元素(an option element)一般以-开头,之后的字符就是命令行选项字符(option characters)。重复调用getopt(),可连续得到命令行选项元素中的选项字符。
optstring是包含选项字符的字符串。若一个字符后面有一个冒号(:),选项需要一个参数(argument),optarg指向选项字符后面的文本(如-oarg,指向arg)或后面的参数(如-o arg,指向arg)。
optind是下一个要处理的选项元素的索引。系统初始化optind为1,可以reset到1以重新扫描argv。
若getopt()找到另一个选项字符,返回该选项字符,并同时更新optind和nextchar。
若没有选项字符了,getopt()返回-1。optind是第一个不是选项元素的argv-element的索引。
两个冒号表示选项有一个可选的参数,有参数时optarg指向参数,无时为0。
默认情况下,getopt()打乱argv顺序,如此所有非选项参数都在最后(如gcc foo.c -Wall, foo.c被排到最后)。
若getopt()不识别选项字符,向stderr打印错误信息,并保存字符到optopt,返回‘?’。程序可以设置opterr为0来阻止错误消息。
若getopt()找到一个选项字符不在optstring,或选项少参数,返回‘?’,并设置optopt为实际字符。optstring的第一个字符是一个冒号(:),getopt()返回’:'来代替‘?’。
返回值
一个选项找到,getopt()返回选项字符。所有选项都解析完后,返回-1。
若一个选项不在optstring中,返回‘?’。
若选项少参数,返回值依赖于optstring第一个字符:若第一个字符为‘:’,返回‘:’,否则返回‘?’。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> int main(int argc, char * argv[])
{
int flags, opt;
int nsecs, tfnd; nsecs = ;
tfnd = ;
flags = ; while((opt=getopt(argc, argv, "nt:")) != -){
switch(opt) {
case 'n':
flags = ;
break;
case 't':
nsecs = atoi(optarg);
tfnd = ;
break;
default : /* '?' */
fprintf(stderr, "Usage: %s [-t nsecs] -n name\n", argv[]);
exit(EXIT_FAILURE);
}
}
printf("flags=%d; tfnd=%d; optind=%d\n", flags, tfnd, optind); if(optind >= argc){
fprintf(stderr, "Expected argument after options\n");
exit(EXIT_FAILURE);
} printf("name argument =%s\n", argv[optind]); exit(EXIT_SUCCESS);
}
getopt--parse command line options的更多相关文章
- Getopt::Long - Extended processing of command line options
use Getopt::Long; my $data = "file.dat"; my $length = 24; my $verbose; GetOptions (" ...
- An annotation based command line parser
Java命令行选项解析之Commons-CLI & Args4J & JCommander http://rensanning.iteye.com/blog/2161201 JComm ...
- IAR Build from the command line 环境变量设置
http://supp.iar.com/Support/?Note=47884 Technical Note 47884 Build from the command line The alterna ...
- List of Chromium Command Line Switches(命令行开关集)——官方指定命令行更新网址
转自:http://peter.sh/experiments/chromium-command-line-switches/ There are lots of command lines which ...
- Cookies with curl the command line tool
w https://curl.haxx.se/docs/http-cookies.html curl has a full cookie "engine" built in. If ...
- How to Use Android ADB Command Line Tool
Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Andro ...
- logoff remote desktop sessions via command line tools
This trick I learned from my one of ex-college. In Windows servers, only two remote desktop session ...
- 《The Linux Command Line》 读书笔记01 基本命令介绍
<The Linux Command Line> 读书笔记01 基本命令介绍 1. What is the Shell? The Shell is a program that takes ...
- python click module for command line interface
Click Module(一) ----xiaojikuaipao The following mat ...
随机推荐
- 初始小R-安装启动与测试
非常感谢<深入浅出数据分析>这本书让我有幸认识了R,多多少少的弥补了我心里对R语言.R分析.R工具的模糊认知,下面我们就来体验一下R语言的魅力吧!GO! 一:下载R R官方地址:http: ...
- 时间记录APP———Time Meter
关注过时间管理的人可能都听过大名鼎鼎的柳比歇夫的时间记录法,在几年前,大多人都推荐纸笔的记录方法,但是纸笔总是会忘,越来越智能的手机可是总不会忘得,所以我始终在寻找一款手机端好用的APP. 不管是时间 ...
- C#应用视频教程3.3 Halcon+C#测试
接下来我们考虑把Halcon的代码移植到C#程序上,首先找到halcon的dll(.NET类库有1.0,2.0,3.5的,如果你安装了更新版本的halcon则有更新的.NET类库,我们复制最新的dll ...
- HTTP浏览器缓存机制
来自:http://kb.cnblogs.com/page/165307/ 浏览器缓存机制 浏览器缓存机制,其实主要就是HTTP协议定义的缓存机制(如: Expires: Cache-control等 ...
- 算法笔记_020:深度优先查找(Java)
目录 1 问题描述 2 解决方案 2.1 蛮力法 1 问题描述 深度优先查找(depth-first search,DFS)可以从任意顶点开始访问图的顶点,然后把该顶点标记为已访问.在每次迭代的时候, ...
- python2.7里的StringIO.StringIO与BytesIO有什么区别
import StringIO与from io import BytesIO的区别 open()函数返回的文件对象取决于模式.当使用文本模式打开文件时,它返回一个TextIOBase的子类.当使用二进 ...
- 安装SQL SERVER 2016 CTP (二)[多图]
内容中包含 base64string 图片造成字符过多,拒绝显示
- SQL SERVER: 合并相关操作(Union,Except,Intersect)
SQL SERVER: 合并相关操作(Union,Except,Intersect) use tempdb create table tempTable1 (id int primary key id ...
- HBase in Action前三章笔记
近期接触HBase,看了HBase In Action的英文版.開始认为还行,做了些笔记.可是兴许看下去,越来越感觉到实战这本书比較偏使用上的细节,对于HBase的具体设计涉及得很少.把前三章的一些笔 ...
- 35 、HashSet详解
HashSet特点 1.不能保证元素的排列顺序 2.没有重复元素 3.HashSet不是同步的,多个线程同时访问一个HashSet,需要通过代码来保持同步 4.集合元素可以是null 当向HashSe ...