diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-27 00:28:24 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-27 00:28:24 +0100 |
commit | a95cf9c477f7259f04d3759acec40a4070ae2b31 (patch) | |
tree | bba0c36bd7ec527bff51eeefdcf1e38452ba9e68 | |
parent | f1d432892ce158490bb564ba3d01982772439a81 (diff) |
first attempt on command line interface (only wrapper and boost program options).
-rw-r--r-- | CMakeLists.txt | 32 | ||||
-rw-r--r-- | src/cli/Main.cpp | 110 |
2 files changed, 133 insertions, 9 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 06f45d1..9e4979e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,7 @@ PKG_CHECK_MODULES(MOZJS REQUIRED mozjs-24) PKG_CHECK_MODULES(EXPAT REQUIRED expat) # Include required Boost components using the Boost cmake package -FIND_PACKAGE(Boost COMPONENTS system filesystem REQUIRED) +FIND_PACKAGE(Boost COMPONENTS system filesystem program_options REQUIRED) # Set utf8cpp include path SET(UTF8CPP_INCLUDE_DIR "lib/utf8cpp") @@ -158,22 +158,22 @@ ADD_LIBRARY(ousia_core # src/core/script/ScriptEngine ) -ADD_LIBRARY(ousia_filesystem - src/plugins/filesystem/FileLocator - src/plugins/filesystem/SpecialPaths +ADD_LIBRARY(ousia_css + src/plugins/css/CSSParser ) -TARGET_LINK_LIBRARIES(ousia_filesystem +TARGET_LINK_LIBRARIES(ousia_css ousia_core - ${Boost_LIBRARIES} ) -ADD_LIBRARY(ousia_css - src/plugins/css/CSSParser +ADD_LIBRARY(ousia_filesystem + src/plugins/filesystem/FileLocator + src/plugins/filesystem/SpecialPaths ) -TARGET_LINK_LIBRARIES(ousia_css +TARGET_LINK_LIBRARIES(ousia_filesystem ousia_core + ${Boost_LIBRARIES} ) ADD_LIBRARY(ousia_html @@ -202,6 +202,20 @@ TARGET_LINK_LIBRARIES(ousia_xml # ${MOZJS_LIBRARIES} #) +# Command line interface + +ADD_EXECUTABLE(ousia + src/cli/Main +) + +TARGET_LINK_LIBRARIES(ousia + ousia_core + ousia_css + ousia_filesystem + ousia_html + ousia_xml +) + # If testing is enabled, build the unit tests IF(TEST) diff --git a/src/cli/Main.cpp b/src/cli/Main.cpp new file mode 100644 index 0000000..73c46ba --- /dev/null +++ b/src/cli/Main.cpp @@ -0,0 +1,110 @@ +/* + Ousía + Copyright (C) 2014 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +/** + * @file Main.cpp + * + * This file provides the command line interface for Ousia. More information + * on command line options can be found in the test. + * + * @author Benjamin Paaßen (bpaassen@techfak.uni-bielefeld.de) + */ +#include <algorithm> +#include <iostream> + +#include <boost/program_options.hpp> + +const size_t ERROR_IN_COMMAND_LINE = 1; +const size_t SUCCESS = 0; +const size_t ERROR_UNHANDLED_EXCEPTION = 2; + +namespace po = boost::program_options; + +int main(int argc, char **argv) +{ + // Program options + po::options_description desc("Options"); + std::string inputPath; + std::string outputPath; + std::string format; + /* + * This is a rather strange access mechanism: add_options() returns an + * easy_init object that has overloaded the () operator to accept new + * initializations. Again: Rather strange syntax, but it works. + */ + desc.add_options()("help", "Program help")( + "input,i", po::value<std::string>(&inputPath)->required(), + "The input document file name")( + "include,I", po::value<std::vector<std::string>>(), + "Include paths, where resources like the input document " + "or additional domains, typesystems, etc. might be " + "found.")( + "output,o", po::value<std::string>(&outputPath), + "The output file name. Per default the input file name will be used.")( + "format,F", po::value<std::string>(&format)->required(), + "The output format that shall be produced."); + + po::variables_map vm; + try { + // try to read the values for each option to the variable map. + po::store(po::parse_command_line(argc, argv, desc), vm); + + // first check the help option. + if (vm.count("help")) { + std::cout + << "Ousia" << std::endl + << "Copyright (C) 2014 Benjamin Paassen, Andreas Stoeckel" + // write the program options description as generated by boost + << std::endl << desc << std::endl << std::endl; + return SUCCESS; + } + + // only if the user did not want help to we use notify, which checks + // if all required options were given. + po::notify(vm); + } + catch (po::error &e) { + std::cerr << "ERROR: " << e.what() << std::endl << std::endl; + std::cerr << desc << std::endl; + return ERROR_IN_COMMAND_LINE; + } + + if(!vm.count("output")){ + // TODO: Handle this better. + outputPath = inputPath; + if(outputPath.find(".oxd") != std::string::npos){ + outputPath.erase(outputPath.end()-3, outputPath.end()); + outputPath += format; + } + } + + // TODO: Program logic. + std::cout << "input : " << vm["input"].as<std::string>() << std::endl; + std::cout << "output : " << outputPath << std::endl; + std::cout << "format : " << vm["format"].as<std::string>() << std::endl; + if(vm.count("include")){ + std::vector<std::string> includes = vm["include"].as<std::vector<std::string>>(); + std::cout << "includes : "; + for(auto& i : includes){ + std::cout << i << ", "; + } + std::cout << std::endl; + } + + return SUCCESS; +} |