如何在boost :: program_options :: variable_map中存储数据? [英] How to store data in boost::program_options::variable_map?

查看:81
本文介绍了如何在boost :: program_options :: variable_map中存储数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试重做一些传给我的代码.该代码的原始点是读取配置文件,并在boost :: program_options :: variable_map中设置文件中的不同选项,然后读取该代码的其他所有已经正常工作的部分./p>

这是我要替换的代码:

 //一些有用的定义boost :: program_options :: variables_map vm;std :: string filecfg ="File_name";std :: ifstream ifs(filecfg.c_str());//设置选项(这是命令行示例,但配置文件相同)boost :: program_options :: options_description df(命令行");df.add_options()(帮助",显示帮助消息")("file-cfg,f",boost :: program_options :: value< std :: string>(),配置文件")(版本",显示版本和修订号");boost :: program_options :: parsed_options parsedc = boost :: program_options :: parse_config_file(ifs,df,true);boost :: program_options :: store(parsedc,vm);boost :: program_options :: notify(vm);std :: vector< std :: string>unrc = boost :: program_options :: collect_unrecognized(parsedc.options,boost :: program_options :: include_positional) 

我认为可以简单地替换boost :: program_options :: parsed_options parsedc并自己创建此对象.我遇到的问题只是没有有关如何执行此操作的文档.我认为主要是因为它不是设计用于这种方式的.

无论如何,我只是想用dc中描述的选项以及可以保存在单独的数据结构(如向量)中的值填充vm对象.

是否可以简单地将值添加到vm?还是我必须通过boost :: program_options :: store()之类的功能?

任何帮助将不胜感激!让我知道是否有不清楚的地方,或者有什么想让我尝试的地方!

谢谢!

解决方案

是的,可以.

请注意,您将必须决定如何模拟"/伪造"商品.它的其他语义.(例如,您可能想将这些选项伪装成默认选项)

从概念上讲,variable_map将是 map< string,variable_value> . variable_value :

期权的类别持有价值.包含有关值如何的详细信息设置并允许方便地获取该值.

还请注意,由于 variable_value 使用 boost :: any 进行存储,因此您必须 精确 您将存储的类型.(因此,如果您需要 std :: string("ah okay"),请不要存储"oops" ).

这是一个简单的演示:

在Coliru上直播

  #include< boost/program_options.hpp>#include< iostream>#include< iomanip>名称空间po = boost :: program_options;使用命名空间std :: string_literals;int main(/* int argc,char ** argv */){//一些有用的定义po :: variables_map vm;vm.emplace("file-cfg",po :: variable_value("string" s,true));vm.emplace("log-level",po :: variable_value(3,false));vm.emplace("option3",po :: variable_value {});notify(vm);std :: vector unrc = {无法识别的",选项"};对于(自动&[名称,值]:vm){std :: cout<<"名称:"<<名称<<std :: boolalpha<<" \ tdefaulted:"<<value.defaulted()<<" \ tempty:"<<value.empty();如果(typeid(std :: string)== value.value().type())std :: cout<<"-字符串"<<std :: quoted(value.as< std :: string>())<<"\ n";否则if(typeid(int)== value.value().type())std :: cout<<"-int"<<value.as< int>()<<"\ n";否则如果(!value.empty())std :: cout<<"-未知类型\ n";}} 

打印

 名称:file-cfg默认值:true空:false-字符串"string"名称:日志级别默认值:false空:false-整数3名称:option3默认值:false空:true 

I am currently trying to rework some code that was handed down to me. The original point of the code is to read a configuration file, and to set up the different options in the file in a boost::program_options::variable_map, which is then read throughout other parts of the code which is already working fine.

Here is the code I am trying to replace:

// Some helpful definitions
boost::program_options::variables_map vm;
std::string filecfg = "File_name";
std::ifstream ifs(filecfg.c_str());

// Setting options (This is command line example, but config file is the same)
boost::program_options::options_description df("Command Line");
df.add_options()
    ("help", "display help message")
    ("file-cfg,f", boost::program_options::value<std::string>(), "config file")
    ("version", "display version and revision number");

boost::program_options::parsed_options parsedc = boost::program_options::parse_config_file(ifs, df, true);
boost::program_options::store(parsedc, vm);
boost::program_options::notify(vm);
std::vector <std::string> unrc = boost::program_options::collect_unrecognized(parsedc.options, boost::program_options::include_positional)

My thinking it to simply replace the boost::program_options::parsed_options parsedc and create this object by myself. The problem I run into is simply that there is no documentation on how to do this. I think it is mostly because it is not designed to be used this way.

In any case, I am just looking to fill up the vm object with the options described in dc, and with values that I can hold in a separate data structure (like a vector).

Is it possible to simply add values to vm? Or do I have to go through a function such as boost::program_options::store()?

Any help would be greatly appreciated! Let me know if something is unclear, or if there is something you'd like me to try!

Thanks!

解决方案

Yeah you can.

Be aware that you will have to decide how to "mock"/"fake" the other semantics of it though. (E.g. you might want to masquerade the options as having been defaulted)

Conceptually, variable_map would be a map<string, variable_value>. variable_value:

Class holding value of option. Contains details about how the value is set and allows to conveniently obtain the value.

Note also that because variable_value uses boost::any for storage you will have to be exact about the types you will store. (So, don't store "oops" if you need a std::string("ah okay")).

Here's a simple demo:

Live On Coliru

#include <boost/program_options.hpp>
#include <iostream>
#include <iomanip>

namespace po = boost::program_options;
using namespace std::string_literals;

int main(/*int argc, char** argv*/) {
    // Some helpful definitions
    po::variables_map vm;

    vm.emplace("file-cfg", po::variable_value("string"s, true));
    vm.emplace("log-level", po::variable_value(3, false));
    vm.emplace("option3", po::variable_value{});
    notify(vm);

    std::vector unrc = { "unrecognized"s, "options"s };

    for (auto& [name, value] : vm) {
        std::cout
            << "Name: " << name
            << std::boolalpha
            << "\tdefaulted:" << value.defaulted()
            << "\tempty:" << value.empty();

        if (typeid(std::string) == value.value().type())
            std::cout << " - string " << std::quoted(value.as<std::string>()) << "\n";
        else if (typeid(int) == value.value().type())
            std::cout << " - int " << value.as<int>() << "\n";
        else if (!value.empty())
            std::cout << " - unknown type\n";
    }
}

Prints

Name: file-cfg  defaulted:true  empty:false - string "string"
Name: log-level defaulted:false empty:false - int 3
Name: option3   defaulted:false empty:true

这篇关于如何在boost :: program_options :: variable_map中存储数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