#include <boost/program_options.hpp>
#include <string>
#include <vector>
#include <iostream>
namespace po = boost::program_options;
using namespace std;
typedef vector<string> Strings;
// 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>(cout, " "));
return os;
}
int main(int argc, char *argv[]) {
po::options_description cmdline_desc("Usage: %s [Options] [args]\nOptions");
cmdline_desc.add_options()
("str", po::value< string >(), "a string arg")
("strs", po::value< Strings >(), "a list of strings")
("foo", po::value< string >(), "a string arg")
("bar", po::value< Strings >(), "a list of strings")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, cmdline_desc), vm);
po::notify(vm);
if (vm.count("str"))
cout << "Got str arg=" << vm["str"].as< string >() << "\n";
if (vm.count("strs"))
cout << "Got strs arg=" << vm["strs"].as< Strings >() << "\n";
if (vm.count("foo"))
cout << "Got foo arg=" << vm["foo"].as< string >() << "\n";
if (vm.count("bar"))
cout << "Got bar arg=" << vm["bar"].as< Strings >() << "\n";
}
./po_test --str "a string" --strs "a, vector, of, strings" --foo "a foo" --bar "a, bar"
Got strs arg=a string a, vector, of, strings
Got foo arg=a foo
Got bar arg=a, bar
It appears the parser is getting confused between the "str" and the "strs". It doesn't seem to be related to ambiguity in handling vectors since the "foo" and "bar" arguments are fine. I see this problem on 32-bit CentOS and Ubuntu machines, but not on 64-bit. I am running Boost 1.43 and from the /usr/local/include/boost/program_options/version.hpp: