patrick
/
plasp
Archived
1
0
Fork 0

Made option group parsing more uniform.

This commit is contained in:
Patrick Lühne 2017-10-13 17:12:33 +02:00
parent 3fe2886925
commit 63c4da8fad
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
12 changed files with 251 additions and 172 deletions

View File

@ -7,7 +7,7 @@ option(PLASP_BUILD_STATIC "Build static binaries" OFF)
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wpedantic -Werror ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "-g ${CMAKE_CXX_FLAGS_DEBUG}")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

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

View File

@ -1,36 +0,0 @@
#include <plasp-app/Commands.h>
#include <map>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Commands
//
////////////////////////////////////////////////////////////////////////////////////////////////////
static const std::map<std::string, Command> commandNames =
{
{"help", Command::Help},
{"-h", Command::Help},
{"--help", Command::Help},
{"version", Command::Version},
{"-v", Command::Version},
{"--version", Command::Version},
{"check-syntax", Command::CheckSyntax},
{"requirements", Command::Requirements},
{"pretty-print", Command::PrettyPrint},
{"normalize", Command::Normalize},
{"translate", Command::Translate},
};
////////////////////////////////////////////////////////////////////////////////////////////////////
Command parseCommand(const std::string &commandString)
{
const auto matchingCommand = commandNames.find(commandString);
if (matchingCommand == commandNames.cend())
throw std::runtime_error(std::string("") + commandString + "” is not a plasp command");
return matchingCommand->second;
}

View File

