add command line options

This commit is contained in:
Chris Cromer 2023-02-08 09:36:43 -03:00
parent e09823bfe8
commit 4a7cf9ec2e
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
4 changed files with 95 additions and 24 deletions

View File

@ -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_sources = files(
'obelisk.cpp', 'obelisk.cpp',
'lexer.cpp', 'lexer.cpp',

View File

@ -2,6 +2,7 @@
#include "lexer.h" #include "lexer.h"
#include "obelisk.h" #include "obelisk.h"
#include "parser.h" #include "parser.h"
#include "version.h"
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
@ -73,38 +74,62 @@ static int obelisk::mainLoop()
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void obelisk::showUsage()
{
std::cout << obelisk::usageMessage << std::endl;
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
for (int i = 1; i < argc; i++) std::vector<std::string> 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. if (optind < argc)
// Inspired by Godot's double precision on the GPU to render large worlds.
/*try
{ {
float first; while (optind < argc)
float second; {
double var = 0.123456789012345; sourceFiles.push_back(argv[optind++]);
}
obelisk::KnowledgeBase* kb = new obelisk::KnowledgeBase("cromer.kb");
kb->getFloat(first, second, var);
std::cout << std::setprecision(std::numeric_limits<double>::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<double>::digits10)
<< "Double: " << var << std::endl
<< "First: " << first << std::endl
<< "Second: " << second << std::endl;
} }
catch (obelisk::KnowledgeBaseException& exception)
if (sourceFiles.size() == 0)
{ {
obelisk::showUsage();
return EXIT_FAILURE; return EXIT_FAILURE;
}*/ }
std::cout << sourceFiles[0] << std::endl;
std::cout << kbFile << std::endl;
return 0;
return obelisk::mainLoop(); return obelisk::mainLoop();
} }

View File

@ -1,10 +1,41 @@
#include <getopt.h>
/** /**
* @brief The obelisk namespace contains everything needed to compile obelisk * @brief The obelisk namespace contains everything needed to compile obelisk.
* code. * code.
* *
*/ */
namespace obelisk 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. * @brief This is the main loop for obelisk.
* This loop handles lexing and parsing of obelisk source code. * This loop handles lexing and parsing of obelisk source code.

8
src/version.h.in Normal file
View File

@ -0,0 +1,8 @@
namespace obelisk
{
/**
* @brief The current version of obelisk.
*
*/
std::string version = "@version@";
} // namespace obelisk