سؤال

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top