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 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用 ...
随机推荐
- slf4j 和 log4j合用的(Maven)配置(转)
简述:添加logger的日志输出,下面是配置信息供备忘 步骤:1. 在Maven的porn.xml 文件中添加dependency如下 <dependency> <groupId&g ...
- IT第十九天 - 继承、接口、多态、面向对象的编程思想
IT第十九天 上午 继承 1.一般情况下,子类在继承父类时,会调用父类中的无参构造方法,即默认的构造方法:如果在父类中只写了有参的构造方法,这时如果在子类中继承时,就会出现报错,原因是子类继承父类时无 ...
- 解题报告 HDU1789 Doing Homework again
Doing Homework again Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- DicomIoException: Requested 132 bytes past end of fixed length stream.
今天在用DicomFile.Open(Stream s)这个接口时,遇到一个异常: DicomIoException: Requested 132 bytes past end of fix ...
- 请问set JAVA_OPTS的各项參数是什么意思?
http://topic.csdn.net/u/20090910/10/20c6ba01-28ac-482e-94b2-bfce0a952f77.html 请问set JAVA_OPTS的各项參数是什 ...
- 典型c库函数的实现
StrToInt:字符串转int输出 enum Status { kValid = , kInvalid = , }; int StrToInt(const char* str) { g_nStatu ...
- javascript 生成页面轮播元素
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> ...
- for循环语句之棋盘放粮食、百鸡百钱、纸张的折叠问题
1.棋盘放粮食 ; ; i < ; i++) { ; ; j <= i; j++) { x = x * ; } lszl = lszl + x; } double zl = lszl * ...
- for(;;)和 while(1) 有什么区别吗?for()和while()的使用情景。
1 for(;;)和 while(1) 有什么区别吗? void main(void) { ; // for(;;) ) { a++; } } arm-linux-gcc -c -o for.o fo ...
- BZOJ 1798: [Ahoi2009]Seq 维护序列seq( 线段树 )
线段树.. 打个 mul , add 的标记就好了.. 这个速度好像还挺快的...( 相比我其他代码 = = ) 好像是#35.. ---------------------------------- ...