Adding command line options parser

This commit is contained in:
Stefano Sanfilippo 2015-03-10 14:36:32 +01:00
parent 04e031a6a0
commit 22f4c334b7
4 changed files with 128 additions and 4 deletions

77
src/CLineParser.cpp Normal file
View File

@ -0,0 +1,77 @@
/*
* Monicelli: an esoteric language compiler
*
* Copyright (C) 2014 Stefano Sanfilippo
*
* 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/>.
*/
#include "CLineParser.hpp"
#include <boost/program_options.hpp>
#include <cstdlib>
#include <string>
#include <vector>
namespace po = boost::program_options;
using namespace monicelli;
static po::variables_map CONFIG;
static const std::string VERSION_STRING =
"mcc, Monicelli compiler <https://github.com/esseks/monicelli>\n"
"\n"
"Copyright © 2014,2015 Stefano Sanfilippo\n"
"\n"
"This program comes with ABSOLUTELY NO WARRANTY;\n"
"This is free software, and you are welcome to redistribute it\n"
"under certain conditions; See LICENSE.txt for all details"
;
po::variables_map const& monicelli::getConfig() {
return CONFIG;
}
void monicelli::parseCommandLine(int argc, char **argv) {
po::options_description desc(
std::string("Usage: ") + argv[0] + " [options] file.mc ..."
);
desc.add_options()
("help,h", "display this help message")
("version,v", "display version")
("input,i", po::value<std::vector<std::string>>(), "input files to process")
;
po::positional_options_description positional;
positional.add("input", -1);
po::store(
po::command_line_parser(argc, argv)
.options(desc)
.positional(positional)
.run(),
CONFIG
);
po::notify(CONFIG);
if (configHas("help")) {
std::cout << desc;
exit(0);
}
if (configHas("version")) {
std::cout << VERSION_STRING << std::endl;
exit(0);
}
}

43
src/CLineParser.hpp Normal file
View File

@ -0,0 +1,43 @@
#ifndef CLINE_PARSER_HPP
#define CLINE_PARSER_HPP
/*
* Monicelli: an esoteric language compiler
*
* Copyright (C) 2014 Stefano Sanfilippo
*
* 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/>.
*/
#include <boost/program_options.hpp>
namespace monicelli {
boost::program_options::variables_map const& getConfig();
void parseCommandLine(int argc, char **argv);
template<typename T> inline
T config(std::string const& name) {
return getConfig()[name].as<T>();
}
static inline
bool configHas(std::string const& name) {
return getConfig().count(name);
}
}
#endif

View File

@ -30,7 +30,7 @@ endif()
## 2. External components ## 2. External components
find_package(Boost 1.48 REQUIRED regex system filesystem) find_package(Boost 1.48 REQUIRED regex system filesystem program_options)
find_package(LLVM REQUIRED CONFIG) find_package(LLVM REQUIRED CONFIG)
find_library(YAML_LIBRARIES yaml-cpp) find_library(YAML_LIBRARIES yaml-cpp)
@ -59,7 +59,7 @@ flex_target(Scanner Monicelli.lpp ${CMAKE_CURRENT_BINARY_DIR}/Lexer.cpp)
add_flex_bison_dependency(Scanner Parser) add_flex_bison_dependency(Scanner Parser)
add_executable(mcc add_executable(mcc
main.cpp Nodes.cpp main.cpp Nodes.cpp CLineParser.cpp
ModuleRegistry.cpp ModuleLoader.cpp ModuleRegistry.cpp ModuleLoader.cpp
${BISON_Parser_OUTPUTS} ${FLEX_Scanner_OUTPUTS} ${BISON_Parser_OUTPUTS} ${FLEX_Scanner_OUTPUTS}
CppEmitter.cpp BitcodeEmitter.cpp CppEmitter.cpp BitcodeEmitter.cpp

View File

@ -23,6 +23,7 @@
#include "ModuleRegistry.hpp" #include "ModuleRegistry.hpp"
#include "ModuleLoader.hpp" #include "ModuleLoader.hpp"
#include "BitcodeEmitter.hpp" #include "BitcodeEmitter.hpp"
#include "CLineParser.hpp"
#include <llvm/Bitcode/ReaderWriter.h> #include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/Support/FileSystem.h> #include <llvm/Support/FileSystem.h>
@ -40,6 +41,10 @@ using namespace monicelli;
int main(int argc, char **argv) { int main(int argc, char **argv) {
parseCommandLine(argc, argv);
if (!configHas("input")) return 0;
registerStdLib(getModuleRegistry()); registerStdLib(getModuleRegistry());
boost::regex namere("^(.+)\\.mc$"); boost::regex namere("^(.+)\\.mc$");
@ -48,8 +53,7 @@ int main(int argc, char **argv) {
std::vector<std::string> sources; std::vector<std::string> sources;
std::vector<std::string> modules; std::vector<std::string> modules;
for (int i = 1; i < argc; ++i) { for (std::string const& arg: config<std::vector<std::string>>("input")) {
std::string arg(argv[i]);
if (boost::regex_match(arg, namere)) { if (boost::regex_match(arg, namere)) {
sources.push_back(arg); sources.push_back(arg);
} else if (boost::regex_match(arg, modulere)) { } else if (boost::regex_match(arg, modulere)) {