命令行参数解析:getopt,getopt_long
#include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg; /* 当前选项对应的参数,or NULL */
extern int optind, opterr, optopt;
/* optind 再次调用getopt()时在argv中的索引;
* 遇到无法解析选项时,返回 '?' ,显示错误消息,若不想显示则opterr=0;
* optopt 最后一个未知选项的索引;
#include <getopt.h>
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getopt(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE
getopt_long(), getopt_long_only(): _GNU_SOURCE
argc,**argv: main函数传递
*optstring: 对于每一个字符选项而言,'c'表示无参数;'c:'表示有参数,形式 "-c Argument"/"-cArgument";'c::'表示参数可有可无,若有形式只能是'-cArgument'。
struct option数组最后的元素全0,*longindex保存当前数组索引。
struct option {
const char *name; /* 长选项名字 */
int has_arg; /* 选项是否有参数:no_argument,0;required_argument,1;optional_argument,2 */
int *flag;
int val;
};
/* *flag,val仅作用于长选项
* flag==NULL时,val;
* flag!=NULL时,返回 0,val保存到flag中
*/
getopt成功,返回选项字符;解析完毕返回-1;遇到不在optstring中的选项,返回 '?';遇到缺少参数的选项,返回 ':'(若optstring第一个字符为 ':',否则返回 '?'。
getopt_long 短选项,返回对应选项字符;长选项时,返回val(flag==NULL),返回0(flag!=NULL,保存val);其他情况同getopt。
getopt_long例子
#include<string.h>
#include<strings.h>
#include<stdio.h>
#include<stdlib.h>
#include<getopt.h> int reload,time; static const struct option long_options[]=
{
{"force",no_argument,NULL,'f'},
{"reload",no_argument,&reload,},
{"time",required_argument,NULL,'t'},
{NULL,,NULL,}
};
int main(int argc, char *argv[])
{
int opt=,options_index=;
while((opt=getopt_long(argc,argv,"frt:?h",long_options,&options_index))!=-)
{
switch(opt)
{
case 'f':
printf("case f opt=%c",opt);
break;
case 'r':
printf("case r opt=%c",opt);
break;
case 't':
printf("case t opt=%c",opt);
break;
case :
printf("case 0 opt=%c",opt);
break;
}
printf(" optarg=%s\t optind=%d\t opterr=%d\t optopt=%d\t ",optarg,optind,opterr,optopt);
printf(" reload=%d,time=%d \n",reload,time);
}
return ;
}
getopt例子
#include<string.h>
#include<strings.h>
#include<stdio.h>
#include<stdlib.h>
#include<getopt.h>
#include<unistd.h> int main(int argc, char *argv[])
{
int opt;
while((opt=getopt(argc,argv,"frt:?h"))!=-)
{
switch(opt)
{
case 'f':
printf("case f opt=%c ",opt);
break;
case 'r':
printf("case r opt=%c ",opt);
break;
case 't':
printf("case t opt=%c ",opt);
break;
}
printf("optarg=%s\n",optarg);
}
return ;
}
命令行参数解析:getopt,getopt_long的更多相关文章
- 【C】命令行参数解析——getopt、getopt_long及getopt_long_only
前言 在linux下学习开源代码Webbench,遇到get_long等函数的用法,一时有点懵,故想深入了解这类命令行解析函数,并记此博文. 1.getopt getopt主要用来处理短命令行选项,例 ...
- Python命令行参数解析模块getopt使用实例
Python命令行参数解析模块getopt使用实例 这篇文章主要介绍了Python命令行参数解析模块getopt使用实例,本文讲解了使用语法格式.短选项参数实例.长选项参数实例等内容,需要的朋友可以参 ...
- python命令行参数解析模块argparse和docopt
http://blog.csdn.net/pipisorry/article/details/53046471 还有其他两个模块实现这一功能,getopt(等同于C语言中的getopt())和弃用的o ...
- shell 命令行参数(getopt和getopts)
getopt 命令 使用getopt命令,可以解析任何命令行选项和参数,但是用法比较复杂.getopt的命令用法如下: $ getopt --help 用法: getopt optstring par ...
- [转]Python 命令行参数和getopt模块详解
FROM : http://www.tuicool.com/articles/jaqQvq 有时候我们需要写一些脚本处理一些任务,这时候往往需要提供一些命令行参数,根据不同参数进行不同的处理,在Pyt ...
- Python 命令行参数和getopt模块详解
有时候我们需要写一些脚本处理一些任务,这时候往往需要提供一些命令行参数,根据不同参数进行不同的处理,在Python里,命令行的参数和C语言很类似(因为标准Python是用C语言实现的).在C语言里,m ...
- gflags命令行参数解析
gflags库是google开源的命令行参数解析工具. 安装 官方没有提供二进制库,但是Debian/Ubuntu平台本身提供了二进制库,可以直接git clone https://github.co ...
- [Go] 命令行参数解析包(flag 包)使用详解
Go 的 flag 包可以解析命令行的参数. 一.命令行语法 命令行语法主要有以下几种形式: cmd -flag // 只支持bool类型 cmd -flag=xxx cmd -flag ...
- $命令行参数解析模块argparse的用法
argparse是python内置的命令行参数解析模块,可以用来为程序配置功能丰富的命令行参数,方便使用,本文总结一下其基本用法. 测试脚本 把以下脚本存在argtest.py文件中: # codin ...
- Google开源命令行参数解析库gflags
Google开源命令行参数解析库gflags http://blog.csdn.net/lming_08/article/details/25072899 CMDLINE的解析 http://blog ...
随机推荐
- PHP 获取客户端IP
function get_ip() { static $realIP; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_ ...
- Python学习笔记(1)——数组差集
面试的时候被问到这样一个问题:有A.B两个数组,找出B中有A中没有的所有元素(换言之即是求差集B-A).当时比较紧张,用了最原始的双重嵌套循环逐个比较,很显然这种时间复杂度高达O(n2)的算法相当lo ...
- maven Spring MVC项目
IntelliJ IDEA上创建maven Spring MVC项目 各软件版本 利用maven骨架建立一个webapp 建立相应的目录 配置Maven和SpringMVC 配置Maven的pom.x ...
- You and your research
英文版http://www.cs.virginia.edu/~robins/YouAndYourResearch.html 视频版http://www.youtube.com/watch?v=a1zD ...
- Material风格的Quick组件,妈妈再也不用担心我的界面不好看了
https://github.com/papyros/qml-material http://www.zhihu.com/question/38523930
- HtmlTextNode & HtmlCommentNode
在HtmlAgilityPack里,HtmlTextNode对应的是文本节点.这是一个非常简单的一个类,方法和字段都比较少. 一.属性 override string InnerHtml { get; ...
- ServerProperties
Spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动.其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat).当然你也可以将项 ...
- 浅析busybox-1.12.0中ash的脚本命令局限性
浅析busybox-1.12.0中ash的脚本命令局限性 LUTHER= 表示将LUTHER清空,将其变为null echo ${LUTHER:-111}如果执行该句之前LUTHER变量不存在,那么显 ...
- css案例学习之div ul li a 实现导航效果
效果 代码 <html> <head> <title>无需表格的菜单</title> <style> body{ background-co ...
- Entity Framework 技术参考:http://kb.cnblogs.com/zt/ef/
Entity Framework 技术参考:http://kb.cnblogs.com/zt/ef/