I'm trying to get the following syntax parsed with boost::program_options:
a)
$ a.out
verbosity: 0
b)
$ a.out -v
verbosity: 1
c)
$ a.out -v -v
verbosity: 2
d)
$ a.out -vv
verbosity: 2
e) (optional)
$ a.out -v3
verbosity: 3
My program so far:
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char *argv[])
{
po::options_description desc;
desc.add_options()
("verbose,v", po::value<int>(), "verbose");
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);
std::cout << "verbosity: " << vm["verbose"].as<int>() << std::endl;
return 0;
}
This works only for e). If I change it to:
po::value<int>()->default_value(0)
it works for a) and e). With
po::value<int>()->default_value(0)->implicit_value(1)
it works for a), b) and e).
How can I get it to parse all of the above cases?
I think I need some combination of a vector of values with zero_tokens(), but I can't seem to get it to work.