getopt、getopt_long和getopt_long_only】的更多相关文章

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:--是一个指示符,表明停止扫…
前言 在linux下学习开源代码Webbench,遇到get_long等函数的用法,一时有点懵,故想深入了解这类命令行解析函数,并记此博文. 1.getopt getopt主要用来处理短命令行选项,例如 ./test -v 中的 -v 就是一个短选项.使用该函数需引入头文件<unistd.h>,下面是该函数定义: int getopt (int ___argc, char *const *___argv, const char *__shortopts); 其中___argc和___argv是…
一:posix约定: 下面是POSIX标准中关于程序名.参数的约定: 程序名不宜少于2个字符且不多于9个字符: 程序名应只包含小写字母和阿拉伯数字: 选项名应该是单字符或单数字,且以短横 '-' 为前綴: 多个不需要选项参数的选项,可以合并.(譬如:foo  -a -b -c  ----> foo  -abc) 选项与其参数之间用空白符隔开: 选项参数不可选. 若选项参数有多值,要将其并为一个字串传进来.譬如:myprog -u "arnold,joe,jane".这种情况下,需…
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…
转载: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是命令选项,命令选项可以是数字或字母,但前面必须有一"-"符号,后面…
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…
#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…
getopt()函数 getopt_long函数 函数原型(function prototype) #include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; #include <getopt.h> int getopt_long(int argc, char…
1.getopt 1.1 函数定义 int getopt(int argc, char * const argv[], const char *optstring);#include <unistd.h> 该函数用来解析命令行参数.前两个参数设为main函数的两个参数.optstring设为由该命令要处理的各个选项组成的字符串.选项后面带有冒号':'时,该选项是一个带参数的选项. 例如:make -f filename -n-f是一个带参数的选项,-n是一个没有参数的选项. 可以下面这样调用函…
命令行工具下的参数选项有两种,长选项和短选项.短选项以-开头,后面跟单个字母:长选项以--开头,后面可跟多个字母. 一. getopt() 1.功能:解析命令行短选项参数 2.函数原型: #include <getopt.h> int getopt(int argc, char * const argv[], const char *optstring); getopt.h中声明的几个外部变量:extern char *optarg; extern int optind, opterr, op…