From 4a7cf9ec2edde6e95072c671160c5e4d74314f11 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 8 Feb 2023 09:36:43 -0300 Subject: [PATCH] add command line options --- src/meson.build | 7 +++++ src/obelisk.cpp | 71 ++++++++++++++++++++++++++++++++---------------- src/obelisk.h | 33 +++++++++++++++++++++- src/version.h.in | 8 ++++++ 4 files changed, 95 insertions(+), 24 deletions(-) create mode 100644 src/version.h.in diff --git a/src/meson.build b/src/meson.build index 0f4d7b2..81adf96 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,3 +1,10 @@ +conf_data = configuration_data() +conf_data.set('version', meson.project_version()) +configure_file(input : 'version.h.in', + output : 'version.h', + configuration : conf_data +) + obelisk_sources = files( 'obelisk.cpp', 'lexer.cpp', diff --git a/src/obelisk.cpp b/src/obelisk.cpp index 8a110df..cb0cb54 100644 --- a/src/obelisk.cpp +++ b/src/obelisk.cpp @@ -2,6 +2,7 @@ #include "lexer.h" #include "obelisk.h" #include "parser.h" +#include "version.h" #include #include @@ -73,38 +74,62 @@ static int obelisk::mainLoop() return EXIT_SUCCESS; } +void obelisk::showUsage() +{ + std::cout << obelisk::usageMessage << std::endl; +} + int main(int argc, char** argv) { - for (int i = 1; i < argc; i++) + std::vector sourceFiles; + std::string kbFile = "obelisk.kb"; + + while (true) { - std::cout << argv[i] << std::endl; + int option_index = 0; + switch (getopt_long(argc, argv, "k:hv", obelisk::long_options, &option_index)) + { + case 'k' : + kbFile = std::string(optarg); + continue; + case 'h' : + obelisk::showUsage(); + return EXIT_SUCCESS; + break; + case 'v' : + std::cout << "obelisk " << obelisk::version << std::endl; + return EXIT_SUCCESS; + break; + default : + obelisk::showUsage(); + return EXIT_FAILURE; + break; + + case -1 : + break; + } + + break; } - // This can be used to store a double as 2 floats in the database, then restore it back to a double. - // Inspired by Godot's double precision on the GPU to render large worlds. - /*try + if (optind < argc) { - float first; - float second; - double var = 0.123456789012345; - - obelisk::KnowledgeBase* kb = new obelisk::KnowledgeBase("cromer.kb"); - kb->getFloat(first, second, var); - std::cout << std::setprecision(std::numeric_limits::digits10) - << "Double: " << var << std::endl - << "First: " << first << std::endl - << "Second: " << second << std::endl; - var = 0.0; - kb->getDouble(var, first, second); - std::cout << std::setprecision(std::numeric_limits::digits10) - << "Double: " << var << std::endl - << "First: " << first << std::endl - << "Second: " << second << std::endl; + while (optind < argc) + { + sourceFiles.push_back(argv[optind++]); + } } - catch (obelisk::KnowledgeBaseException& exception) + + if (sourceFiles.size() == 0) { + obelisk::showUsage(); return EXIT_FAILURE; - }*/ + } + + std::cout << sourceFiles[0] << std::endl; + std::cout << kbFile << std::endl; + + return 0; return obelisk::mainLoop(); } diff --git a/src/obelisk.h b/src/obelisk.h index 588182e..ec0d644 100644 --- a/src/obelisk.h +++ b/src/obelisk.h @@ -1,10 +1,41 @@ +#include + /** - * @brief The obelisk namespace contains everything needed to compile obelisk + * @brief The obelisk namespace contains everything needed to compile obelisk. * code. * */ namespace obelisk { + /** + * @brief The usage messsage displayed during help or incorrect usage. + * + */ + std::string usageMessage = R"(Usage: obelisk [OPTION]... [FILE]... +Compile the obelisk source FILE(s) into knoweldge base and library. + +Options: + -h, --help shows this help/usage message + -k, --kb=FILENAME output knowldege base filename + -v, --version shows the version of obelisk)"; + + /** + * @brief The command line arguments that obelisk accepts. + * + */ + static struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"kb", required_argument, 0, 'k'}, + {"version", no_argument, 0, 'v'}, + {0, 0, 0, 0 } + }; + + /** + * @brief Prints out the usage of obelisk to the stdin. + * + */ + static void showUsage(); + /** * @brief This is the main loop for obelisk. * This loop handles lexing and parsing of obelisk source code. diff --git a/src/version.h.in b/src/version.h.in new file mode 100644 index 0000000..7e82845 --- /dev/null +++ b/src/version.h.in @@ -0,0 +1,8 @@ +namespace obelisk +{ + /** + * @brief The current version of obelisk. + * + */ + std::string version = "@version@"; +} // namespace obelisk