Added help and version options to the translate command.

This commit is contained in:
Patrick Lühne 2017-10-12 17:18:00 +02:00
parent 569b170aba
commit 86486207b8
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
7 changed files with 306 additions and 211 deletions

View File

@ -0,0 +1,29 @@
#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,7 +1,7 @@
#ifndef __PLASP_APP__COMMON_OPTIONS_H #ifndef __PLASP_APP__COMMON_OPTIONS_H
#define __PLASP_APP__COMMON_OPTIONS_H #define __PLASP_APP__COMMON_OPTIONS_H
#include <boost/program_options.hpp> #include <cxxopts.hpp>
#include <colorlog/ColorStream.h> #include <colorlog/ColorStream.h>
#include <colorlog/Priority.h> #include <colorlog/Priority.h>
@ -17,10 +17,6 @@
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
namespace po = boost::program_options;
////////////////////////////////////////////////////////////////////////////////////////////////////
class OptionException : public pddl::Exception class OptionException : public pddl::Exception
{ {
public: public:
@ -29,10 +25,9 @@ class OptionException : public pddl::Exception
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
po::options_description basicOptions(); void addBasicOptions(cxxopts::Options &options);
po::options_description outputOptions(); void addOutputOptions(cxxopts::Options &options);
po::options_description parserOptions(); void addParserOptions(cxxopts::Options &options);
po::positional_options_description parserPositionalOptions();
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -62,9 +57,9 @@ struct ParserOptions
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
BasicOptions parseBasicOptions(const po::variables_map &variablesMap); BasicOptions parseBasicOptions(cxxopts::Options &options);
OutputOptions parseOutputOptions(const po::variables_map &variablesMap); OutputOptions parseOutputOptions(cxxopts::Options &options);
ParserOptions parseParserOptions(const po::variables_map &variablesMap); ParserOptions parseParserOptions(cxxopts::Options &options);
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

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

View File

@ -0,0 +1,36 @@
#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

@ -6,72 +6,54 @@
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
po::options_description basicOptions() void addBasicOptions(cxxopts::Options &options)
{ {
po::options_description basicOptions("Basic options"); options.add_options("basic")
basicOptions.add_options() ("h,help", "Display this help message")
("help,h", po::bool_switch(), "Display this help message") ("v,version", "Display version information")
("version,v", po::bool_switch(), "Display version information") ("warnings-as-errors", "Treat warnings as errors");
("warnings-as-errors", po::bool_switch(), "Treat warnings as errors");
return basicOptions;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
po::options_description outputOptions() void addOutputOptions(cxxopts::Options &options)
{ {
po::options_description outputOptions("Output options"); options.add_options("output")
outputOptions.add_options() ("color", "Colorize output (always, never, auto)", cxxopts::value<std::string>()->default_value("auto"))
("color", po::value<std::string>()->default_value("auto"), "Colorize output (always, never, auto)") ("p,log-priority", "Log messages starting from this priority (debug, info, warning, error)", cxxopts::value<std::string>()->default_value("info"));
("log-priority,p", po::value<std::string>()->default_value("info"), "Log messages starting from this priority (debug, info, warning, error)");
return outputOptions;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
po::options_description parserOptions() void addParserOptions(cxxopts::Options &options)
{ {
po::options_description parserOptions("Parser options options"); options.add_options("parser")
parserOptions.add_options() ("i,input", "Input files (in PDDL or SAS format)", cxxopts::value<std::vector<std::string>>())
("input,i", po::value<std::vector<std::string>>(), "Input files (in PDDL or SAS format)") ("parsing-mode", "Parsing mode (strict, compatibility)", cxxopts::value<std::string>()->default_value("strict"))
("parsing-mode", po::value<std::string>()->default_value("strict"), "Parsing mode (strict, compatibility)") ("l,language", "Input language (pddl, sas, auto)", cxxopts::value<std::string>()->default_value("auto"));
("language,l", po::value<std::string>()->default_value("auto"), "Input language (pddl, sas, auto)"); options.parse_positional("input");
return parserOptions;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
po::positional_options_description parserPositionalOptions() BasicOptions parseBasicOptions(cxxopts::Options &options)
{
po::positional_options_description positionalOptions;
positionalOptions.add("input", -1);
return positionalOptions;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
BasicOptions parseBasicOptions(const po::variables_map &variablesMap)
{ {
BasicOptions basicOptions; BasicOptions basicOptions;
basicOptions.help = variablesMap["help"].as<bool>(); basicOptions.help = options["help"].as<bool>();
basicOptions.version = variablesMap["version"].as<bool>(); basicOptions.version = options["version"].as<bool>();
basicOptions.warningsAsErrors = variablesMap["warnings-as-errors"].as<bool>(); basicOptions.warningsAsErrors = options["warnings-as-errors"].as<bool>();
return basicOptions; return basicOptions;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
OutputOptions parseOutputOptions(const po::variables_map &variablesMap) OutputOptions parseOutputOptions(cxxopts::Options &options)
{ {
OutputOptions outputOptions; OutputOptions outputOptions;
const auto colorPolicy = variablesMap["color"].as<std::string>(); const auto colorPolicy = options["color"].as<std::string>();
if (colorPolicy == "auto") if (colorPolicy == "auto")
outputOptions.colorPolicy = colorlog::ColorStream::ColorPolicy::Auto; outputOptions.colorPolicy = colorlog::ColorStream::ColorPolicy::Auto;
@ -82,7 +64,7 @@ OutputOptions parseOutputOptions(const po::variables_map &variablesMap)
else else
throw OptionException("unknown color policy “" + colorPolicy + ""); throw OptionException("unknown color policy “" + colorPolicy + "");
const auto logPriorityString = variablesMap["log-priority"].as<std::string>(); const auto logPriorityString = options["log-priority"].as<std::string>();
try try
{ {
@ -98,21 +80,21 @@ OutputOptions parseOutputOptions(const po::variables_map &variablesMap)
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
ParserOptions parseParserOptions(const po::variables_map &variablesMap) ParserOptions parseParserOptions(cxxopts::Options &options)
{ {
ParserOptions parserOptions; ParserOptions parserOptions;
const auto parsingModeString = variablesMap["parsing-mode"].as<std::string>(); const auto parsingModeString = options["parsing-mode"].as<std::string>();
if (parsingModeString == "compatibility") if (parsingModeString == "compatibility")
parserOptions.parsingMode = pddl::Mode::Compatibility; parserOptions.parsingMode = pddl::Mode::Compatibility;
else if (parsingModeString != "strict") else if (parsingModeString != "strict")
throw OptionException("unknown parsing mode “" + parsingModeString + ""); throw OptionException("unknown parsing mode “" + parsingModeString + "");
if (variablesMap.count("input")) if (options.count("input"))
parserOptions.inputFiles = variablesMap["input"].as<std::vector<std::string>>(); parserOptions.inputFiles = options["input"].as<std::vector<std::string>>();
const auto languageName = variablesMap["language"].as<std::string>(); const auto languageName = options["language"].as<std::string>();
parserOptions.language = plasp::Language::fromString(languageName); parserOptions.language = plasp::Language::fromString(languageName);
if (parserOptions.language == plasp::Language::Type::Unknown) if (parserOptions.language == plasp::Language::Type::Unknown)

View File

@ -0,0 +1,166 @@
#include <plasp-app/commands/Translate.h>
#include <iostream>
#include <string>
#include <cxxopts.hpp>
#include <colorlog/ColorStream.h>
#include <colorlog/Logger.h>
#include <colorlog/Priority.h>
#include <pddl/AST.h>
#include <pddl/Exception.h>
#include <pddl/Mode.h>
#include <pddl/Normalize.h>
#include <pddl/NormalizedASTOutput.h>
#include <pddl/Parse.h>
#include <pddl/detail/normalization/Reduction.h>
#include <plasp/LanguageDetection.h>
#include <plasp/TranslatorException.h>
#include <plasp/pddl/TranslatorASP.h>
#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)
{
cxxopts::Options options("plasp translate", "Translate PDDL to ASP.");
addBasicOptions(options);
addOutputOptions(options);
addParserOptions(options);
const auto printHelp =
[&]()
{
std::cout << options.help({"", "basic", "output", "parser"});
};
options.parse(argc, argv);
const auto basicOptions = parseBasicOptions(options);
const auto outputOptions = parseOutputOptions(options);
const auto parserOptions = parseParserOptions(options);
if (basicOptions.help)
{
printHelp();
return EXIT_SUCCESS;
}
if (basicOptions.version)
{
std::cout << Version << std::endl;
return EXIT_SUCCESS;
}
colorlog::Logger logger;
logger.setColorPolicy(outputOptions.colorPolicy);
logger.setLogPriority(outputOptions.logPriority);
if (basicOptions.warningsAsErrors)
logger.setAbortPriority(colorlog::Priority::Warning);
const auto printCompatibilityInfo =
[&]()
{
if (parserOptions.parsingMode != pddl::Mode::Compatibility)
logger.log(colorlog::Priority::Info, "try using --parsing-mode=compatibility for extended legacy feature support");
};
try
{
tokenize::Tokenizer<tokenize::CaseInsensitiveTokenizerPolicy> tokenizer;
if (!parserOptions.inputFiles.empty())
std::for_each(parserOptions.inputFiles.cbegin(), parserOptions.inputFiles.cend(),
[&](const auto &inputFile)
{
tokenizer.read(inputFile);
});
else
{
logger.log(colorlog::Priority::Info, "reading from stdin");
tokenizer.read("std::cin", std::cin);
}
const auto detectLanguage =
[&]()
{
if (parserOptions.language == plasp::Language::Type::Automatic)
return plasp::detectLanguage(tokenizer);
return parserOptions.language;
};
const auto language = detectLanguage();
// TODO: get rid of unknown language type, use exception instead
if (language == plasp::Language::Type::Unknown)
{
logger.log(colorlog::Priority::Error, "unknown input language");
std::cout << std::endl;
printHelp();
return EXIT_FAILURE;
}
if (language == plasp::Language::Type::PDDL)
{
const auto logWarning =
[&](const auto &location, const auto &warning)
{
logger.log(colorlog::Priority::Warning, location, warning);
};
auto context = pddl::Context(std::move(tokenizer), logWarning);
context.mode = parserOptions.parsingMode;
auto description = pddl::parseDescription(context);
auto normalizedDescription = pddl::normalize(std::move(description));
const auto translator = plasp::pddl::TranslatorASP(std::move(normalizedDescription), logger.outputStream());
translator.translate();
}
else if (language == plasp::Language::Type::SAS)
{
const auto description = plasp::sas::Description::fromTokenizer(std::move(tokenizer));
const auto translator = plasp::sas::TranslatorASP(description, logger.outputStream());
translator.translate();
}
}
catch (const tokenize::TokenizerException &e)
{
logger.log(colorlog::Priority::Error, e.location(), e.message().c_str());
printCompatibilityInfo();
return EXIT_FAILURE;
}
catch (const pddl::ParserException &e)
{
if (e.location())
logger.log(colorlog::Priority::Error, e.location().value(), e.message().c_str());
else
logger.log(colorlog::Priority::Error, e.message().c_str());
printCompatibilityInfo();
return EXIT_FAILURE;
}
catch (const plasp::TranslatorException &e)
{
logger.log(colorlog::Priority::Error, e.what());
return EXIT_FAILURE;
}
catch (const std::exception &e)
{
logger.log(colorlog::Priority::Error, e.what());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@ -2,183 +2,64 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <boost/program_options.hpp> #include <cxxopts.hpp>
#include <colorlog/ColorStream.h>
#include <colorlog/Logger.h> #include <colorlog/Logger.h>
#include <colorlog/Priority.h> #include <colorlog/Priority.h>
#include <pddl/AST.h> #include <plasp-app/Commands.h>
#include <pddl/Exception.h>
#include <pddl/Mode.h>
#include <pddl/Normalize.h>
#include <pddl/NormalizedASTOutput.h>
#include <pddl/Parse.h>
#include <pddl/detail/normalization/Reduction.h>
#include <plasp/LanguageDetection.h>
#include <plasp/TranslatorException.h>
#include <plasp/pddl/TranslatorASP.h>
#include <plasp/sas/Description.h>
#include <plasp/sas/TranslatorASP.h>
#include <plasp-app/CommonOptions.h>
#include <plasp-app/Version.h> #include <plasp-app/Version.h>
#include <plasp-app/commands/Translate.h>
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
namespace po = boost::program_options;
po::options_description description;
description.add(basicOptions());
description.add(outputOptions());
description.add(parserOptions());
po::variables_map variablesMap;
const auto printHelp = const auto printHelp =
[&]() [&]()
{ {
std::cout << "Usage: plasp [options] file..." << std::endl; // TODO: add list of available commands
std::cout << "Translate PDDL to ASP." << std::endl; std::cout
<< "ASP planning tools for PDDL." << std::endl
<< "Usage: plasp <command> [<arguments>]" << std::endl
<< "Translate PDDL to ASP." << std::endl;
};
std::cout << description; const auto printVersion =
[&]()
{
std::cout << Version << std::endl;
}; };
colorlog::Logger logger; colorlog::Logger logger;
if (argc < 2)
{
printHelp();
return EXIT_FAILURE;
}
try try
{ {
po::store(po::command_line_parser(argc, argv) switch (parseCommand(argv[1]))
.options(description) {
.positional(parserPositionalOptions()) case Command::Help:
.run(), printHelp();
variablesMap); return EXIT_SUCCESS;
po::notify(variablesMap); case Command::Version:
printVersion();
return EXIT_SUCCESS;
case Command::Translate:
return translate(argc - 1, &argv[1]);
default:
exit(EXIT_FAILURE);
}
} }
catch (const po::error &e) catch (std::exception &exception)
{ {
logger.log(colorlog::Priority::Error, e.what()); logger.log(colorlog::Priority::Error, exception.what());
std::cout << std::endl; std::cout << std::endl;
printHelp(); printHelp();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
BasicOptions basicOptions; return EXIT_FAILURE;
OutputOptions outputOptions;
ParserOptions parserOptions;
try
{
basicOptions = parseBasicOptions(variablesMap);
if (basicOptions.help)
{
printHelp();
return EXIT_SUCCESS;
}
if (basicOptions.version)
{
std::cout << Version << std::endl;
return EXIT_SUCCESS;
}
if (basicOptions.warningsAsErrors)
logger.setAbortPriority(colorlog::Priority::Warning);
const auto outputOptions = parseOutputOptions(variablesMap);
logger.setColorPolicy(outputOptions.colorPolicy);
logger.setLogPriority(outputOptions.logPriority);
parserOptions = parseParserOptions(variablesMap);
}
catch (const OptionException &e)
{
logger.log(colorlog::Priority::Error, e.what());
std::cout << std::endl;
printHelp();
return EXIT_FAILURE;
}
const auto printCompatibilityInfo =
[&]()
{
if (parserOptions.parsingMode != pddl::Mode::Compatibility)
logger.log(colorlog::Priority::Info, "try using --parsing-mode=compatibility for extended legacy feature support");
};
try
{
tokenize::Tokenizer<tokenize::CaseInsensitiveTokenizerPolicy> tokenizer;
std::for_each(parserOptions.inputFiles.cbegin(), parserOptions.inputFiles.cend(),
[&](const auto &inputFile)
{
tokenizer.read(inputFile);
});
if (parserOptions.inputFiles.empty())
tokenizer.read("std::cin", std::cin);
if (parserOptions.language == plasp::Language::Type::Automatic)
parserOptions.language = plasp::detectLanguage(tokenizer);
if (parserOptions.language == plasp::Language::Type::PDDL)
{
const auto logWarning =
[&](const auto &location, const auto &warning)
{
logger.log(colorlog::Priority::Warning, location, warning);
};
auto context = pddl::Context(std::move(tokenizer), logWarning);
context.mode = parserOptions.parsingMode;
auto description = pddl::parseDescription(context);
auto normalizedDescription = pddl::normalize(std::move(description));
const auto translator = plasp::pddl::TranslatorASP(std::move(normalizedDescription), logger.outputStream());
translator.translate();
}
else if (parserOptions.language == plasp::Language::Type::SAS)
{
const auto description = plasp::sas::Description::fromTokenizer(std::move(tokenizer));
const auto translator = plasp::sas::TranslatorASP(description, logger.outputStream());
translator.translate();
}
else
throw std::runtime_error("language detection failed");
}
catch (const tokenize::TokenizerException &e)
{
logger.log(colorlog::Priority::Error, e.location(), e.message().c_str());
printCompatibilityInfo();
return EXIT_FAILURE;
}
catch (const pddl::ParserException &e)
{
if (e.location())
logger.log(colorlog::Priority::Error, e.location().value(), e.message().c_str());
else
logger.log(colorlog::Priority::Error, e.message().c_str());
printCompatibilityInfo();
return EXIT_FAILURE;
}
catch (const plasp::TranslatorException &e)
{
logger.log(colorlog::Priority::Error, e.what());
return EXIT_FAILURE;
}
catch (const std::exception &e)
{
logger.log(colorlog::Priority::Error, e.what());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} }