如何在<string> boost::p rogram_options 中指定矢量的默认值

How do I specify a default value for vector<string> in boost::program_options

本文关键字:默认值 options lt string gt boost rogram      更新时间:2023-10-16

我确实想为位置参数提供一个默认值,如代码中的注释,但编译器抱怨。代码编译良好。我使用提升 1.46.1 和 g++

int main(int argc, char *argv[]) {
    namespace po = boost::program_options;
    po::positional_options_description p;
    p.add("path", -1);
    po::options_description desc("Options");
    std::vector<std::string> vec_str;
    std::string str;
    desc.add_options()
        ("foo,f", po::value< std::string >()->default_value(str), "bar")
        //("path,p", po::value< std::vector<std::string> >()->default_value(vec_str), "input files.")
        ("path,p", po::value< std::vector<std::string> >(), "input files.")
    ;
    po::variables_map vm;
    po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
    po::notify(vm);
}

我需要给出默认值的文本表示,请参阅 http://lists.boost.org/boost-users/2010/01/55054.php。

即以下行有效:

 ("path,p", po::value< std::vector<std::string> > ()->default_value(std::vector<std::string>(), ""), "input files.")

我想这是帮助输出所必需的,在我的示例中可以

std::cout << desc << std::endl;

由于编译器不知道如何重载operator<<()以进行vector<string>,因此它抱怨。