From f6cfc55e210f18565b630f92a00fcf25c903e43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 10 Jun 2016 17:12:23 +0200 Subject: [PATCH] Put exception handling back into plasp binary for convenient use. --- apps/plasp-app/main.cpp | 75 +++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/apps/plasp-app/main.cpp b/apps/plasp-app/main.cpp index a3ababd..09f892a 100644 --- a/apps/plasp-app/main.cpp +++ b/apps/plasp-app/main.cpp @@ -62,51 +62,60 @@ int main(int argc, char **argv) return EXIT_SUCCESS; } - plasp::utils::Parser parser; - - parser.setCaseSensitive(false); - - if (variablesMap.count("input")) + try { - const auto &inputFiles = variablesMap["input"].as>(); + plasp::utils::Parser parser; - std::for_each(inputFiles.cbegin(), inputFiles.cend(), - [&](const auto &inputFile) - { - parser.readFile(inputFile); - }); - } - else - parser.readStream("std::cin", std::cin); + parser.setCaseSensitive(false); - const auto detectLanguage = - [&]() + if (variablesMap.count("input")) { - if (variablesMap.count("language") == 0) - return plasp::detectLanguage(parser); + const auto &inputFiles = variablesMap["input"].as>(); - const auto languageName = variablesMap["language"].as(); + std::for_each(inputFiles.cbegin(), inputFiles.cend(), + [&](const auto &inputFile) + { + parser.readFile(inputFile); + }); + } + else + parser.readStream("std::cin", std::cin); - return plasp::Language::fromString(languageName); - }; + const auto detectLanguage = + [&]() + { + if (variablesMap.count("language") == 0) + return plasp::detectLanguage(parser); - const auto language = detectLanguage(); + const auto languageName = variablesMap["language"].as(); - if (language == plasp::Language::Type::Unknown) + return plasp::Language::fromString(languageName); + }; + + const auto language = detectLanguage(); + + if (language == plasp::Language::Type::Unknown) + { + std::cerr << "Error: Unknown input language" << std::endl << std::endl; + printHelp(); + return EXIT_FAILURE; + } + + if (language == plasp::Language::Type::PDDL) + plasp::pddl::Description::fromParser(std::move(parser)); + else if (language == plasp::Language::Type::SAS) + { + const auto sasDescription = plasp::sas::Description::fromParser(std::move(parser)); + const auto sasTranslator = plasp::sas::TranslatorASP(sasDescription); + sasTranslator.translate(std::cout); + } + } + catch (const std::exception &e) { - std::cerr << "Error: Unknown input language" << std::endl << std::endl; + std::cerr << "Error: " << e.what() << std::endl << std::endl; printHelp(); return EXIT_FAILURE; } - if (language == plasp::Language::Type::PDDL) - plasp::pddl::Description::fromParser(std::move(parser)); - else if (language == plasp::Language::Type::SAS) - { - const auto sasDescription = plasp::sas::Description::fromParser(std::move(parser)); - const auto sasTranslator = plasp::sas::TranslatorASP(sasDescription); - sasTranslator.translate(std::cout); - } - return EXIT_SUCCESS; }