13

I do want to give a default value for the positional parameter as in the comment in the code, but the compiler complains. The code as it is compiles fine. I use boost 1.46.1 and 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);
}
Michael
  • 1,464
  • 1
  • 20
  • 40
  • 1
    [Does this help?](http://stackoverflow.com/a/3152802/220636) – nabulke Aug 10 '12 at 12:08
  • this line compiles:
    ("path,p", po::value< std::vector >()->default_value(std::vector(), ""), "input files.")
    but I don't know why
    – Michael Aug 10 '12 at 13:49

1 Answers1

13

I need to give a textual representation of the default value, see http://lists.boost.org/boost-users/2010/01/55054.php.

I.e. the following line works:

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

I guess this is needed for the help output, which could in my example

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

Since the compiler doesn't know how to overload the operator<<() for vector<string>, it complains.

Michael
  • 1,464
  • 1
  • 20
  • 40