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 ...
随机推荐
- 【转】Android:最全面的 Webview 详解
原文: https://blog.csdn.net/carson_ho/article/details/52693322 前言 现在很多App里都内置了Web网页(Hyprid App),比如说很多电 ...
- PostgreSQL流复制参数max_wal_senders详解
转自:http://my.oschina.net/Kenyon/blog/152234PostgreSQL 9.2.4 主机:192.25.10.76 从机:192.25.10.71 做postgre ...
- 关于listview,scrollview显示模糊边缘的设置
朋友们有时可能在开发中遇到这样的莫名其妙的问题,listview或scrollview滑动时上边和下边会出现两条模糊的边缘,有时会影响到我们app的视觉效果,我们怎么去掉这两条模糊的边缘呢?很简单,一 ...
- 汇编入门学习笔记 (十二)—— int指令、port
疯狂的暑假学习之 汇编入门学习笔记 (十二)-- int指令.port 參考: <汇编语言> 王爽 第13.14章 一.int指令 1. int指令引发的中断 int n指令,相当于引 ...
- VC中关于预编译头文件的概括,以及无法打开预编译头文件的错误解决
1.预编译头文件 在VC中编译器一般都是以文件为单位进行编译,如果修改了工程中的一个文件,那么将导致所有文件都要从新编译,这样的编译将耗费很长时间.为了提高编译速度,将那些不常被修改,比较稳定,文件单 ...
- 【jquery操作cookie】JQuery中$.cookie()方法的使用(同名cookie会覆盖)
jquery.cookie.js插件: <script type="text/javascript" src="js/jquery-1.6.2.min.js&quo ...
- Docker实战(五)编写Dockerfile
一.创建Dockerfile文件 首先,需要创建一个目录来存放 Dockerfile 文件,目录名称可以任意,在目录里创建Dockerfile文件: 二.Dockerfile 基本框架 Dockerf ...
- Android SDK中 tools 工具介绍
Android SDK包含了各种各样的定制工具,简介如下: Android模拟器(Android Emulator ) 它是在你的计算机上运行的一个虚拟移动设备.你可以使用模拟器来在一个实际的Andr ...
- Android实现开机自动运行程序
有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call y ...
- VB命令行参数分隔, 类似C语言中的main(int argc, char* argv[])
VB6.0为了提供命令行参数的支持,提供了Command()接口,于是通过 Command() 函数可以得到所有传入的参数,但是很不友好的是,VB的所有参数都被合在了一起,成为了一个字符串,当有多个参 ...