boost :: Program_options一种判断命令行或ini文件中的值的方法吗?

时间:2018-03-17 01:56:34

标签: 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”的值来自何处,以便我可以相应地修改该值。谢谢!

1 个答案:

答案 0 :(得分:0)

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

但是,您可以通过首先暂时解析命令行并在通知之前存储inifile和命令行选项来获得所需的结果。

  

这也消除了解析inifile之前的notify()会在命令行中没有提供缺少强制选项--abc的问题。

一个小技巧是使用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
相关问题