Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using boost::program_options

In my program I have a list of pairs - name and size.

I want to build this list from the command line interface using boost::program_options.

It should look something like this:

myProg --value("John",10) --value("Steve",14) --value("Marge",28)

I also need this to be in order - Steve will be after John and before Marge on the list. Is that possible with boost::program_options?

This CLI syntax is just an idea to get the list. If you have a better one, do tell.

like image 534
n00b programmer Avatar asked Dec 23 '10 12:12

n00b programmer


1 Answers

You just define your option

("value", value<vector<YourPairType>>()->composing(), "description")

and an appropriate

istream& operator >> (istream& in, YourPairType& pr) { /* ... */ }

that reads a single YourPairType from in in your ("John",10) format. Parsed options will be stored in the order they appear in the command line.

You can achieve greater flexibility if you use custom validators instead of operator >>.

like image 170
Yakov Galka Avatar answered Nov 11 '22 21:11

Yakov Galka