Boost.Program_options fixed number of tokens

Posted by kloffy on Stack Overflow See other posts from Stack Overflow or by kloffy
Published on 2010-03-05T19:49:07Z Indexed on 2010/05/06 20:08 UTC
Read the original article Hit count: 590

Filed under:
|

Boost.Program_options provides a facility to pass multiple tokens via command line arguments as follows:

std::vector<int> nums;    

po::options_description desc("Allowed options");
desc.add_options()
    ("help", "Produce help message.")
    ("nums", po::value< std::vector<int> >(&nums)->multitoken(), "Numbers.")
;

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);

However, what is the preferred way of accepting only a fixed number of arguments? The only solution I could come is to manually assign values:

int nums[2];    

po::options_description desc("Allowed options");
desc.add_options()
    ("help", "Produce help message.")
    ("nums", "Numbers.")
;

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);

if (vm.count("nums")) {
   // Assign nums
}

This feels a bit clumsy. Is there a better solution?

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost-program-options