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. JWT token心得

    token的组成 token串的生成流程. token在客户端与服务器端的交互流程 Token的优点和思考 参考代码:核心代码使用参考,不是全部代码 JWT token的组成 头部(Header),格 ...

  2. solr入门之參考淘宝搜索提示功能优化拼音加汉字搜索功能

    首先看一下从淘宝输入搜索keyword获取到的一些数据信息: 第一张:使用拼音的全程来查询 能够看到提示的是匹配的转换的拼音的方式,看最后一个提示项 这里另一个在指定分类文件夹下搜索的功能,难道后台还 ...

  3. gedit如何使用代码片段

    gedit的片段插件 1 打开这个插件   2 以HTML为例,我们看到触发器是doctype   3则我在编辑一个HTML文档的时候,先输入doctype,再按Tab键,将会自动提示,我选择即可插入 ...

  4. angularjs库及ionic库下载地址

    angularjs库下载地址: 下载地址:https://code.angularjs.org/ 引用地址:http://www.bootcdn.cn/angular.js/ ionic库下载地址: ...

  5. Citrix服务器虚拟化之二十九 XenApp 6.5发布服务器上的应用程序

    Citrix服务器虚拟化之二十九  XenApp 6.5发布服务器上的应用程序 XenApp可发布以下类型的资源向用户提供信息访问,这些资源可在服务器或桌面上虚拟化: 1)  服务器桌面:发布场中服务 ...

  6. GridLayout with span

    Widgets can span multiple columns or rows in a grid. In the next example we illustrate this. #!/usr/ ...

  7. taro 环境判断

    Taro.ENV_TYPE ENV_TYPE.WEAPP 微信小程序环境 ENV_TYPE.SWAN 百度小程序环境 ENV_TYPE.ALIPAY 支付宝小程序环境 ENV_TYPE.TT 字节跳动 ...

  8. linux sheel重复执行上条命令

    Linux系统下Shell重复执行上条命令的 4 种方法: 1.使用上方向键,并回车执行. 2.按 !! 并回车执行. 3.输入 !-1 并回车执行. 4.按 Ctrl+P 并回车执行.

  9. 关于AWS的备份策略

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

  10. Linux命令-文件处理命令:tail

    tail /etc/services 查看etc目录的services文件最后10行内容(默认显示后10行内容) tail -n /etc/services 查看etc目录的services文件的后5 ...