这并不简单,但是您需要的语法很奇怪,并且肯定需要一些手动调整,例如multitoken 语法的验证器,用于识别“额外”参数。
我先从最酷的部分开始:
./a.out 1st_positional --foo yes off false yes file.txt --bar 5 -- another positional
parsed foo values: 1, 0, 0, 1,
parsed bar values: 5
parsed positional values: 1st_positional, another, positional, file.txt,
因此,即使对于非常奇怪的选项组合,它似乎也可以工作。它还处理了:
./a.out 1st_positional --foo --bar 5 -- another positional
./a.out 1st_positional --foo file.txt --bar 5 -- another positional
解决办法
您可以在运行command_line_parser 之后手动篡改识别值,然后再使用store。
以下是粗略的草稿。它在 --foo multitoken 选项的末尾处理一个额外的令牌。它调用自定义验证并将最后一个违规标记移动到位置参数。我在代码之后描述的警告很少。我故意留下了一些调试couts,以便任何人都可以轻松地使用它。
所以这里是draft:
#include <vector>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/positional_options.hpp>
#include <boost/program_options/option.hpp>
#include <algorithm>
using namespace boost::program_options;
#include <iostream>
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;
}
bool validate_foo(const string& s)
{
return s == "yes" || s == "no";
}
int main(int ac, char* av[])
{
try {
options_description desc("Allowed options");
desc.add_options()
("help", "produce a help message")
("foo", value<std::vector<bool>>()->multitoken()->zero_tokens())
("bar", value<int>())
("positional", value<std::vector<string>>())
;
positional_options_description p;
p.add("positional", -1);
variables_map vm;
auto clp = command_line_parser(ac, av).positional(p).options(desc).run();
// ---------- Crucial part -----------
auto foo_itr = find_if( begin(clp.options), end(clp.options), [](const auto& opt) { return opt.string_key == string("foo"); });
if ( foo_itr != end(clp.options) ) {
auto& foo_opt = *foo_itr;
cout << foo_opt.string_key << '\n';
std::cout << "foo values: " << foo_opt.value << '\n';
if ( !validate_foo(foo_opt.value.back()) ) { // [1]
auto last_value = foo_opt.value.back(); //consider std::move
foo_opt.value.pop_back();
cout << "Last value of foo (`" << last_value << "`) seems wrong. Let's take care of it.\n";
clp.options.emplace_back(string("positional"), vector<string>{last_value} ); // [2]
}
}
// ~~~~~~~~~~ Crucial part ~~~~~~~~~~~~
auto pos = find_if( begin(clp.options), end(clp.options), [](const auto& opt) { return opt.string_key == string("positional"); });
if ( pos != end(clp.options)) {
auto& pos_opt = *pos;
cout << "positional pos_key: " << pos_opt.position_key << '\n';
cout << "positional string_key: " << pos_opt.string_key << '\n';
cout << "positional values: " << pos_opt.value << '\n';
cout << "positional original_tokens: " << pos_opt.original_tokens << '\n';
}
store(clp, vm);
notify(vm);
if (vm.count("help")) {
cout << desc;
}
if (vm.count("foo")) {
cout << "parsed foo values: "
<< vm["foo"].as<vector<bool>>() << "\n";
}
if (vm.count("bar")) {
cout << "parsed bar values: "
<< vm["bar"].as<int>() << "\n";
}
if (vm.count("positional")) {
cout << "parsed positional values: " <<
vm["positional"].as< vector<string> >() << "\n";
}
}
catch(exception& e) {
cout << e.what() << "\n";
}
}
所以我看到的问题是:
自定义验证应与解析器用于选项类型的验证相同。如您所见,program_options 对bool 比validate_foo 更宽容。您可以制作最后一个令牌false,它会被错误地移动。我不知道如何提取库用于选项的验证器,所以我提供了一个粗略的自定义版本。
向basic_parsed_options::option 添加一个条目相当棘手。它基本上与对象的内部状态相混淆。正如人们所看到的,我制作了一个相当基本的版本,例如它复制value,但仅留下original_tokens 向量,从而在数据结构中产生差异。其他字段也保持原样。
如果不考虑命令行中其他位置的positional 参数,可能会发生奇怪的事情。这意味着command_line_parser 将在basic_parsed_options::option 中创建一个条目,而代码将添加另一个具有相同string_key 的条目。我不确定后果,但它确实适用于我使用的奇怪示例。
解决问题 1. 可以使它成为一个很好的解决方案。我想其他东西是用于诊断的。 (虽然不是 100% 确定!)。也可以通过其他方式或循环识别违规令牌。
您可以删除违规令牌并将它们存储在一边,但将其留给boost_options 仍然使用它的验证例程,这可能很好。 (你可以试试把positional改成value<std::vector<int>>())