diff options
Diffstat (limited to 'apps/apps.cpp')
-rw-r--r-- | apps/apps.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/apps/apps.cpp b/apps/apps.cpp index 21c4fe6..2da8359 100644 --- a/apps/apps.cpp +++ b/apps/apps.cpp @@ -6,6 +6,7 @@ namespace trygvis { namespace apps { using namespace log4cplus; +using namespace std; namespace po = boost::program_options; const po::options_description logging_options() { @@ -19,5 +20,48 @@ void setup_logging(po::variables_map vm) { config.configure(); } +int launch_app(int argc, char *argv[], app& app) { + po::options_description desc("Options"); + + auto x = desc.add_options(); + app.add_options(x); + + desc.add(logging_options()); + + po::variables_map vm; + auto parsed = po::parse_command_line(argc, argv, desc); + po::store(parsed, vm); + + app_execution execution(desc, vm); + + try { + po::notify(vm); + } catch (boost::program_options::required_option &e) { + cerr << "Missing required option: " << e.get_option_name() << endl; + execution.usage(); + return EXIT_FAILURE; + } + + auto unrecognized = po::collect_unrecognized(parsed.options, po::include_positional); + + if (vm.count("help")) { + cerr << desc << "\n"; + return EXIT_FAILURE; + } + + if (unrecognized.size()) { + cerr << "Unrecognized option: " << unrecognized.at(0) << "\n"; + return EXIT_FAILURE; + } + + setup_logging(vm); + + return app.main(execution); +} + +void app_execution::usage() { + cerr << desc << endl; +} + } } |