@ -1,22 +1,31 @@
#include <plasp-app/CommonOptions.h>
#include <plasp-app/OptionGroups.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Common Options
// Option Groups
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void addBasicOptions(cxxopts::Options &options)
void OptionGroupBasic::addTo(cxxopts::Options &options)
{
options.add_options("basic")
("h,help", "Display this help message")
("v,version", "Display version information")
("warnings-as-errors", "Treat warnings as errors");
("h,help", "Display this help message")
("v,version", "Display version information")
("warnings-as-errors", "Treat warnings as errors");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void addOutputOptions(cxxopts::Options &options)
void OptionGroupBasic::parse(cxxopts::Options &options)
{
help = options["help"].as<bool>();
version = options["version"].as<bool>();
warningsAsErrors = options["warnings-as-errors"].as<bool>();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void OptionGroupOutput::addTo(cxxopts::Options &options)
{
options.add_options("output")
("color", "Colorize output (always, never, auto)", cxxopts::value<std::string>()->default_value("auto"))
@ -25,7 +34,34 @@ void addOutputOptions(cxxopts::Options &options)
////////////////////////////////////////////////////////////////////////////////////////////////////
void addParserOptions(cxxopts::Options &options)
void OptionGroupOutput::parse(cxxopts::Options &options)
{
const auto colorPolicyString = options["color"].as<std::string>();
if (colorPolicyString == "auto")
colorPolicy = colorlog::ColorStream::ColorPolicy::Auto;
else if (colorPolicyString == "never")
colorPolicy = colorlog::ColorStream::ColorPolicy::Never;
else if (colorPolicyString == "always")
colorPolicy = colorlog::ColorStream::ColorPolicy::Always;
else
throw OptionException("unknown color policy “" + colorPolicyString + "");
const auto logPriorityString = options["log-priority"].as<std::string>();
try
{
logPriority = colorlog::priorityFromName(logPriorityString.c_str());
}
catch (const std::exception &e)
{
throw OptionException(e.what());
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void OptionGroupParser::addTo(cxxopts::Options &options)
{
options.add_options("parser")
("i,input", "Input files (in PDDL or SAS format)", cxxopts::value<std::vector<std::string>>())
@ -36,69 +72,21 @@ void addParserOptions(cxxopts::Options &options)
////////////////////////////////////////////////////////////////////////////////////////////////////
BasicOptions parseBasicOptions(cxxopts::Options &options)
void OptionGroupParser::parse(cxxopts::Options &options)
{
BasicOptions basicOptions;
basicOptions.help = options["help"].as<bool>();
basicOptions.version = options["version"].as<bool>();
basicOptions.warningsAsErrors = options["warnings-as-errors"].as<bool>();
return basicOptions;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
OutputOptions parseOutputOptions(cxxopts::Options &options)
{
OutputOptions outputOptions;
const auto colorPolicy = options["color"].as<std::string>();
if (colorPolicy == "auto")
outputOptions.colorPolicy = colorlog::ColorStream::ColorPolicy::Auto;
else if (colorPolicy == "never")
outputOptions.colorPolicy = colorlog::ColorStream::ColorPolicy::Never;
else if (colorPolicy == "always")
outputOptions.colorPolicy = colorlog::ColorStream::ColorPolicy::Always;
else
throw OptionException("unknown color policy “" + colorPolicy + "");
const auto logPriorityString = options["log-priority"].as<std::string>();
try
{
outputOptions.logPriority = colorlog::priorityFromName(logPriorityString.c_str());
}
catch (const std::exception &e)
{
throw OptionException(e.what());
}
return outputOptions;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ParserOptions parseParserOptions(cxxopts::Options &options)
{
ParserOptions parserOptions;
const auto parsingModeString = options["parsing-mode"].as<std::string>();
if (parsingModeString == "compatibility")
parserOptions.parsingMode = pddl::Mode::Compatibility;
parsingMode = pddl::Mode::Compatibility;
else if (parsingModeString != "strict")
throw OptionException("unknown parsing mode “" + parsingModeString + "");
if (options.count("input"))
parserOptions.inputFiles = options["input"].as<std::vector<std::string>>();
inputFiles = options["input"].as<std::vector<std::string>>();
const auto languageName = options["language"].as<std::string>();
parserOptions.language = plasp::Language::fromString(languageName);
language = plasp::Language::fromString(languageName);
if (parserOptions.language == plasp::Language::Type::Unknown)
if (language == plasp::Language::Type::Unknown)
throw OptionException("unknown input language “" + languageName + "");
return parserOptions;
}

View File

@ -1,4 +1,5 @@
#include <plasp-app/commands/Translate.h>
#include <plasp-app/commands/CommandTranslate.h>
#include <iostream>
#include <string>
@ -23,18 +24,19 @@
#include <plasp/sas/Description.h>
#include <plasp/sas/TranslatorASP.h>
#include <plasp-app/Commands.h>
#include <plasp-app/CommonOptions.h>
#include <plasp-app/Version.h>
#include <plasp-app/commands/Translate.h>
int translate(int argc, char **argv)
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Command Translate
//
////////////////////////////////////////////////////////////////////////////////////////////////////
int CommandTranslate::run(int argc, char **argv)
{
cxxopts::Options options("plasp translate", "Translate PDDL to ASP.");
addBasicOptions(options);
addOutputOptions(options);
addParserOptions(options);
addOptionGroupsTo(options);
const auto printHelp =
[&]()
@ -44,9 +46,11 @@ int translate(int argc, char **argv)
options.parse(argc, argv);
const auto basicOptions = parseBasicOptions(options);
const auto outputOptions = parseOutputOptions(options);
const auto parserOptions = parseParserOptions(options);
parseOptionGroups(options);
const auto &basicOptions = std::get<OptionGroupBasic>(m_optionGroups);
const auto &outputOptions = std::get<OptionGroupOutput>(m_optionGroups);
const auto &parserOptions = std::get<OptionGroupParser>(m_optionGroups);
if (basicOptions.help)
{

View File

@ -7,9 +7,58 @@
#include <colorlog/Logger.h>
#include <colorlog/Priority.h>
#include <plasp-app/Commands.h>
#include <plasp-app/Command.h>
#include <plasp-app/Version.h>
#include <plasp-app/commands/Translate.h>
#include <plasp-app/commands/CommandTranslate.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main
//
////////////////////////////////////////////////////////////////////////////////////////////////////
enum class CommandType
{
Help,
Version,
CheckSyntax,
Requirements,
PrettyPrint,
Normalize,
Translate
};
////////////////////////////////////////////////////////////////////////////////////////////////////
static const std::map<std::string, CommandType> commandNames =
{
{"help", CommandType::Help},
{"-h", CommandType::Help},
{"--help", CommandType::Help},
{"version", CommandType::Version},
{"-v", CommandType::Version},
{"--version", CommandType::Version},
{"check-syntax", CommandType::CheckSyntax},
{"requirements", CommandType::Requirements},
{"pretty-print", CommandType::PrettyPrint},
{"normalize", CommandType::Normalize},
{"translate", CommandType::Translate},
};
////////////////////////////////////////////////////////////////////////////////////////////////////
const auto parseCommandType =
[](const std::string &commandString)
{
const auto matchingCommand = commandNames.find(commandString);
if (matchingCommand == commandNames.cend())
throw std::runtime_error(std::string("") + commandString + "” is not a plasp command");
return matchingCommand->second;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
@ -19,8 +68,7 @@ int main(int argc, char **argv)
// TODO: add list of available commands
std::cout
<< "ASP planning tools for PDDL." << std::endl
<< "Usage: plasp <command> [<arguments>]" << std::endl
<< "Translate PDDL to ASP." << std::endl;
<< "Usage: plasp <command> [<arguments>]" << std::endl;
};
const auto printVersion =
@ -39,16 +87,16 @@ int main(int argc, char **argv)
try
{
switch (parseCommand(argv[1]))
switch (parseCommandType(argv[1]))
{
case Command::Help:
case CommandType::Help:
printHelp();
return EXIT_SUCCESS;
case Command::Version:
case CommandType::Version:
printVersion();
return EXIT_SUCCESS;
case Command::Translate:
return translate(argc - 1, &argv[1]);
case CommandType::Translate:
return CommandTranslate().run(argc - 1, &argv[1]);
default:
exit(EXIT_FAILURE);
}

View File

@ -1,6 +1,6 @@
# Building
`plasp` requires a C++14 compiler (preferrably GCC ≥ 6.1 or clang ≥ 3.8), the `boost` libraries (≥ 1.55), and CMake for building.
`plasp` requires a C++17 compiler (preferrably GCC ≥ 6.1 or clang ≥ 3.8), the `boost` libraries (≥ 1.55), and CMake for building.
```bash
$ git clone https://github.com/potassco/plasp.git