boost之program_options库,解析命令行参数、读取配置文件
一、命令行解析
tprogram_options解析命令行参数示例代码:
- #include <iostream>
- using namespace std;
- #include <boost/program_options.hpp>
- namespace po = boost::program_options;
- int main(int argc, char*argv[])
- {
- //int level;
- po::options_description desc("Allowed options");
- desc.add_options()
- ("help", "produce help message")
- //("help,h", "produce help message")
- ("compression", po::value<int>(), "set compression level");
- //("compression", po::value<int>(&level)->default_value(1), "set compression level");
- po::variables_map vm;
- po::store(po::parse_command_line(argc, argv, desc), vm);
- po::notify(vm);
- if(vm.count("help"))
- {
- cout<<desc<<endl;
- return 1;
- }
- if(vm.count("compression"))
- {
- cout<<"compression level was set to "<<vm["compression"].as<int>()<<"."<<endl;
- //cout<<"compression level: "<<level<<endl;
- }
- else
- {
- cout<<"compression level was not set."<<endl;
- }
- return 0;
- }
运行结果:
输入参数:--help
输入参数:--compression 10
二、读取配置文件(Linux、Windows均可)
2.1 代码
- #include <fstream>
- #include <map>
- using namespace std;
- #include <boost/program_options.hpp>
- using namespace boost;
- namespace po = boost::program_options;
- #ifdef WIN32
- #include "C:\Users\gwy8868\Desktop\fast_dr302\fast_dr302\global\xtokens.h"
- #else
- #include "/opt/guowenyan/fast_dr302/global/xtokens.h"
- #endif
- std::pair<std::string, std::string> at_option_parser(std::string const& s)
- {
- if ('@' == s[0])
- {
- return make_pair(std::string("config"), s.substr(1));
- }
- else
- {
- return std::pair<std::string, std::string>();
- }
- }
- int main(int argc, char*argv[])
- {
- //
- string host_ip;
- short host_port;
- string server_ip;
- short server_port;
- //
- po::options_description hostoptions("host options");
- hostoptions.add_options()
- ("host_ip,H", po::value<string>(&host_ip), "set db_host")
- ("host_port,P", po::value<short>(&host_port)->default_value(3306), "set db_port");
- po::options_description general("general options");
- general.add_options()
- ("help,h", "produce help message")
- ("server_ip,s", po::value<string>(&server_ip), "set the http_server's ip. e.g. '202.106.0.20'")
- ("server_port,p", po::value<short>(&server_port)->default_value(80), "set the http_server's port. default:80");
- string config_file;
- po::options_description config("config options");
- config.add_options()
- ("config", po::value<string>(&config_file)->default_value("config.conf"),
- "set config file, specified with '@name' too");
- po::options_description all("All options");
- all.add(hostoptions).add(general).add(config);
- po::variables_map vm;
- po::store(po::command_line_parser(argc, argv).options(all).extra_parser(::at_option_parser).run(), vm);
- if (vm.count("help"))
- {
- cout << hostoptions <<endl;
- cout << general << endl;
- cout << config << endl;
- return false;
- }
- if (vm.count("config"))
- {
- string conf_name = vm["config"].as<string>();
- ifstream ifs_config(conf_name.c_str());
- if (! ifs_config)
- {
- cerr << "could not open the configure file" << endl;
- return false;
- }
- stringstream ss_config;
- ss_config << ifs_config.rdbuf();
- global::strings_t args;
- global::separate_tokens(ss_config.str(), args, " \r\n");
- po::store(po::command_line_parser(args).options(all).run(), vm);
- }
- po::notify(vm);
- //
- cout<<"host_ip: "<<host_ip<<endl;
- cout<<"host_port: "<<host_port<<endl;
- cout<<"server_ip: "<<server_ip<<endl;
- cout<<"server_port: "<<server_port<<endl;
- return 0;
- }
2.2 配置文件
config.conf:
config2.conf:
2.3 输出结果
boost之program_options库,解析命令行参数、读取配置文件的更多相关文章
- linux 中解析命令行参数(getopt_long用法)
linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...
- optparse模块解析命令行参数的说明及优化
一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考: ...
- python解析命令行参数
常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.a ...
- C语言中使用库函数解析命令行参数
在编写需要命令行参数的C程序的时候,往往我们需要先解析命令行参数,然后根据这些参数来启动我们的程序. C的库函数中提供了两个函数可以用来帮助我们解析命令行参数:getopt.getopt_long. ...
- Windows下解析命令行参数
linux通常使用GNU C提供的函数getopt.getopt_long.getopt_long_only函数来解析命令行参数. 移植到Windows下 getopt.h #ifndef _GETO ...
- 3.QT中QCommandLineParser和QCommandLineOption解析命令行参数
1 新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include & ...
- 使用 Apache Commons CLI 解析命令行参数示例
很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...
- Shell 参数(2) --解析命令行参数工具:getopts/getopt
getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,geto ...
- getopt_long函数解析命令行参数
转载:http://blog.csdn.net/hcx25909/article/details/7388750 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用 ...
随机推荐
- C语言之一数三平方
一数三平方 有这样一个六位数,它本身是一个整数的平方,其高三位和低三位也分别是一个整数的平方,如225625=475*475,225=15*15,625=25*25;统计所有符合该条件的六位数 源代码 ...
- Android 网络交互之MD5为什么要加盐
MD5为什么要加盐 之前面试的时候,遇到一个面试的哥哥.不停的跟我确认我对网络传输过程中的password进行MD5加密的时候,是否加key了. 当时我很纳闷,因为MD5本身已经是不可逆的了,需要破解 ...
- Javabyte[]数组和十六进制String之间的转换Util------包含案例和代码
Java中byte用二进制表示占用8位,而我们知道16进制的每个字符需要用4位二进制位来表示(23 + 22 + 21 + 20 = 15),所以我们就可以把每个byte转换成两个相应的16进制字符, ...
- 使用SharedPreferences即时存储之后,不能即时获取到数据
在这里简介一下我所遇到的情况,由于情况非常特殊,所以我就来记录一下自己在这个方面的经历! 事由:在我所做的app中有一个视频的播放功能,因为之前做优化的时候.我听说对于视频这种比較耗费资源的应该给他独 ...
- Robotium之Android控件定位实践和建议(Appium/UIAutomator姊妹篇)
本人之前以前撰文描写叙述Appium和UIAutomator框架是怎样定位Android界面上的控件的. UIAutomator定位Android控件的方法实践和建议 Appium基于安卓的各种Fin ...
- Android Intent 解析之二
服务端Intent运行过程: Sticky:这个类型的BroadCast表示某些Intent须要被保留,当新的应用起来后,须要关注这个消息,可是呢,又不须要启动这个应用来接收此消息,比方耳机插入等消息 ...
- java中将list、map对象写入文件
链接地址:http://blog.sina.com.cn/s/blog_4a4f9fb50101p6jv.html 推荐:凤爪女瓜子男怪象该谁反思伦敦房价为什么持续暴涨 × wvqusrtg个 ...
- [转] Chrome 控制台不完全指南
转自: http://www.cnblogs.com/Wayou/p/chrome-console-tips-and-tricks.html#home Chrome的开发者工具已经强大到没朋友的地步了 ...
- Chapter 13 建造者模式
建造者模式又叫生成器模式:将一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象. 代码: package xiao; import java.util. ...
- kingso_sort - Taocode
kingso_sort - Taocode 如何编写新sort 由于排序逻辑多种多样,kingso的排序设计成是由一个个排序对象串起的排序链条组成.排序对象之间可以任意组合(只需要改配置文件),就可以 ...