使用getopt_long解析程序长选项参数
2011-08-23 14:20
597 查看
写在前面:
对于可选参数一定要使用以下两种方法标明其值 –wValue 或--who==Value 而不能是 --who Value,
而对于必填参数则可以使用-lValue 或 --love Value或--love=Value,
这并不是bug.
//============================================================================ // Name : TestOpt.cpp // Author : yangyh // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <getopt.h> using namespace std; int version; struct option longopts[] = { { "version", no_argument, &version, 'v' }, { "name", no_argument, NULL, 'n' }, { "love", required_argument, NULL,'l' }, { "who",optional_argument,NULL,'w'}, { 0, 0, 0, 0 } }; int main(int argc, char *argv[]) { int c; while ((c = getopt_long(argc, argv, "vl:w::", longopts, NULL)) != -1) { switch (c) { case 'l': printf("love = %s!\n", optarg); break; case 0: // printf("getopt_long()设置变量 : version = %c\n", version); break; case 'v': printf("version..\n"); break;
case 'w': printf("who = %s\n",optarg); break; } } return 0; }
相关文章推荐
- 使用getopt_long解析程序长选项参数
- 使用getopt解析程序的参数选项
- 程序参数解析函数使用getopt_long()函数,改编自webbench
- 命令行程序选项解析函数—getopt_long
- getopt_long()解析命令行选项参数
- Linux命令选项及参数解析 getopt() getopt_long() 函数
- Linux解析命令行选项参数getopt和getopt_long
- 使用getopt 解析参数
- Linux下C程序设计(4)----操作环境变量 、程序传递参数getopt getopt_long操作、获取时间
- 使用getopt_long()从命令行获取参数
- AJAX请求 $.ajaxSetup方法的使用:设置AJAX请求的默认参数选项,当程序中需要发起多个AJAX请求时,则不用再为每一个请求配置请求的参数
- linux-解析命令行选项getopt_long用法
- python_getopt解析命令行输入参数的使用
- 命令行程序选项解析函数—getopt
- 如何使用getopt()函数解析参数
- 使用getopt_long()从命令行获取参数,struct option
- 命令行程序选项解析函数—getopt
- linux解析命令行选项getopt_long用法解读
- 使用 getopt 处理命令行长参数(长选项)
- GNU长选项命令行解析getopt_long()