代码证明:
boost::program_options::options_description options;
Parser::Parser(): options("Allowed options")
{
options.add_options()
("help,h", "produce help message")
("type,t", po::value<std::string>()->required()->implicit_value(""), "Type")
}
这一行没问题:
("type,t", po::value<std::string>()->required()->implicit_value(""), "Type")
如何添加此行才能正常使用?:
("file,f", po::value< std::vector<std::string> >()->required()->multitoken()->implicit_value(std::vector<std::string>(0,"")), "File(s)")
这是string-s的向量。
答案 0 :(得分:0)
您只需要帮助选项说明了解如何向最终用户提供默认值。
也就是说,通常implicit_value
会使用lexical_cast<>
来获取文字表示,但(显然)不适用于vector<string>
。所以,提供你自己的文字表示:
("file,f", po::value<strings>()->required()
->implicit_value(strings { "santa", "claus" }, "santa,claus"), "File(s)");
完整演示
<强> Live On Coliru 强>
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char** argv) {
po::options_description options/*("Allowed options")*/;
using strings = std::vector<std::string>;
options.add_options()
("help,h", "produce help message")
("file,f", po::value<strings>()->required()->implicit_value(strings { "santa", "claus" }, "santa,claus"), "File(s)");
std::cout << options << "\n";
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, options, po::command_line_style::default_style), vm);
po::notify(vm);
auto types = vm["file"].as<strings>();
for (auto t : types)
std::cout << "Got: " << t << "\n";
}
打印:
-h [ --help ] produce help message
-f [ --file ] [=arg(=santa,claus)] File(s)
Got: santa
Got: claus