Boost::program options

From Yefu's notes
Jump to: navigation, search
// This is a demo on how to use boost::program_options
//

#include "stdafx.h"
#include <boost/program_options.hpp>
#include <string>
#include <iostream>

using namespace std;

boost::program_options::options_description BuildCommandLineOptions()
{
	boost::program_options::options_description desc("Allowed options");

	desc.add_options()
		("help,h", "Print this help message.")
		("input,i", boost::program_options::value<string>()->required(), "Input file")
		("output,o", boost::program_options::value<string>()->required(), "Output file")
		;

	return desc;
}

boost::program_options::variables_map ParseCommandlineOptions(int argc, _TCHAR* argv[])
{
	auto options = BuildCommandLineOptions();
	boost::program_options::variables_map vm;
	store(parse_command_line(argc, argv, options), vm);
	boost::program_options::notify(vm);
	return vm;
}


int _tmain(int argc, _TCHAR* argv[])
{
	auto options = ParseCommandlineOptions(argc, argv);
	if (options.count("help"))
	{
		cout << BuildCommandLineOptions() << endl;
		return 0;
	}
	string input = options["input"].as<string>();
	return 0;
}