Does anyone use boost program_options? What I'm trying to do is this:

--Files 1.jpg 2.jpg 3.jpg

The only thing I know how to do is

--File1 1.jpg --File2 2.jpg --File3 3.jpg

Does anyone know how to do this, and maybe store them in a vector<string>?

Thanks,

Dave

Recommended Answers

All 7 Replies

If those are the only command-line options why do you need Boost? That's like swatting a fly with a sludge hammer.

I have a lot of other arguments too, I just took out this one part.

In any case, the solution is:

("FileList", po::value<vector<string> >()->multitoken(), "List of files.")

Ok I have a better one for you:

I want to have multiple lists of files, like this:

--NumLists 3 --List1 a.txt b.txt c.txt --List2 d.txt e.txt --List3 f.txt g.txt

How would you handle that? (The number of lists is not known until the NumLists param is given)

Thanks,
Dave

a 2d vector vector< vector<string> > lists;

Sure, but how do you input them? Ie- how do you tell boost to start filling lists[1] instead of lists[0]?

I tried this:

#include <iostream>
#include <vector>
#include <cstdlib>

#include <cmath>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

using namespace std;

int main(int argc, char* argv[])
{
	vector<vector<string> > Lists(2);
			
	po::options_description desc("Allowed options");
	desc.add_options()
			("help", "produce help message")
			("List0", po::value<vector<string> >(&Lists[0])->multitoken(), "List0.")
			("List1", po::value<vector<string> >(&Lists[1])->multitoken(), "List1.")
			;

	po::variables_map vm;
	po::store(po::parse_command_line(argc, argv, desc), vm);
	po::notify(vm); //assign the variables (if they were specified)

	if (vm.count("help"))
	{
		cout << desc << "\n";
		return 1;
	}

	for(unsigned int list = 0; list < Lists.size(); list++)
	{
		cout << "List: " << list << endl << "-----------" << endl;
		for(unsigned int item = 0; item < Lists[list].size(); item++)
		{
			cout << "List: " << list << " Item: " << item << " " << Lists[list][item] << endl;
		}
	}
	
	return 0;
}

but the output is:

List: 0
-----------
List: 0 Item: 0 1scan0.txt
List: 0 Item: 1 2scan0.txt
List: 0 Item: 2 --List1
List: 0 Item: 3 1scan1.txt
List: 0 Item: 4 2scan1.txt
List: 1
-----------

ie. it thinks --List1 is another option of --List0 !!

Just for the record for people stumbling upon this: The issue of the last post has been fixed in some more recent version of Boost.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.