1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <boost/program_options.hpp>
#include <boost/ref.hpp>
#include <stdexcept>
#include "ParseConfig.h"
/** Parse the command-line parameters using Boost program_options. The
* command-line options are parsed and the configuration options are
* set in the parseConfig parameter.
*
* @return true if the program should exit, such as when help or version
* information has been displayed, or false if the program should
* continue running.
*/
bool parse(int argc, char* argv[], ParseConfig& parseConfig)
{
namespace po = boost::program_options;
// Declare the supported options.
po::options_description desc(
"Program options");
desc.add_options()
("help,h", "Print this help message and exit.")
("version,v", "Print the application version and exit.")
("input,i", po::value<std::string>(&(parseConfig.input)),
"Input file (required).")
("output,o", po::value<std::string>(&(parseConfig.output)),
"Output file (default is STDOUT).");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help"))
{
std::cout << "Run program" << std::endl << desc << std::endl;
return true;
}
if (vm.count("version"))
{
std::cout << argv[0] << ": " << EXEC_VERSION << std::endl;
return true;
}
return false;
}
int main(int argc, char *argv[])
{
ParseConfig parseConfig;
int result = 0;
try
{
/** Parse the command-line to configure input and output files
* or display program version or help menu.
*/
if (parse(argc, argv, parseConfig))
{
return result;
}
result = 0;
}
catch (std::exception& ex)
{
std::cerr << std::endl << ex.what() << std::endl;
result = 1;
}
catch (...)
{
std::cerr << std::endl << " unknown error ***" << std::endl;
result = 1;
}
return result;
}
|