getopt_long】的更多相关文章

getopt_long原型 #define no_argument 0 #define required_argument 1 #define optional_argument 2 struct option { const char *name; //名称,下面实例中使用的--help,--version int has_arg; //是否有参数,可选0,1,2三个值,就是上面的那三个宏定义 int *flag; //返回值,传入的一个int指针,表示该参数的解析结果,如果是NULL,那么返…
命令行选项解析函数(C语言):getopt()和getopt_long() 上午在看源码项目webbench时,刚开始就被一个似乎挺陌生函数getopt_long()给卡住了,说实话这函数没怎么见过,自然不知道这哥们是干什么的.于是乎百度了一番,原来是处理命令行选项参数的,的确,正规点的大型程序一般第一步就是处理命令行参数的,接着才是主干程序.在百度和man的帮助下,找到了具体使用方法和解释,二话不说赶紧学习一下,并总结出文档记录一下. 平时在写程序时常常需要对命令行参数进行处理,因为参数少,自…
Today I came across a function [getopt] by accident. It is very useful to parse command-line arguments with this tool! Here is: #inlcude <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int opt…
getopt_long函数可以轻松的解析main函数的命令行参数. int getopt_long(int argc,char * const argv[],const char *optstring,const struct option *longopts,int *longindex) 函数中的参数argc和argv通常直接从main()的两个参数传递而来.optstring是选项参数组成的字符串. 字符串optstring可以下列元素: 1. 单个字符,表示选项, 2. 单个字符后接一个…
转载:http://blog.chinaunix.net/uid-20321537-id-1966849.html   在头文件中int getopt(int argc,char *argv[], const char *optstring); extern char *optargextern int optind,opterr,optopt;其中agrc和argv是直接从主函数中传递过来的参数,而optstring是命令选项,命令选项可以是数字或字母,但前面必须有一"-"符号,后面…
getopt_long支持长选项的命令行解析,使用man getopt_long,得到其声明如下: #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 cha…
#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; * opto…
GNU/Linux的命令行选项有两种类型:短选项和长选项,前者以 '-' 作为前导符,后者以 '--' 作为前导符.比如有一个命令: $ myprog -a vv --add -b --file a.txt b.txt - -- -e c.txt 在GNU/Linux系统,对这种情况的一种合理解释是:a是短选项,带一个参数vv:add是长选项,无参数:b是短选项,无参数:file是长选项,带一个参数a.txt:b.txt是参数:-是参数,通常表示标准输入,stdin:--是一个指示符,表明停止扫…
     在执行某个程序的时候,我们通常使用命令行參数来进行配置其行为.命令行选项和參数控制 UNIX 程序,告知它们怎样动作. 当 gcc的程序启动代码调用我们的入口函数 main(int argc,char *argv[]) 时,已经对命令行进行了处理.argc 參数包括程序參数的个数,而 argv 包括指向这些參数的指针数组. 程序的參数能够分为三种:选项.选项的关联值,非选项參数. 比如: $gcc getopt_test.c -o testopt getopt_test.c是非选项參数…
文件 #include <getopt.h> 函数原型 int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex); 函数说明 getopt被用来解析命令行选项参数. getopt_long支持长选项的命令行解析,使用man getopt_long,得到其声明如下: int getopt_long(int argc, ch…