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

标签 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 . 是否也可以对配置文件进行不区分大小写的解析?

最佳答案

那不是一个选择。

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

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

关于c++ - boost program_option 不区分大小写的配置文件解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50930123/

相关文章:

c++ - 对于一个参数

c++ - 在 STL 集合和自定义比较函数中使用结构时避免复制

c++ - boost::program_options::store 在选项字符串包含混合语言字符时抛出异常

c++ - 在程序选项值(ini 文件)中使用井号

c++ - 我怎样才能找出这个 ffmpeg 错误代码的含义?

c++ - Visual Studio 无法打开包含文件;没有这样的文件或目录

c++ - 从目录中提取以特定单词开头的所有文件

c++ - boost::property_tree 传递包含 <xmlattr> 的子树

visual-studio-2010 - 如何通过b2使用pdb文件构建boost库

c++ - Boost 程序选项 - 从函数结果传递 arg 名称