Made option group parsing more uniform.

This commit is contained in:
2017-10-13 17:12:33 +02:00
parent 3fe2886925
commit 63c4da8fad
12 changed files with 251 additions and 172 deletions

View File

@@ -0,0 +1,43 @@
#ifndef __PLASP_APP__COMMAND_H
#define __PLASP_APP__COMMAND_H
#include <tuple>
#include <cxxopts.hpp>
#include <plasp-app/Utils.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Command
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class... OptionGroups>
class Command
{
protected:
void addOptionGroupsTo(cxxopts::Options &options)
{
forEach(m_optionGroups,
[&](auto &optionGroup)
{
optionGroup.addTo(options);
});
}
void parseOptionGroups(cxxopts::Options &options)
{
forEach(m_optionGroups,
[&](auto &optionGroup)
{
optionGroup.parse(options);
});
}
std::tuple<OptionGroups...> m_optionGroups;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
#endif

View File

@@ -1,29 +0,0 @@
#ifndef __PLASP_APP__COMMANDS_H
#define __PLASP_APP__COMMANDS_H
#include <string>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Commands
//
////////////////////////////////////////////////////////////////////////////////////////////////////
enum class Command
{
Help,
Version,
CheckSyntax,
Requirements,
PrettyPrint,
Normalize,
Translate
};
////////////////////////////////////////////////////////////////////////////////////////////////////
Command parseCommand(const std::string &commandString);
////////////////////////////////////////////////////////////////////////////////////////////////////
#endif

View File

@@ -1,5 +1,5 @@
#ifndef __PLASP_APP__COMMON_OPTIONS_H
#define __PLASP_APP__COMMON_OPTIONS_H
#ifndef __PLASP_APP__OPTION_GROUPS_H
#define __PLASP_APP__OPTION_GROUPS_H
#include <cxxopts.hpp>
@@ -13,7 +13,7 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Common Options
// Option Groups
//
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -25,14 +25,12 @@ class OptionException : public pddl::Exception
////////////////////////////////////////////////////////////////////////////////////////////////////
void addBasicOptions(cxxopts::Options &options);
void addOutputOptions(cxxopts::Options &options);
void addParserOptions(cxxopts::Options &options);
////////////////////////////////////////////////////////////////////////////////////////////////////
struct BasicOptions
struct OptionGroupBasic
{
void addTo(cxxopts::Options &options);
void parse(cxxopts::Options &options);
void printHelp(std::ostream &stream);
bool help = false;
bool version = false;
bool warningsAsErrors = false;
@@ -40,16 +38,24 @@ struct BasicOptions
////////////////////////////////////////////////////////////////////////////////////////////////////
struct OutputOptions
struct OptionGroupOutput
{
void addTo(cxxopts::Options &options);
void parse(cxxopts::Options &options);
void printHelp(std::ostream &stream);
colorlog::ColorStream::ColorPolicy colorPolicy = colorlog::ColorStream::ColorPolicy::Auto;
colorlog::Priority logPriority = colorlog::Priority::Info;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct ParserOptions
struct OptionGroupParser
{
void addTo(cxxopts::Options &options);
void parse(cxxopts::Options &options);
void printHelp(std::ostream &stream);
std::vector<std::string> inputFiles;
pddl::Mode parsingMode = pddl::Mode::Strict;
plasp::Language::Type language = plasp::Language::Type::Automatic;
@@ -57,10 +63,4 @@ struct ParserOptions
////////////////////////////////////////////////////////////////////////////////////////////////////
BasicOptions parseBasicOptions(cxxopts::Options &options);
OutputOptions parseOutputOptions(cxxopts::Options &options);
ParserOptions parseParserOptions(cxxopts::Options &options);
////////////////////////////////////////////////////////////////////////////////////////////////////
#endif

View File

@@ -0,0 +1,46 @@
#ifndef __PLASP_APP__UTILS_H
#define __PLASP_APP__UTILS_H
#include <cxxopts.hpp>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Command
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template <std::size_t... Index>
auto makeIndexDispatcher(std::index_sequence<Index...>)
{
return
[](auto &&f)
{
(f(std::integral_constant<std::size_t, Index>{}), ...);
};
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template <std::size_t N>
auto makeIndexDispatcher()
{
return makeIndexDispatcher(std::make_index_sequence<N>{});
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename Tuple, typename Functor>
void forEach(Tuple &&tuple, Functor &&functor)
{
constexpr auto n = std::tuple_size<std::decay_t<Tuple>>::value;
auto dispatcher = makeIndexDispatcher<n>();
dispatcher(
[&functor, &tuple](auto index)
{
functor(std::get<index>(std::forward<Tuple>(tuple)));
});
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#endif

View File

@@ -0,0 +1,21 @@
#ifndef __PLASP_APP__COMMANDS__TRANSLATE_H
#define __PLASP_APP__COMMANDS__TRANSLATE_H
#include <plasp-app/Command.h>
#include <plasp-app/OptionGroups.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Command Translate
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class CommandTranslate : public Command<OptionGroupBasic, OptionGroupOutput, OptionGroupParser>
{
public:
int run(int argc, char **argv);
};
////////////////////////////////////////////////////////////////////////////////////////////////////
#endif

View File

@@ -1,6 +0,0 @@
#ifndef __PLASP_APP__COMMANDS__TRANSLATE_H
#define __PLASP_APP__COMMANDS__TRANSLATE_H
int translate(int argc, char **argv);
#endif