CPP/Boost/ProgramOptions/MultipleArguments
From ProgrammingExamples
MultipleArguments.cpp
#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; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) Project(MultipleArguments) set(Boost_USE_MULTITHREADED ON) # which is the default FIND_PACKAGE(Boost 1.38 COMPONENTS program_options required) INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS}) LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS}) ADD_EXECUTABLE(MultipleArguments MultipleArguments.cpp) target_link_libraries(MultipleArguments boost_program_options-mt)