boost program_option不区分大小写的配置文件解析

时间:2018-06-19 13:49:16

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

我想使用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。 是否还可以进行区分大小写的解析,以便从配置文件读取?

1 个答案:

答案 0 :(得分:1)

那不是选择。

您可以向库维护者建议功能。

您可以使用其他工具来将inifile转换为首选大小写,或者添加用于大小写补码的选项说明。