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的更多相关文章

  1. Getopt::Long - Extended processing of command line options

    use Getopt::Long; my $data   = "file.dat"; my $length = 24; my $verbose; GetOptions (" ...

  2. An annotation based command line parser

    Java命令行选项解析之Commons-CLI & Args4J & JCommander http://rensanning.iteye.com/blog/2161201 JComm ...

  3. IAR Build from the command line 环境变量设置

    http://supp.iar.com/Support/?Note=47884 Technical Note 47884 Build from the command line The alterna ...

  4. List of Chromium Command Line Switches(命令行开关集)——官方指定命令行更新网址

    转自:http://peter.sh/experiments/chromium-command-line-switches/ There are lots of command lines which ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 《The Linux Command Line》 读书笔记01 基本命令介绍

    <The Linux Command Line> 读书笔记01 基本命令介绍 1. What is the Shell? The Shell is a program that takes ...

  9. python click module for command line interface

    Click Module(一)                                                  ----xiaojikuaipao The following mat ...

随机推荐

  1. 稍复杂的ionic例子:显示一个列表,并且允许点击进入列表项

    这个例子,按照MVC的方式进行了分层,下面是代码: demo3.htm <!DOCTYPE html> <html ng-app="app"> <he ...

  2. vue常用属性解释。

    props:详看 示例-网格组件. props 可以是数组或对象,用于接收来自父组件的数据.props 可以是简单的数组,或者使用对象作为替代,对象允许配置高级选项,如类型检测.自定义校验和设置默认值 ...

  3. KineticJS教程(3)

    KineticJS教程(3) 作者: ysm  3.图形对象 3.1.Shape Kinetic提供了一个Shape对象用于在层上绘制图形,我们可以通过Kinetic.Shape()构造方法返回一个S ...

  4. NGUI所见即所得之深入剖析UIPanel,UIWidget,UIDrawCall底层原理

    NGUI所见即所得之深入剖析UIPanel,UIWidget,UIDrawCall底层原理 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com 之 ...

  5. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  6. Python 更新set

    更新set 由于set存储的是一组不重复的无序元素,因此,更新set主要做两件事: 一是把新的元素添加到set中,二是把已有元素从set中删除. 添加元素时,用set的add()方法: weekday ...

  7. Python list添加新元素

    append()和insert() 添加新元素现在,班里有3名同学:>>> L = ['Adam', 'Lisa', 'Bart']今天,班里转来一名新同学 Paul,如何把新同学添 ...

  8. Android SDK镜像的介绍使用【转发】

    由于一些原因,Google相关很多服务都无法访问,所以在很多时候我们SDK也无法升级,当然通过技术手段肯定可以解决,但是比较麻烦,而且下载速度也不怎么样. 这里笔者介绍一个国内的Android镜像站, ...

  9. 关于AWS的备份策略

    AWS有一个很强大的功能,就是snapshot,翻译过来就是对EBS进行快照.通俗的说,即是对整个硬盘进行完整的镜像备份.如此一来,在其中一台EC2挂掉的时候,我们迅速的另起一台EC2,并将通过快照恢 ...

  10. myql基准测试工具Sysbench

    一.Sysbench介绍 SysBench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况.它主要包括以下几种方式的测试: 1.cpu性能 2.磁盘io性 ...