【问题标题】:boost program_option case insensitive parse of a configuration fileboost program_option 不区分大小写的配置文件解析
【发布时间】:2018-11-28 12:27:15
【问题描述】:

我想使用 boost::program_options 从配置文件中读取选项,允许不区分大小写的解析。

考虑一下,例如,下面的简单代码:

#include <iostream>
#include <fstream>
#include <string>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>

int main()
{
  using namespace std;
  namespace po = boost::program_options;

  ifstream inputfile("input.dat");
  po::options_description desc("");
  desc.add_options()
    ("l", po::value<unsigned int>())
    ;
  po::variables_map vm;
  po::store(po::parse_config_file(inputfile, desc), vm);
  po::notify(vm);

  if (vm.count("l"))
    cout << "l is set as " << vm["l"].as<unsigned int>() << endl;
  else
    cout << "l is not set";

  return 0;
}

带有以下input.dat文件

l=3

程序运行良好并给出输出

l is set as 3

如果我将input.dat 更改为

L=3

程序终止引发异常

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::unknown_option> >'
  what():  unrecognised option 'L'
Aborted

在命令行上显然可以进行不区分大小写的解析,请参阅here 的讨论。 是否也可以进行不区分大小写的解析以从配置文件中读取?

【问题讨论】:

    标签: c++ boost boost-program-options


    【解决方案1】:

    这不是一个选项。

    您可以向库维护者推荐功能。

    您可以使用其他工具将 inifile 转换为首选大小写,或者为大小写补充添加选项描述。

    【讨论】:

    • 谢谢,我怀疑。按照您的建议,我将过滤后的输入文件插入stringstream 对象并将其传递给po::parse_config_file
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 2014-04-15
    • 2012-02-03
    • 2012-03-09
    • 1970-01-01
    • 2011-08-30
    相关资源
    最近更新 更多