`

program_options读命令行和配置文件

 
阅读更多
#include <boost/program_options.hpp>
#include <string.h>
#include <iostream>
#include <fstream>
#include <map>

using namespace std;
using namespace boost;

namespace bpo = boost::program_options;

/*
config.ini:
p2p-endpoint = 0.0.0.1:31010

rpc-endpoint = 0.0.0.0:38090

*/
int main(int argc,char** argv)
{
    cout << "cmd line:" << endl;
    bpo::options_description desc("Allowed options");
    desc.add_options()
            ("help", "produce help message")
            ("df_value", bpo::value<string>()->default_value("123"), ""); //give default

    bpo::variables_map cmdValueMap;
    bpo::store(bpo::parse_command_line(argc, argv, desc), cmdValueMap);
    bpo::notify(cmdValueMap);

    if(cmdValueMap.count("help")){
        cout<<desc<<endl;
    }
    if(cmdValueMap.count("df_value")){
        cout << "df_value:" << cmdValueMap["df_value"].as<string>() << endl;
    }

    cout << endl;
    cout << "直接读取文件配置:" << endl;
    boost::program_options::options_description options("Graphene Witness Node");
    bpo::variables_map fileValueMap;
    options.add_options()
             ("p2p-endpoint", bpo::value<string>(), "Endpoint for P2P node to listen on")
             ("rpc-endpoint", bpo::value<string>(),"aaa")
             ("genesis-json", bpo::value<string>()->default_value("my-genesis.json"),"genesis-json");

    bool useIfStream = true;//both is ok
    if(useIfStream)
    {
        ifstream ifs("config.ini");
        boost::program_options::store(boost::program_options::parse_config_file(ifs,options), fileValueMap);
    }else
    {
        boost::program_options::store(boost::program_options::parse_config_file<char>("config.ini",options, true), fileValueMap);
    }
    boost::program_options::notify(fileValueMap);

    if(fileValueMap.count("p2p-endpoint")){
        cout << fileValueMap["p2p-endpoint"].as<string>() << endl;
    }
    if(fileValueMap.count("rpc-endpoint")){
        cout << fileValueMap["rpc-endpoint"].as<string>() << endl;
    }
    if(fileValueMap.count("genesis-json")){
        cout << fileValueMap["genesis-json"].as<string>() << endl;
    }

    cout << endl;
    cout << "遍历valueMap:" << endl;
    for(bpo::variables_map::iterator iter = fileValueMap.begin(); iter != fileValueMap.end(); ++iter){
        cout << iter->first << ":" << iter->second.as<string>() << endl;
    }
}




执行:./a.out --help

cmd line:
Allowed options:
  --help                produce help message
  --df_value arg (=123)

df_value:123

直接读取文件配置:
0.0.0.1:31010
0.0.0.0:38090
my-genesis.json

遍历valueMap:
genesis-json:my-genesis.json
p2p-endpoint:0.0.0.1:31010
rpc-endpoint:0.0.0.0:38090
分享到:
评论

相关推荐

    program_options:Boost.org program_options模块

    程序选项是,它允许通过常规方法(例如命令行和配置文件)从用户定义和获取(名称,值)对。 它与getopt_long大致相似,但可用于C ++。 执照 根据分发。 特性 C ++ 03 需要链接 建置状态 (进行中...) 分支 ...

    C++命令行参数的实现方法及使用方法,代码详细,执行正确。

    执行程序时,经常会用到命令行参数传递参数这种方式,今天实战一个命令行参数的从头到尾。 博文虽长,但均为干货。...program options是一系列pair组成的选项列表,它允许程序通过命令行或配置文件来读取这些参

    pcchess:并行中国象棋(象棋)引擎

    #平行国际象棋##依赖Boost&gt; = 1.57(组件:mpi,序列化,date_time,program_options) CMake&gt; = 2.8 螺纹GCC&gt; = 5.1.1(或clang&gt; = 3.4)* OpenMP(可选,但推荐) gperftools(Google Performance Tools,可选,...

    cmake-boost-example:增强CMake使用示例

    cmake-boost示例 ...Program options解析命令行参数 Asio网络 Property Tree解析XML配置文件 Test单元测试 String Algo字符串算法 System系统错误码 Serialization序列化 Uuid通用统一标识码 CRC和计算 Outcome返

    raconfig:C ++仅标头库,可轻松进行程序配置处理

    Boost.Program Options上的声明式配置包装器动机Boost.Program Options是一个非常好的库,用于从命令行,文本文件甚至进程环境中获取选项。 但这还需要编写命令性代码以设置解析器,并且经常需要使用琐碎的(或没有...

    svn-1.4.3-setup.exe.7z

    为了让 Apache 知道哪个 URL 是用作 Subversion 版本库,你需要使用任意文本编辑器(例如记事本)编辑 Apache 的配置文件(通常是 c:\program files\apache group\apache2\conf\httpd.conf): 1. 在配置文件最后添加...

    flynnid:将单个Selenium节点注册到Selenium Grid Hub

    安装从PyPI: $ pip install flynnid来自来源: $ python setup.py install运行FlynnID FlynnID需要JSON格式的配置文件或命令行参数。 使用配置文件允许注册一个或多个多个节点,而命令行参数仅允许注册一个节点。 ...

    跨平台:将NGINX配置转换为JSON并返回的快速可靠方法

    可靠,快速的NGINX配置文件解析器和生成器 安装 您可以通过以下方式安装和: pip install crossplane 命令行界面 usage: crossplane &lt;command&gt; [options] various operations for nginx config files optional ...

    Squid中文权威指南

    5.2 对配置文件查错 5.3 初始化cache目录 5.4 在终端窗口里测试squid 5.5 将squid作为服务进程运行 5.5.1 squid_start脚本 5.6 启动脚本 5.6.1 /etc/rc.local 5.6.2 init.d和rc.d 5.6.3 /etc/inittab ...

    如何编写批处理文件批处理文件批处理文件

    简明批处理教程22009年10月20日 星期二 下午 05:35 最近对于批处理技术的探讨比较热,也有不少好的批处理程序发布,但是如果没有一定的相关知识恐怕不容易看懂和理解这些批处理文件,也就更谈不上自己动手编写了,古...

    代码语法错误分析工具pclint8.0

    2.将c:\pclint\lnt 下的3个文件lib-w32.lnt,env-vc6.lnt,co-msc60.lnt拷贝至c:\pclint下, 再在安装目录下创建std.lnt和options.lnt两个文件,其中std.lnt的内容如下 // contents of std.lnt c:\pclint\co-msc60...

    ELDK使用与开发手册

    (译者注:应该根据你自己的具体开发板配置,如$ make &lt;yourboard&gt;_config,如果没有相应的开发板,应该自己照着建立相应的目录和配置文件。) 最后我们可以开始编译U-Boot了: $ make all 5.4 安装 5.4.1 动手之前 ...

    PHP5 完整官方 中文教程

    Program Execution — Program Execution Functions PS — PostScript document creation Pspell — Pspell Functions qtdom — qtdom Functions radius — Radius Rar — Rar Functions Readline — GNU Readline ...

    PHP5中文参考手册

    Program Execution — Program Execution Functions PS — PostScript document creation Pspell — Pspell Functions qtdom — qtdom Functions radius — Radius Rar — Rar Functions Readline — GNU Readline ...

    everything

    现在双击找到的文件会打开,双击目录会激活tc跳到该目录(如果你想设定在左边,右边或者新建一个tab可以尝试加上tc的命令行参数,不一定能用),右键选择文件,open path会用tc打开文件所在目录。 再在tc里或者其他...

    php帮助文档,php。chm,php必备的中文手册

    Program Execution Functions CXIX. PostScript document creation CXX. Pspell Functions CXXI. qtdom Functions CXXII. Radius CXXIII. Rar Functions CXXIV. GNU Readline CXXV. GNU Recode Functions CXXVI. ...

    PHP函数参考手册大全

    作者和贡献者 I. 入门指引 1. 简介 2. 简明教程 II. 安装与配置 3. 安装前需要考虑的事项 4. Unix 系统下的安装 5. Mac OS X 系统下的安装 6. Windows 系统下的安装 7. PECL 扩展库安装 8. 还有问题? 9. 运行时配置...

    中文版PHP使用手册

    Program Execution Functions CXVIII. Pspell Functions CXIX. qtdom Functions CXX. Radius CXXI. Rar Functions CXXII. Perl 兼容正则表达式函数 CXXIII. POSIX 扩展正则表达式函数 CXXIV. runkit Functions CXXV...

    php手册PHP5研究室编无乱码版本chm

    Program Execution Functions CXVIII. Pspell Functions CXIX. qtdom Functions CXX. Radius CXXI. Rar Functions CXXII. Perl 兼容正则表达式函数 CXXIII. POSIX 扩展正则表达式函数 CXXIV. runkit ...

Global site tag (gtag.js) - Google Analytics