From 2be3f4256adea5860bac290c0895d194ac5c0040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 13 Oct 2017 18:14:24 +0200 Subject: [PATCH] Refactored command base class for simplicity. --- app/include/plasp-app/Command.h | 16 ++++++++++------ .../plasp-app/commands/CommandTranslate.h | 2 ++ app/src/plasp-app/commands/CommandTranslate.cpp | 17 ++++++++++------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/include/plasp-app/Command.h b/app/include/plasp-app/Command.h index a9b02b1..57de926 100644 --- a/app/include/plasp-app/Command.h +++ b/app/include/plasp-app/Command.h @@ -17,25 +17,28 @@ template class Command { protected: - void addOptionGroupsTo(cxxopts::Options &options) + Command(cxxopts::Options &&options) + : m_options{options} { forEach(m_optionGroups, [&](auto &optionGroup) { - optionGroup.addTo(options); + optionGroup.addTo(m_options); }); } - void parseOptionGroups(cxxopts::Options &options) + void parseOptions(int argc, char **argv) { + m_options.parse(argc, argv); + forEach(m_optionGroups, [&](auto &optionGroup) { - optionGroup.parse(options); + optionGroup.parse(m_options); }); } - void printHelp(cxxopts::Options &options) + void printHelp() { const auto numberOfOptionGroups = std::tuple_size>(); @@ -49,9 +52,10 @@ class Command optionGroupNames.emplace_back(optionGroup.Name); }); - std::cout << options.help(optionGroupNames) << std::endl; + std::cout << m_options.help(optionGroupNames) << std::endl; } + cxxopts::Options m_options; std::tuple m_optionGroups; }; diff --git a/app/include/plasp-app/commands/CommandTranslate.h b/app/include/plasp-app/commands/CommandTranslate.h index b791785..a4066e3 100644 --- a/app/include/plasp-app/commands/CommandTranslate.h +++ b/app/include/plasp-app/commands/CommandTranslate.h @@ -13,6 +13,8 @@ class CommandTranslate : public Command { public: + CommandTranslate(); + int run(int argc, char **argv); }; diff --git a/app/src/plasp-app/commands/CommandTranslate.cpp b/app/src/plasp-app/commands/CommandTranslate.cpp index e631fed..ec3b1c7 100644 --- a/app/src/plasp-app/commands/CommandTranslate.cpp +++ b/app/src/plasp-app/commands/CommandTranslate.cpp @@ -32,13 +32,16 @@ // //////////////////////////////////////////////////////////////////////////////////////////////////// +CommandTranslate::CommandTranslate() +: Command(cxxopts::Options("plasp translate", "Translate PDDL to ASP.")) +{ +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + int CommandTranslate::run(int argc, char **argv) { - cxxopts::Options options("plasp translate", "Translate PDDL to ASP."); - - addOptionGroupsTo(options); - options.parse(argc, argv); - parseOptionGroups(options); + parseOptions(argc, argv); const auto &basicOptions = std::get(m_optionGroups); const auto &outputOptions = std::get(m_optionGroups); @@ -46,7 +49,7 @@ int CommandTranslate::run(int argc, char **argv) if (basicOptions.help) { - printHelp(options); + printHelp(); return EXIT_SUCCESS; } @@ -102,7 +105,7 @@ int CommandTranslate::run(int argc, char **argv) { logger.log(colorlog::Priority::Error, "unknown input language"); std::cout << std::endl; - printHelp(options); + printHelp(); return EXIT_FAILURE; }