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

boost program_option case insensitive parse of a configuration file

本文关键字:大小写 不区 提升 option 配置文件 program      更新时间:2023-10-16

我想使用 boost::p rogram_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

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

这不是一个选择。

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

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