Refactored command base class for simplicity.

This commit is contained in:
Patrick Lühne 2017-10-13 18:14:24 +02:00
parent 5427876e36
commit 2be3f4256a
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
3 changed files with 22 additions and 13 deletions

View File

@ -17,25 +17,28 @@ template<class... OptionGroups>
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<std::decay_t<decltype(m_optionGroups)>>();
@ -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<OptionGroups...> m_optionGroups;
};

View File

@ -13,6 +13,8 @@
class CommandTranslate : public Command<OptionGroupBasic, OptionGroupOutput, OptionGroupParser>
{
public:
CommandTranslate();
int run(int argc, char **argv);
};

View File

@ -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<OptionGroupBasic>(m_optionGroups);
const auto &outputOptions = std::get<OptionGroupOutput>(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;
}