代码之家  ›  专栏  ›  技术社区  ›  francesco

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

  •  2
  • francesco  · 技术社区  · 7 年前

    我想使用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
    

    如果我换了 输入输出 作为

    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 . 是否也可以使用case-insentive解析来读取配置文件?

    1 回复  |  直到 7 年前
        1
  •  1
  •   sehe    7 年前

    这不是一个选择。

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

    您可以使用其他工具将IIFIL转换为首选情况,或者为案例补充添加选项说明。