在您通知所有解析结果并将它们合并到单个变量映射之后,我不知道检测有效选项值来源的优雅方法。
但是,您可以通过首先尝试解析命令行并在 notify 之前存储 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