I'm currently working with boost::program_options
. My program is supposed to take as arguments (amongst other things...) an arbitrary number of 'lists' of arbitrary length. For example, the user should be able to call
./myprogram -list item1 item2 item3 -list item1 item2 -list item1 item2
Obviously, I don't want to get one list/vector with all the items one after the other as a result, but (in this case) three lists/vectors (or, for example, one vector of vectors containing the elements) with two or three items per list (each item is supposed to be a string, but I guess this doesn't matter).
As I said before, the number of lists (as well as the number of items per list!) should be arbitrary.
How can I do that with boost::program_options
?
This can be done without a whole lot of extra code. The secret is to separate the parsing step from the storage step, as also done in this answer.
The parser will return a container of key/value structs as the options are presented from the user. If an option is submitted multiple times then the container will have a separate entry for each option submission. It is quite straightforward to scan for a particular option and organize its values however we want.
Here's an example that prints out each input multi-token option on a separate line:
And here's a sample invocation (live at coliru):