c++之getopt函数的使用
2016-07-18 15:56
477 查看
命令行参数解析函数 —— getopt()
getopt()函数声明如下:
该函数的argc和argv参数通常直接从main()的参数直接传递而来。optstring是选项字母组成的字串。如果该字串里的任一字符后面有冒号,那么这个选项就要求有选项参数。
当给定getopt()命令参数的数量 (
int opterr——这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
以下面的程序为例:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#ifdef _WIN32
#include "getopt.h"
#include <windows.h>
#include <direct.h>
#include <io.h>
#else
#include <unistd.h>
#include <getopt.h>
#endif
#include <iostream>
using namespace std;
//参考http://blog.csdn.net/mr_jj_lian/article/details/6835137
//getopt -bcde
//getopt -a aa -b bb -c cc -d dd -f ff
int main(int argc, char *argv[])
{
int ch;
// opterr = 0;
while ((ch = getopt(argc,argv,"a:b:c:de"))!=-1)
{
switch(ch)
{
case 'a':
printf("option a=%s\n",optarg);
break;
case 'b':
printf("option b=%s\n",optarg);
break;
case 'c':
printf("option c=%s\n",optarg);
break;
case '?':
printf("无效的选项字符 \'%c\'!\n", (char)optopt);
break;
case ':':
printf("缺少选项参数!\n");
break;
default:
printf("other option=%c\n",ch);
break;
}
}
printf("optopt +%c\n",optopt);
printf("run here opterr=%d\n",opterr);
return 0;
}
//http://blog.csdn.net/mr_jj_lian/article/details/6835137
//getopt -n
//getopt -g
//getopt -l xiaohua
//getopt -ngl forever
int main3 (int argc, char **argv)
{
int oc; /*选项字符 */
char *b_opt_arg; /*选项参数字串 */
while((oc = getopt(argc, argv, "ngl:")) != -1)//:表示必须传入参数。
{
switch(oc)
{
case 'n':
printf("My name is Lyong.\n");
break;
case 'g':
printf("Her name is Xxiong.\n");
break;
case 'l':
b_opt_arg = optarg;
printf("Our love is %s\n", optarg);
break;
}
}
return 0;
}
//getopt -l
int main4(int argc, char **argv)
{
int oc; /*选项字符 */
char *b_opt_arg; /*选项参数字串 */
while((oc = getopt(argc, argv, "ngl:")) != -1)
{
switch(oc)
{
case 'n':
printf("My name is Lyong.\n");
break;
case 'g':
printf("Her name is Xxiong.\n");
break;
case 'l':
b_opt_arg = optarg;
printf("Our love is %s\n", optarg);
break;
case '?':
printf("arguments error!\n");
break;
}
}
return 0;
}
//getopt -a
//getopt -l
int main2 (int argc, char **argv)
{
int oc; /*选项字符 */
char ec; /*无效的选项字符*/
char *b_opt_arg; /*选项参数字串 */
opterr=0;
//在调用getopt()之前,将opterr设置为0,这样就可以在getopt()函数发现错误的时候强制它不输出任何消息。
while((oc = getopt(argc, argv, ":ngl:")) != -1)
{
switch(oc)
{
case 'n':
printf("My name is Lyong.\n");
break;
case 'g':
printf("Her name is Xxiong.\n");
break;
case 'l':
b_opt_arg = optarg;
printf("Our love is %s\n", optarg);
break;
case '?':
ec = (char)optopt;
printf("无效的选项字符 \' %c \'!\n", ec);
break;
case ':':
printf("缺少选项参数!\n");
break;
}
}
return 0;
}
http://blog.csdn.net/mr_jj_lian/article/details/6835137
getopt()函数声明如下:
#include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; |
当给定getopt()命令参数的数量 (
argc)、指向这些参数的数组 (
argv) 和选项字串 (
optstring) 后,
getopt()将返回第一个选项,并设置一些全局变量。使用相同的参数再次调用该函数时,它将返回下一个选项,并设置相应的全局变量。如果不再有可识别的选项,将返回
-1,此任务就完成了。
getopt()所设置的全局变量包括:
char *optarg——当前选项参数字串(如果有)。
int optind——argv的当前索引值。当getopt()在while循环中使用时,循环结束后,剩下的字串视为操作数,在argv[optind]至argv[argc-1]中可以找到。
int opterr——这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
int optopt——当发现无效选项字符之时,getopt()函数或返回'?'字符,或返回':'字符,并且optopt包含了所发现的无效选项字符。
以下面的程序为例:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#ifdef _WIN32
#include "getopt.h"
#include <windows.h>
#include <direct.h>
#include <io.h>
#else
#include <unistd.h>
#include <getopt.h>
#endif
#include <iostream>
using namespace std;
//参考http://blog.csdn.net/mr_jj_lian/article/details/6835137
//getopt -bcde
//getopt -a aa -b bb -c cc -d dd -f ff
int main(int argc, char *argv[])
{
int ch;
// opterr = 0;
while ((ch = getopt(argc,argv,"a:b:c:de"))!=-1)
{
switch(ch)
{
case 'a':
printf("option a=%s\n",optarg);
break;
case 'b':
printf("option b=%s\n",optarg);
break;
case 'c':
printf("option c=%s\n",optarg);
break;
case '?':
printf("无效的选项字符 \'%c\'!\n", (char)optopt);
break;
case ':':
printf("缺少选项参数!\n");
break;
default:
printf("other option=%c\n",ch);
break;
}
}
printf("optopt +%c\n",optopt);
printf("run here opterr=%d\n",opterr);
return 0;
}
//http://blog.csdn.net/mr_jj_lian/article/details/6835137
//getopt -n
//getopt -g
//getopt -l xiaohua
//getopt -ngl forever
int main3 (int argc, char **argv)
{
int oc; /*选项字符 */
char *b_opt_arg; /*选项参数字串 */
while((oc = getopt(argc, argv, "ngl:")) != -1)//:表示必须传入参数。
{
switch(oc)
{
case 'n':
printf("My name is Lyong.\n");
break;
case 'g':
printf("Her name is Xxiong.\n");
break;
case 'l':
b_opt_arg = optarg;
printf("Our love is %s\n", optarg);
break;
}
}
return 0;
}
//getopt -l
int main4(int argc, char **argv)
{
int oc; /*选项字符 */
char *b_opt_arg; /*选项参数字串 */
while((oc = getopt(argc, argv, "ngl:")) != -1)
{
switch(oc)
{
case 'n':
printf("My name is Lyong.\n");
break;
case 'g':
printf("Her name is Xxiong.\n");
break;
case 'l':
b_opt_arg = optarg;
printf("Our love is %s\n", optarg);
break;
case '?':
printf("arguments error!\n");
break;
}
}
return 0;
}
//getopt -a
//getopt -l
int main2 (int argc, char **argv)
{
int oc; /*选项字符 */
char ec; /*无效的选项字符*/
char *b_opt_arg; /*选项参数字串 */
opterr=0;
//在调用getopt()之前,将opterr设置为0,这样就可以在getopt()函数发现错误的时候强制它不输出任何消息。
while((oc = getopt(argc, argv, ":ngl:")) != -1)
{
switch(oc)
{
case 'n':
printf("My name is Lyong.\n");
break;
case 'g':
printf("Her name is Xxiong.\n");
break;
case 'l':
b_opt_arg = optarg;
printf("Our love is %s\n", optarg);
break;
case '?':
ec = (char)optopt;
printf("无效的选项字符 \' %c \'!\n", ec);
break;
case ':':
printf("缺少选项参数!\n");
break;
}
}
return 0;
}
http://blog.csdn.net/mr_jj_lian/article/details/6835137
相关文章推荐
- C语言_向函数传递结构体信息_plusC14.5
- 快速排序算法C语言
- 【链表】C++链表反转、链表逆序打印
- ROS学习之 cpp服务
- 【C语言入门教程】5.6 函数库和文件
- 【C语言入门教程】5.5 实现问题(效率)
- 【C语言入门教程】5.4 递归
- 【C语言入门教程】5.3 函数的调用 与 参数
- Leetcode 38. Count and Say (Easy) (cpp)
- 【C语言入门教程】5.2 函数的作用域规则(auto, static)
- C++后台实践:古老的CGI与Web开发
- c字符串的一些操作
- C语言范例学习02
- 【C语言入门教程】5.1 函数说明 与 返回值
- 【C语言入门教程】4.10 综合实例 - 媒体播放器
- C++使用gSoap写Web Server和Web Client
- 【C语言入门教程】4.9 指向指针的指针
- 《Windows via C/C++》学习笔记 —— 线程的相关性
- 【C语言入门教程】4.8 指针数组
- 【C语言入门教程】4.7 指针的地址分配 - mallocl(), free()