命令行参数解析: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 ...
随机推荐
- PXE简要配置过程
目录 1.所需服务 2.简要配置过程 1.dhcp服务 2.tftp服务 3.提供pxelinux.0配置文件 4.提供系统所需文件 1.所需服务: dhcp服 ...
- [转]浅谈C/C++内存泄露及其检测工具
转自:http://www.cnblogs.com/taoxu0903/archive/2007/10/27/939261.html 对于一个c/c++程序员来说,内存泄漏是一个常见的也是令人头疼的问 ...
- A(51)和C(51)相互调用
C语言是一种编译型程序设计语言,它兼顾了多种高级语言的特点,并可以调用汇编语言的子程序.用C语言设计开发微控制器程序已成为一种必然的趋势.Franklin C51是一种专门针对Intel 8051系列 ...
- Rotate Array 解答
Question Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, t ...
- Repeated DNA Sequences 解答
Question All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...
- FWA winner | Car Visualizer WebGL
FWA winner | Car Visualizer WebGL Car Visualizer made in WebGL using ThreeJS. It requires a modern b ...
- poj 2411 新写法
别以为我在刷水题.... 今天做了场srm,500pt想到了是dp但是无从下手,但是看了rng_58的神代码后顿觉海阔天空啊(盯着看了一个下午),相比于一年前的写法,真的是不忍直视啊, TC真是个好地 ...
- Curvy unity
想获得当前物体在路径的进度 1. 利用CurvySpline.GetNearestPointTF(); 2. 利用CurvyController.RelativePosition;
- leetcode_question_73 Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow ...
- php接口数据加密、解密、验证签名代码实例
php接口数据加密.解密.验证签名 代码非常easy,这里就不多废话了,直接奉上代码 <?php /** * 数据加密.解密.验证签名 * @edit http://www.lai18.com ...