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 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用 ...
随机推荐
- qt qml 利用xmlhttprequest 调用有赞api
最近朋友在有赞商城上面开了一个店铺,因为有实体店,一般卖商品后送货上门,但是打票时候老是人工用world文档人工复制黏贴订单打印小票, 所以就找我帮忙做一个软件专门打印小票的,就研究起来调用有赞第三方 ...
- Application和Session的例子
%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationTest.as ...
- 2014ACM/ICPC亚洲区鞍山赛区现场赛1009Osu!
鞍山的签到题,求两点之间的距离除以时间的最大值.直接暴力过的. A - Osu! Time Limit:1000MS Memory Limit:262144KB 64bit IO Fo ...
- Uva 552 Prime Ring Problem(dfs)
题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...
- 1297 - Largest Box(三分)
1297 - Largest Box PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB In t ...
- splice()函数的使用方法
splice()函数的使用方法,这是一个拗口的函数.用起来有点麻烦.图3所看到的是splice函数的功能.将一个列表插入到还有一个列表其中.list容器类定义了splice()函数的3个版本号: sp ...
- c++中的对象引用(object reference)与对象指针的区别
★ 相同点: 1. 都是地址的概念: 指针指向一块内存,它的内容是所指内存的地址:引用是某块内存的别名. ★ 区别: 1. 指针是一个实体,而引用仅是个别名: 2. 引用使用时无需解引用(*),指针需 ...
- android 实现蓝牙自动配对连接
BluetoothConnectActivityReceiver.java:监听蓝牙配对的广播 代码: package com.imte.Broadcast; import com.imte.util ...
- Android 中 ListView 常用属性合集
class ListView.FixedViewInfo//用来在列表内展现一个固定位置视图,如在列表顶端的header和在列表底端的footer 一.XML属性 1.ListView的XML属性 a ...
- HDU 4891
一道简单的模拟题 需要要匹配{} 和 $$ 符里面的东西即可 //#pragma comment(linker, "/STACK:16777216") //for c++ Comp ...