A simple scripting language in C++
Ferenc Szontágh
2025-04-18 c91e935c62b8e254b9daadf37b915c983518bff4
cli/main.cpp
@@ -5,9 +5,11 @@
#include "options.h"
#include "VoidScript.hpp"
// Supported command-line parameters and descriptions
const std::unordered_map<std::string, std::string> params = {
    { "--help",    "Print this help message"          },
    { "--version", "Print the version of the program" },
    { "--help",    "Print this help message"                                                                     },
    { "--version", "Print the version of the program"                                                            },
    { "--debug",   "Enable debug output (all components or use --debug=lexer, parser, interpreter, symboltable)" },
};
int main(int argc, char * argv[]) {
@@ -15,38 +17,67 @@
    for (const auto & [key, value] : params) {
        usage.append(" [" + key + "]");
    }
    if (argc < 2) {
        std::cerr << usage << "\n";
        return 1;
    }
    // Parse arguments: allow --help, --version, --debug[=component], and a single file
    bool debugLexer       = false;
    bool debugParser      = false;
    bool debugInterp      = false;
    bool debugSymbolTable = false;
    std::string file;
    const std::string arg = std::string(argv[1]);
    if (arg.starts_with("-")) {
        auto it = params.find(arg);
        if (it != params.end()) {
            if (arg == "--help") {
                std::cout << usage << "\n";
                for (const auto & [key, value] : params) {
                    std::cout << "  " << key << ": " << value << "\n";
                }
                return 0;
            }
            if (arg == "--version") {
                std::cout << "Version:      " << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH;
                std::cout << " (" << VERSION_GIT_HASH << ")\n";
                std::cout << "Architecture: " << VERSION_ARCH << "\n";
                std::cout << "System:       " << VERSION_SYSTEM_NAME << "\n";
                return 0;
    for (int i = 1; i < argc; ++i) {
        std::string a = argv[i];
        if (a == "--help") {
            std::cout << usage << "\n";
            for (const auto & [key, value] : params) {
                std::cout << "  " << key << ": " << value << "\n";
            }
            return 0;
        } else if (a == "--version") {
            std::cout << "Version:      " << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH << " ("
                      << VERSION_GIT_HASH << ")\n";
            std::cout << "Architecture: " << VERSION_ARCH << "\n";
            std::cout << "System:       " << VERSION_SYSTEM_NAME << "\n";
            return 0;
        } else if (a.rfind("--debug", 0) == 0) {
            if (a == "--debug") {
                debugLexer = debugParser = debugInterp = true;
            } else if (a.rfind("--debug=", 0) == 0) {
                std::string comp = a.substr(std::string("--debug=").size());
                if (comp == "lexer") {
                    debugLexer = true;
                } else if (comp == "parser") {
                    debugParser = true;
                } else if (comp == "interpreter") {
                    debugInterp = true;
                } else if (comp == "symboltable") {
                    debugSymbolTable = true;
                } else {
                    std::cerr << "Error: Unknown debug component '" << comp << "'\n";
                    std::cerr << usage << "\n";
                    return 1;
                }
            } else {
                std::cerr << "Error: Unknown option '" << a << "'\n";
                std::cerr << usage << "\n";
                return 1;
            }
        } else if (a.starts_with("-")) {
            std::cerr << "Error: Unknown option '" << a << "'\n";
            std::cerr << usage << "\n";
            return 1;
        } else if (file.empty()) {
            file = a;
        } else {
            std::cerr << "Error: Multiple files specified\n";
            std::cerr << usage << "\n";
            return 1;
        }
        std::cerr << "Error: Unknown option " << arg << "\n";
    }
    if (file.empty()) {
        std::cerr << "Error: No input file specified\n";
        std::cerr << usage << "\n";
        return 1;
    }
    file = arg;
    if (!std::filesystem::exists(file)) {
        std::cerr << "Error: File " << file << " does not exist.\n";
@@ -55,6 +86,7 @@
    const std::string filename = std::filesystem::canonical(file).string();
    VoidScript voidscript(filename);
    // Initialize and run with debug options
    VoidScript voidscript(filename, debugLexer, debugParser, debugInterp, debugSymbolTable);
    return voidscript.run();
}