c++ - boost::Program_options 一种判断值是来自命令行还是 ini 文件的方法?

标签 c++ boost boost-program-options

我想知道 Boost::Program_options 中是否有一种方法可以指示选项的值(在我的示例中为“abc”)来自何处,它是来自命令行还是来自 ini 文件。原因是如果值来自 ini 文件,我会修改它。这是我的选项说明:

po::options_description desc{ "Options" };
desc.add_options()
    ("abc", po::wvalue<std::wstring>()->required(), "Path to abc.txt")
    ("ini", po::wvalue<std::wstring>(), "INI file path.");

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);

if (vm.count("ini"))  
{
    wifstream ifs(vm["ini"].as<std::wstring>(););
    if (ifs)
    {
        store(po::parse_config_file(ifs, desc, true), vm);
    }
}

如您所见,“abc”是必需的选项,因此可以从命令行、ini 文件或两种方法输入(命令行值具有更高的优先级)。如上所述,我想知道是否有一种方法可以指示“abc”的值来自何处,以便我可以相应地修改该值。谢谢!

最佳答案

在通知所有解析结果并将它们合并到单个变量映射后,我不知道检测有效选项值来源的优雅方法。

但是,您可以通过先尝试解析命令行,并在通知之前将 ini 文件和命令行选项都存储起来来获得所需的结果。

This also removes the problem that the notify() before parsing the inifile would complain about missing mandatory option --abc if none was supplied at the command line.

一个小技巧是使用 allow_unregistered() 忽略 boostrap_command_line() 中的所有其他选项:

Live On Coliru

#include <boost/program_options.hpp>
#include <boost/iostreams/stream.hpp>
#include <fstream>
#include <iostream>

namespace po = boost::program_options;

po::variables_map boostrap_command_line(int argc, char** argv) {
    po::options_description desc{ "Bootstrap Options" };
    desc.add_options()
        ("ini", po::value<std::string>()->default_value(""), "INI file path.");

    po::variables_map vm;
    po::store(po::command_line_parser(argc, argv).options(desc).allow_unregistered().run(), vm);
    po::notify(vm);
    return vm;
}

int main(int argc, char** argv) {
    po::options_description desc{ "Options" };

    auto boostrap = boostrap_command_line(argc, argv);

    desc.add_options()
        ("abc", po::wvalue<std::wstring>()->required(), "Path to abc.txt")
        ("ini", po::wvalue<std::wstring>(), "INI file path.");

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);

    if (boostrap.count("ini")) {
        // Sidenote: the standard does not allow `std::wifstream` to construct from
        // `std::wstring`, see
        // http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream
        std::wifstream ifs(boostrap["ini"].as<std::string>());
        if (ifs) {
            store(po::parse_config_file(ifs, desc, true), vm);
        }
    }

    po::notify(vm);

    std::wcout << "abc option: " << vm["abc"].as<std::wstring>() << "\n";
}

测试:

$ ./a.out --abc direct

abc option: direct

$ touch test.ini
$ ./a.out --ini test.ini

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::required_option> >'
  what():  the option '--abc' is required but missing
bash: line 9: 11369 Aborted                 (core dumped) ./a.out --ini test.ini

$ echo abc=fromini >> test.ini
$ ./a.out --ini test.ini

abc option: fromini

$ ./a.out --ini test.ini --abc cli-overrides

abc option: cli-overrides

关于c++ - boost::Program_options 一种判断值是来自命令行还是 ini 文件的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49331768/

相关文章:

c++ - 如何在 C/C++ 中使用 Ruby

c++ - C++ 中的模板参数

c++ - 没有 getline 的重载函数实例

c++ - 在 C++ 中使用 Boost 的线程池无法正常工作

c++ - boost::make_zip_iterator 的 decltype?

c++ - 从有更多参数的配置文件中读取一个参数,使用 boost program_options

c++ - 为什么这个 C++ 显式模板特化代码是非法的?

c++ - (boost.python) 暴露重载的 operator+() 时出错。 "TypeError: No to_python (by-value) converter found"

c++ - 为什么 boost::program_options 接受切词?

c++ - Boost 程序选项 bool 总是 True