boost之program_options库,解析命令行参数、读取配置文件
来源:互联网 发布:三角形软件测试报告 编辑:程序博客网 时间:2024/06/05 09:58
一、命令行解析
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;
- }
config.conf:
config2.conf:
2.3 输出结果
0 0
- boost之program_options库,解析命令行参数、读取配置文件
- boost之program_options库,解析命令行参数、读取配置文件
- boost之program_options库,解析命令行参数、读取配置文件
- boost之program_options库,解析命令行参数、读取配置文件
- boost学习之命令行参数解析库program_options
- boost::program_options 解析命令行参数
- boost.Program_options 命令行参数解析
- boost库之program_options
- Boost库之Program_options
- 【boost学习笔记】命令行解析库(program_options)
- ACE命令行参数解析及配置文件读取
- Boost库之program_options的使用
- Boost库之program_options的使用
- Boost:解析命令行参数
- boost::program_options 命令行选项 子命令
- boost:库program_options--第一篇
- boost program_options
- boost.program_options
- eclipse java 注释模板
- ADRCI 命令删除oracle 11g alert 日志
- myEclipse 安装插件
- UICollectionView的使用
- struts2 与 OGNL 表达式,jsp中 利用ognl 在valuestack中取值
- boost之program_options库,解析命令行参数、读取配置文件
- CFS完全公平调度算法 - per entity load tracking 几个重要的函数分析
- 文本向量表示及TFIDF词汇权值
- 构件图
- js中如何调用Jsp中的变量
- DedeCMS Error: (PHP 5.3 and above) Please set request_order
- FPGA设计开发软件ISE使用技巧之:片上逻辑分析仪(ChipScope Pro)使用技巧
- [代码阅读]gem5 classic cache初步(3)
- 错误处理(Error Handling)