Using boost::program

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.


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 >> .


A file with each line having one pair of values can be one option. The file could be a plain ascii text file or you can go for xml files too - refer to boost serialization.

链接地址: http://www.djcxy.com/p/49556.html

上一篇: 如果HTTP CODE的curl getinfo没有返回代码?

下一篇: 使用boost :: program