[program_options] vector parameters not working.

hello, I recently picked up boost to make use of the various libraries it includes. Unfortunately, I got stuck with the very first one (program_options) I used in my code. Any help would be appreciated very much. I want an option "bunch" which goes along with a bunch of numbers. I thought the following code would do the job: --------------------------------------------------------------------------------------- #include <iostream> #include <string> #include <vector> #include <boost/program_options.hpp> using namespace std; namespace po = boost::program_options; int main(int argc, char* argv[]) { try { vector<int> numbers; po::options_description params("parameters"); po::variables_map options; params.add_options() ("bunch,b", po::value< vector<int> >(&numbers), "a bunch of numbers"); po::store(po::parse_command_line(argc, argv, params), options); po::notify(options); if(options.count("bunch")!=0) { copy (numbers.begin(), numbers.end(), std::ostream_iterator<int>(cout, "\n")); } } catch(exception & e) { cerr << e.what() << endl; } return 0; }; --------------------------------------------------------------------------------------- However, it doesn't seem to work as it should. When I run this as follows: ---------------------------------------------------------------------------------------
test --bunch 4 2 42 4
It is just printing the first value - Am I doing something wrong here? I'm using 'boost_1_40_0' which I compiled using MS Visual Studio 2005. thanks,

On Mon, Sep 21, 2009 at 10:04 AM, aiooua <aiooua@gmail.com> wrote:
hello,
I recently picked up boost to make use of the various libraries it includes. Unfortunately, I got stuck with the very first one (program_options) I used in my code. Any help would be appreciated very much.
I want an option "bunch" which goes along with a bunch of numbers. I thought the following code would do the job:
--------------------------------------------------------------------------------------- #include <iostream> #include <string> #include <vector> #include <boost/program_options.hpp>
using namespace std; namespace po = boost::program_options;
int main(int argc, char* argv[]) { try { vector<int> numbers; po::options_description params("parameters"); po::variables_map options; params.add_options() ("bunch,b", po::value< vector<int> >(&numbers), "a bunch of numbers"); po::store(po::parse_command_line(argc, argv, params), options); po::notify(options); if(options.count("bunch")!=0) { copy (numbers.begin(), numbers.end(), std::ostream_iterator<int>(cout, "\n")); } } catch(exception & e) { cerr << e.what() << endl; } return 0; };
---------------------------------------------------------------------------------------
However, it doesn't seem to work as it should. When I run this as follows:
---------------------------------------------------------------------------------------
test --bunch 4 2 42 4
---------------------------------------------------------------------------------------
It is just printing the first value - Am I doing something wrong here? I'm using 'boost_1_40_0' which I compiled using MS Visual Studio 2005.
thanks, _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
You have to use the multitoken() function - here is a short example (which should DEFINITELY be included on the documentation page). Unfortunately, the multitoken list has to appear at the end of the argument list (i.e. you cannot have "--param1 something --NumberList 1 2 3 4 --param3 something_else", instead you must have "--param1 something --param3 something_else --NumberList 1 2 3 4") because the multitoken argument will just keep eating the arguments until it gets to the very end!). #include <iostream> #include <vector> #include <cstdlib> #include <cmath> #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc, char* argv[]) { po::options_description desc("Allowed options"); desc.add_options() ("help", "produce help message") ("NumberList,N", po::value<std::vector<int> >()->multitoken(), "List of numbers.")//lets you use --NumberList or -N ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); //assign the variables (if they were specified) if(vm.count("help")) { std::cout << desc << std::endl;; return 1; } if (vm.count("NumberList")) { std::cout << "count: " << vm.count("NumberList") << std::endl; std::vector<int> NumberList = vm["NumberList"].as<std::vector<int>
(); std::cout << "NumberList is length " << NumberList.size() << std::endl; for(unsigned int i = 0; i < NumberList.size(); i++) std::cout << NumberList[i] << std::endl; } else { std::cout << "NumberList was not set." << std::endl; }
return 0; } I hope this answers your question. Thanks, David
participants (2)
-
aiooua
-
David Doria