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. KVM&amp;Libvirt基本概念及开发杂谈

    导读 大家好,本次肖力分享的主题是KVM&Libvirt基本概念及开发杂谈,内容有些凌乱松散,主要基于自己早期整理的笔记内容和实践感悟,有些内容难免有失偏颇,望见谅.前面先介绍下需要了解的基本 ...

  2. java中 this和super的差别

    this表示当前调用方法的对象的引用: (谁调用这种方法,谁就是这个对象,这个this就是它的引用) 比方: <pre name="code" class="jav ...

  3. tomcat生产部署关键參数设置

    JVM设置 个节点.每一个tomcat估计处理500个链接,那么连接池的长连接数最大设为2000. 全节点复制(DeltaManager)模式集群节点数3-6为宜. 主备复制(BackupMnagag ...

  4. gsoap

    C++中如何使用gsoap开发WebService 1. 什么是gSOAPgSOAP是一个夸平台的,用于开发Web Service服务端和客户端的工具,在Windows.Linux.MAC OS和UN ...

  5. 关于ActiveMQ的配置

    http://www.cnblogs.com/CopyPaster/archive/2012/04/27/2473179.html

  6. (转)更新Java final常量后,请重新编译你的class

    程序中使用的各种常量用一个类来统一管理,类似: public class AppConst { public static final String STR_1 = "String1&quo ...

  7. Call to a member function select() on string错误

    Call to a member function select() on string错误 Call to a member function select() on array错误 我也是 Get ...

  8. 高阶函数简述 js

    1.简述 高阶函数似乎是一种先进编程的的技术.然而,并不是. 高阶函数其实就是将函数作为参数或者返回值的函数.其中作为参数的函数一般是回调函数. 2.例子 (1)最简单的例子 大家都熟悉数组的sort ...

  9. UI组件之TextView及其子类(五)计时器Chronometer

    Chronometer直接继承了TextView组件,它会显示一段文本,显示从某个事实上时间開始.一共过了多长时间.我们看Chronometer的源代码: watermark/2/text/aHR0c ...

  10. linux 打包和压缩文件

    打包成tar文件 tar -cf mydir.tar mydir/ 打包tar压缩成gz tar -czf mydir.tar.gz mydir/ 解压mydirtar文件 tar -xvf mydi ...