c++之getopt函数的使用
命令行参数解析函数 —— getopt()
#include <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; |
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++之getopt函数的使用相关推荐
- Linux下getopt函数的使用
getopt为解析命令行参数函数,它是Linux C库函数.使用此函数需要包含系统头文件unistd.h. getopt函数声明如下: int getopt(int argc, char * cons ...
- python中getopt函数_python getopt模块详解
getopt这个函数 就是用来抽取 sys.argv 获得的用户输入来确定执行步骤. getopt是个模块,而这个模块里面又有getopt 函数,所以getopt需要这样这样用. getopt.get ...
- getopt 函数2
昨天在做code review的时候,看到函数getopt(),似乎没见过,狗狗之,大惊--发现原来自己干过N次愚蠢的事!!! 函数getopt()用来分析命令行参数(可怜的Sam啊- ...
- C语言-getopt函数
#include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...
- Mysql —— C语言链接mysql数据库,命令行形式(getopt()函数),用户、用户组增删改查(用户组表内有用户控制的策略字段)
函数说明--getopt(): 函数说明 getopt()用来分析命令行参数.参数argc和argv分别代表参数个数和内容,跟main()函数的命令行参数是一样的. optstring中的指定的内容的 ...
- Linux getopt()函数 getopt_long()函数---转
http://hi.baidu.com/scoundrelgg/item/d4083f8412eea05d26ebd97f Linux getopt()函数 getopt_long()函数 get_o ...
- [转载]Linux下getopt()函数的简单使用
转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...
- python中getopt函数_Python中getopt()函数的使用
在运行程序时,可能需要根据不同的条件,输入不同的命令行选项来实现不同的功能.目前有短选项和长选项两种格式.短选项格式为"-"加上单个字母选项:长选项为"--"加 ...
- C/C++ getopt()函数的介绍及使用
函数原型: int getopt(int argc,char * const argv[ ],const char * optstring); 前两个参数大家不会陌生,没错,就是老大main函数的两个 ...
- getopt函数应用
相关函数 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const argv[ ],const char * optstr ...
最新文章
- 【深度学习理论】(3) 激活函数
- 据说,程序员已成为女生年度最喜欢男友职业Top3?
- JavaScript数组方法大全解
- 1、cocos2dx开发学习第一篇-项目工程的创建
- imclearboder matlab,Lucas
- mysql命令大全_Keeplived+mysql双master高可用如何实现?
- 2017.9.20 回文串 思考记录
- CentOS 7 Graylog安装
- ImportError: No Module Named [x]
- mysql大于等于怎么写_MySQL 对于千万级的大表要怎么优化?我写了6000字的深度解读...
- [转载]遗留系统中的RUBY中文解决方案
- Java程序员常用的快捷键有哪些?
- R语言绘图及检验——正态分布曲线
- 将Spring Boot Web应用部署到Tomcat服务器
- android跳转到rn界面,第五章 RN与Native—由原生页面跳转到Rn页面;在Rn页面调用Android Native组件和Native数据...
- html怎样实现动态背景效果,利用jQuery实现动态背景特效
- Banner(轮播)
- Python2 简介
- mysql数据库引擎面试,mysql数据库引擎面试
- 计算机与电视连接方式是什么,电脑投屏到电视机怎么设置(详解两者间的投屏方法)...
热门文章
- 机器学习(一):模型的参数估计方法
- oracle vba 数组_vba 数据库目录
- 「信号机制」Python信号处理—signal模块
- Qt编程中的信号和槽机制
- 信号在传播中产生的不同衰落:多径效应、时延扩展和相干宽带
- 巧学活用html4,巧用教材、活用教材
- 计算并输出学生平均成绩。
- feded计算机音乐,Romeo And Jazzie/Alan Walker《Faded Refix》[FLAC/MP3-320K]
- LWN:使用Rust实现OpenPGP,这就是Sequoia项目!
- thinkphp6 循环 视图_ThinkPHP模板循环输出Volist标签用法实例详解