[program_options] How to allow multiple options?

Hi, I have spent several hours looking at the docs and sources, but I cannot figure out how to allow multiple occurances of an option. Many unix tools allow you to specify the --verbose option multiple times, to mean increasing verbosity of output, and I want to replicate this behaviour. Does this require writing a new class derived from value_semantic, that combines the behaviour of bool_switch() and typed_value<std::vector<bool>> ? I'm not sure how to go about doing that. It is a very useful lib, but I do wish (often!) there was some real reference docs (aside from the doxygen stuff and comments in the sources themselves, which isn't enough :-( Unrelated, but in trying to figure this out from the docs, I noticed in the overview.html, in the Syntactic Information there is a call to a function value<string>()->implicit(). However typed_value<T> has no such member function. Maybe it is left over from a previous design iteration or something, as there is a member variable m_implicit but it is never initialized or accessed. Cheers, Ian McCulloch

Hi, Multiple occurrences are definitely allowed and handled. If you do specify the option value to populate a container, and then pass the containerpointer to the add_options() method, you can later iterate over the options. For e.g, in class cmdLineParser{ ... private: vector<string>* _sources; } In cmdLineParser.cpp, parse(int argc, char* argv[]) { po::options_description genericDesc("Generic"); genericDesc.add_options() ("help", "Help Message") ("source,s", value(_sources), "source node name") ; store(parse_command_line(argc,argv, genericDesc),_vm); .... } good luck, -Harsh Deshmane On Wednesday 16 August 2006 17:03, Ian McCulloch wrote:
Hi,
I have spent several hours looking at the docs and sources, but I cannot figure out how to allow multiple occurances of an option. Many unix tools allow you to specify the --verbose option multiple times, to mean increasing verbosity of output, and I want to replicate this behaviour.
Does this require writing a new class derived from value_semantic, that combines the behaviour of bool_switch() and typed_value<std::vector<bool>> ? I'm not sure how to go about doing that.
It is a very useful lib, but I do wish (often!) there was some real reference docs (aside from the doxygen stuff and comments in the sources themselves, which isn't enough :-(
Unrelated, but in trying to figure this out from the docs, I noticed in the overview.html, in the Syntactic Information there is a call to a function value<string>()->implicit(). However typed_value<T> has no such member function. Maybe it is left over from a previous design iteration or something, as there is a member variable m_implicit but it is never initialized or accessed.
Cheers, Ian McCulloch
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Harsh Deshmane wrote:
Hi,
Multiple occurrences are definitely allowed and handled.
If you do specify the option value to populate a container, and then pass the containerpointer to the add_options() method, you can later iterate over the options.
[...] Sorry, I should have been clearer. I know how to do multiple occurances of options that require a value, what I want though is multiple occurances of a switch. A single switch can be done with something like desc.add_options() ("verbose,v", bool_switch(), "increase verbosity"); but this fails with a multiple_occurrences exception if more than one -v option is supplied. Multiple boolean options can be done with something like desc.add_options() ("verbose,v", value<std::vector<bool> >(), "increase verbosity"); but this requires that each option is given a value, such as $ myprogram -v true -v true -v true which is not what I want. I want instead to be able to use a command line like $ myprogram -v -v -v Cheers, Ian
participants (2)
-
Harsh Deshmane
-
Ian McCulloch