anthem/app/main.cpp

133 lines
4.0 KiB
C++
Raw Normal View History

2016-11-21 17:51:14 +01:00
#include <iostream>
#include <cxxopts.hpp>
2016-11-21 17:51:14 +01:00
#include <anthem/AST.h>
#include <anthem/Context.h>
2016-11-22 03:15:52 +01:00
#include <anthem/Translation.h>
2016-11-21 17:51:14 +01:00
int main(int argc, char **argv)
{
2016-11-24 02:57:33 +01:00
anthem::Context context;
cxxopts::Options options("anthem", "Translate ASP programs to the language of first-order theorem provers.");
2016-11-21 17:51:14 +01:00
options.add_options()
("h,help", "Display this help message")
("v,version", "Display version information")
("i,input", "Input files", cxxopts::value<std::vector<std::string>>())
("no-simplify", "Do not simplify the output")
("no-complete", "Do not perform completion")
("no-detect-integers", "Do not detect integer variables")
("color", "Colorize output (always, never, auto)", cxxopts::value<std::string>()->default_value("auto"))
("parentheses", "Parenthesis style (normal, full)", cxxopts::value<std::string>()->default_value("normal"))
("p,log-priority", "Log messages starting from this priority (debug, info, warning, error)", cxxopts::value<std::string>()->default_value("info"));
2016-11-21 17:51:14 +01:00
options.parse_positional("input");
options.positional_help("[<input file...>]");
2016-11-21 17:51:14 +01:00
const auto printHelp =
[&]()
{
std::cout << options.help();
2016-11-21 17:51:14 +01:00
};
bool help;
bool version;
std::vector<std::string> inputFiles;
std::string colorPolicyString;
std::string parenthesisStyleString;
std::string logPriorityString;
2016-11-21 17:51:14 +01:00
try
{
const auto parseResult = options.parse(argc, argv);
help = (parseResult.count("help") > 0);
version = (parseResult.count("version") > 0);
if (parseResult.count("input") > 0)
inputFiles = parseResult["input"].as<std::vector<std::string>>();
context.performSimplification = (parseResult.count("no-simplify") == 0);
context.performCompletion = (parseResult.count("no-complete") == 0);
context.performIntegerDetection = (parseResult.count("no-detect-integers") == 0);
colorPolicyString = parseResult["color"].as<std::string>();
parenthesisStyleString = parseResult["parentheses"].as<std::string>();
logPriorityString = parseResult["log-priority"].as<std::string>();
2016-11-21 17:51:14 +01:00
}
catch (const std::exception &exception)
2016-11-21 17:51:14 +01:00
{
context.logger.log(anthem::output::Priority::Error) << exception.what();
context.logger.errorStream() << std::endl;
2016-11-21 17:51:14 +01:00
printHelp();
return EXIT_FAILURE;
}
if (help)
2016-11-21 17:51:14 +01:00
{
printHelp();
return EXIT_SUCCESS;
}
if (version)
2016-11-21 17:51:14 +01:00
{
2018-04-20 16:37:03 +02:00
std::cout << "anthem version 0.1.8+git" << std::endl;
2016-11-21 17:51:14 +01:00
return EXIT_SUCCESS;
}
if (colorPolicyString == "auto")
context.logger.setColorPolicy(anthem::output::ColorStream::ColorPolicy::Auto);
else if (colorPolicyString == "never")
context.logger.setColorPolicy(anthem::output::ColorStream::ColorPolicy::Never);
else if (colorPolicyString == "always")
context.logger.setColorPolicy(anthem::output::ColorStream::ColorPolicy::Always);
else
{
2017-05-30 17:19:26 +02:00
context.logger.log(anthem::output::Priority::Error) << "unknown color policy “" << colorPolicyString << "";
context.logger.errorStream() << std::endl;
printHelp();
return EXIT_FAILURE;
}
if (parenthesisStyleString == "normal")
context.parenthesisStyle = anthem::ast::ParenthesisStyle::Normal;
else if (parenthesisStyleString == "full")
context.parenthesisStyle = anthem::ast::ParenthesisStyle::Full;
else
{
context.logger.log(anthem::output::Priority::Error) << "unknown parenthesis style “" << parenthesisStyleString << "";
context.logger.errorStream() << std::endl;
printHelp();
return EXIT_FAILURE;
}
try
{
const auto logPriority = anthem::output::priorityFromName(logPriorityString.c_str());
context.logger.setLogPriority(logPriority);
}
catch (const std::exception &e)
{
2017-05-30 17:19:26 +02:00
context.logger.log(anthem::output::Priority::Error) << "unknown log priorty “" << logPriorityString << "";
context.logger.errorStream() << std::endl;
printHelp();
return EXIT_FAILURE;
}
2016-11-22 03:15:52 +01:00
try
{
if (!inputFiles.empty())
anthem::translate(inputFiles, context);
2016-11-22 03:15:52 +01:00
else
anthem::translate("std::cin", std::cin, context);
2016-11-22 03:15:52 +01:00
}
catch (const std::exception &e)
{
2017-05-30 17:19:26 +02:00
context.logger.log(anthem::output::Priority::Error) << e.what();
2016-11-22 03:15:52 +01:00
return EXIT_FAILURE;
}
2016-11-21 17:51:14 +01:00
return EXIT_SUCCESS;
}