13

我正在使用 Boost program_options 以标准方式解析配置文件,如
program_options 的 multiple_sources.cpp 示例文件所示。

    ifstream ini_file("config.ini");  
    po::store(po::parse_config_file(ini_file, desc, true), vm);  
    po::notify(vm);

然而,config.ini 文件有空的键=值对,例如:

key1=value1  
key2=value2  
key3=  
key4=  
key5=value5  

尝试读取此文件会导致 Boost 错误:

boost::program_options::invalid_option_value

boost::program_options 中是否有任何机制来读取这些带有空整的配置文件?

任何帮助将非常感激。


我正在编辑此问题,因为问题尚未解决。我将仅引用 Boost (1.53) 中的示例。

这是完整的 multiple_sources.cpp 文件:

#include <boost/program_options.hpp>
namespace po = boost::program_options;


#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;

// A helper function to simplify the main part.
template<class T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    copy(v.begin(), v.end(), ostream_iterator<T>(os, " ")); 
    return os;
}


int main(int ac, char* av[])
{
    try {
        int opt;
        string config_file;

        // Declare a group of options that will be 
        // allowed only on command line
        po::options_description generic("Generic options");
        generic.add_options()
            ("version,v", "print version string")
            ("help", "produce help message")
            //("optimization","optimization level")      
            ("config,c", po::value<string>(&config_file)->default_value("multiple_sources.cfg"),
                  "name of a file of a configuration.")
            ;

        // Declare a group of options that will be 
        // allowed both on command line and in
        // config file
        po::options_description config("Configuration");
        config.add_options()
            ("optimization", po::value<int>(&opt)->default_value(10), 
                  "optimization level")
            ("include-path,I", po::value< vector<string> >()->composing(), 
                 "include path")
            ;

        // Hidden options, will be allowed both on command line and
        // in config file, but will not be shown to the user.
        po::options_description hidden("Hidden options");
        hidden.add_options()
            ("input-file", po::value< vector<string> >(), "input file")
            ;

        po::options_description cmdline_options;
        cmdline_options.add(generic).add(config).add(hidden);

        po::options_description config_file_options;
        config_file_options.add(config).add(hidden);

        po::options_description visible("Allowed options");
        visible.add(generic).add(config);

        po::positional_options_description p;
        p.add("input-file", -1);

        po::variables_map vm;
        store(po::command_line_parser(ac, av).
              options(cmdline_options).positional(p).run(), vm);
        notify(vm);

        ifstream ifs(config_file.c_str());
        if (!ifs)
        {
            cout << "can not open config file: " << config_file << "\n";
            return 0;
        }
        else
        {
            store(parse_config_file(ifs, config_file_options), vm);
            notify(vm);
        }

        if (vm.count("help")) {
            cout << visible << "\n";
            return 0;
        }

        if (vm.count("version")) {
            cout << "Multiple sources example, version 1.0\n";
            return 0;
        }

        if (vm.count("include-path"))
        {
            cout << "Include paths are: " 
                 << vm["include-path"].as< vector<string> >() << "\n";
        }

        if (vm.count("input-file"))
        {
            cout << "Input files are: " 
                 << vm["input-file"].as< vector<string> >() << "\n";
        }

        cout << "Optimization level is " << opt << "\n";                
    }
    catch(exception& e)
    {
        cout << e.what() << "\n";
        return 1;
    }    
    return 0;
}

而对应的配置文件(multiple_sources.cfg)为:

#
# Comment out this line to use hard-coded default value of 10
# 
optimization = 1
include-path = /opt

如果现在将此文件修改为:

#
# Comment out this line to use hard-coded default value of 10
# 
optimization = 
include-path = /opt

抛出以下错误消息:

the argument for option 'optimization' is invalid

具有验证重载等的建议解决方案似乎不必要地复杂,特别是因为可能必须为每种数据类型编写验证函数,并结合识别“\ n”其他空白的可能性。

有什么建议么?感谢大家抽出宝贵的时间。


按照 Aditya 的建议,我替换了以下行:

            ("optimization", po::value<int>(&opt)->default_value(10), 
                  "optimization level")

具有以下内容:

            ("optimization", po::value<int>(&opt)->zero_tokens(), 
                  "optimization level")

和 :

            ("optimization", po::value<int>(&opt)->implicit_value(10), 
                  "optimization level")

他们都没有阅读空白选项。Boost 示例程序“parser_test.cpp”通过将键值对读入向量中绕过了 zero_tokens() 或implicit_value() 的使用,如下所示:

void test_config_file(const char* config_file)
{
    options_description desc;
    desc.add_options()
        ("gv1", new untyped_value)
        ("gv2", new untyped_value)
        ("empty_value", new untyped_value)
        ("plug*", new untyped_value)
        ("m1.v1", new untyped_value)
        ("m1.v2", new untyped_value)
        ("b", bool_switch())
    ;

    const char content1[] =
    " gv1 = 0#asd\n"
    "empty_value = \n"
    "plug3 = 7\n"
    "b = true\n"
    "[m1]\n"
    "v1 = 1\n"
    "\n"
    "v2 = 2\n"    
    ;

    vector<option> a2 = parse_config_file<char>(config_file, desc).options;
    BOOST_REQUIRE(a2.size() == 6);
    check_value(a2[0], "gv1", "0");
    check_value(a2[1], "empty_value", "");
    check_value(a2[2], "plug3", "7");
    check_value(a2[3], "b", "true");
    check_value(a2[4], "m1.v1", "1");
    check_value(a2[5], "m1.v2", "2");
}
4

3 回答 3

1

我建议你用 try/catch 这个异常包围,并且只为那些对你的程序真正必要并且不能为空的字段抛出错误,否则该字段将被忽略。

于 2013-03-27T14:08:24.347 回答
1

不,目前没有办法在 boost::program_options 中处理这个问题。在 INI 文件中有一个空的键:值对与在命令行上指定选项而不提供参数相同。ypnos 建议的编写自定义验证器的方法可能有效,但似乎不切实际,因为您需要将其应用于您期望的每个选项,可能会留空。您必须编写自己的 po::parse_config_file 实现,它会忽略没有值的行(除非相应的选项被标记为 zero_token)以获得您正在寻找的结果。

于 2014-01-09T16:34:45.713 回答
1

我多次偶然发现这个问题,最后通过从初始 INI 文件中删除无效行,然后将其传递给program_options::parse_config_file. 可以通过在文件系统上创建一个临时修改的 INI 文件或将整个原始 INI 文件读取到内存并在那里修改它,然后创建istringstream以传递给parse_config_file.

boost::iostreams我个人为此创建了一个过滤流类来动态过滤ifstream。这不会创建任何新的依赖项,因为我已经使用了 Boost,并且boost::iostreams在我的情况下只能使用标头。

这是它的使用方式:

#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/program_options.hpp>
#include "inifilefilter.h" // IniFileInputFilter
...
std::ifstream file("config.ini");
if (file)
{
   // filtering out empty values, i.e. name=
   boost::iostreams::filtering_istream filteredIniFile; 
   filteredIniFile.push(IniFileInputFilter());
   filteredIniFile.push(file);
   po::parsed_options parsedFileOpts =
         po::parse_config_file(filteredIniFile, configFileOptions, true);
   ...
}

我不会从inifilefilter.h这里展示代码,因为它对所有检查和极端情况都有些冗长,但同时它的想法很简单。' IniFileInputFiltersread()方法缓冲每个输入行,然后在行尾仅在有效时才进一步传递该行。“有效”意味着它要么是一行,要么在第一行和行尾[section]之间有一些非空格字符。'='

于 2018-09-05T16:19:28.830 回答