如何在boost :: program_options </string>中为vector <string>指定默认值

时间:2012-08-10 10:11:28

标签: c++ boost boost-program-options

我想为代码中的注释提供位置参数的默认值,但编译器会抱怨。这个代码编译得很好。我使用boost 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);
}

1 个答案:

答案 0 :(得分:12)

我需要提供默认值的文字表示,请参阅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<<()的{​​{1}}重载,它会抱怨。

相关问题