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

  1. 【C】命令行参数解析——getopt、getopt_long及getopt_long_only

    前言 在linux下学习开源代码Webbench,遇到get_long等函数的用法,一时有点懵,故想深入了解这类命令行解析函数,并记此博文. 1.getopt getopt主要用来处理短命令行选项,例 ...

  2. Python命令行参数解析模块getopt使用实例

    Python命令行参数解析模块getopt使用实例 这篇文章主要介绍了Python命令行参数解析模块getopt使用实例,本文讲解了使用语法格式.短选项参数实例.长选项参数实例等内容,需要的朋友可以参 ...

  3. python命令行参数解析模块argparse和docopt

    http://blog.csdn.net/pipisorry/article/details/53046471 还有其他两个模块实现这一功能,getopt(等同于C语言中的getopt())和弃用的o ...

  4. shell 命令行参数(getopt和getopts)

    getopt 命令 使用getopt命令,可以解析任何命令行选项和参数,但是用法比较复杂.getopt的命令用法如下: $ getopt --help 用法: getopt optstring par ...

  5. [转]Python 命令行参数和getopt模块详解

    FROM : http://www.tuicool.com/articles/jaqQvq 有时候我们需要写一些脚本处理一些任务,这时候往往需要提供一些命令行参数,根据不同参数进行不同的处理,在Pyt ...

  6. Python 命令行参数和getopt模块详解

    有时候我们需要写一些脚本处理一些任务,这时候往往需要提供一些命令行参数,根据不同参数进行不同的处理,在Python里,命令行的参数和C语言很类似(因为标准Python是用C语言实现的).在C语言里,m ...

  7. gflags命令行参数解析

    gflags库是google开源的命令行参数解析工具. 安装 官方没有提供二进制库,但是Debian/Ubuntu平台本身提供了二进制库,可以直接git clone https://github.co ...

  8. [Go] 命令行参数解析包(flag 包)使用详解

    Go 的 flag 包可以解析命令行的参数. 一.命令行语法 命令行语法主要有以下几种形式: cmd -flag       // 只支持bool类型 cmd -flag=xxx cmd -flag ...

  9. $命令行参数解析模块argparse的用法

    argparse是python内置的命令行参数解析模块,可以用来为程序配置功能丰富的命令行参数,方便使用,本文总结一下其基本用法. 测试脚本 把以下脚本存在argtest.py文件中: # codin ...

  10. Google开源命令行参数解析库gflags

    Google开源命令行参数解析库gflags http://blog.csdn.net/lming_08/article/details/25072899 CMDLINE的解析 http://blog ...

随机推荐

  1. PHP遍历文件夹下的文件和获取到input name的值

    <?php$dir = dirname(__FILE__); //要遍历的目录名字 ->当前文件所在的文件夹//$dir='D:\PHP\wamp\www\admin\hosts\admi ...

  2. php的一些小笔记--数组

    array_chunk  分割数组  第三个参数确定分割的数组是否维持原样key,默认为false array_column 返回数组指定的列 array_combine 合并数组     第一个数组 ...

  3. linq分组查询

    string[] arrStr = { ".com", "www.baidu.com", "www.qq.com", "www.b ...

  4. js序列化json对象

    SerializeJsonToStr : function( oJson ) { if( oJson == null ) return "null"; if( typeof(oJs ...

  5. 从客户端(******)中检测到有潜在危险的 Request.Form 值。

    在 提交表单时候,asp.net 提示:"从客户端(......)中检测到有潜在危险的 Request.Form 值" .asp.net中的请求验证特性提供了某一等级的保护措施防止 ...

  6. SQL查询 addScalar()或addEntity()

    Hibernate除了支持HQL查询外,还支持原生SQL查询.   对原生SQL查询执行的控制是通过SQLQuery接口进行的,通过执行Session.createSQLQuery()获取这个接口.该 ...

  7. hdu 1208 Pascal's Travels

    http://acm.hdu.edu.cn/showproblem.php?pid=1208 #include <cstdio> #include <cstring> #inc ...

  8. SqlServer sysobjects_table

    --这是查询所有表的信息 select * from sysobjects where xtype='U'; --这是查询表的数量 select count(*) from sysobjects wh ...

  9. css案例学习之div a实现立体菜单

    效果 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  10. 用日志文件备份sqlserver

    USE [TestDB] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO )) as ) ),),)),)+ '.bak' backup dat ...