switch to using getopt for command line options

This commit is contained in:
Xircon 2026-08-01 03:44:22 -04:00
parent e30c147da4
commit 4bddf4f5ea
2 changed files with 53 additions and 5 deletions

View File

@ -39,4 +39,12 @@ namespace err {
inline void undeclared(std::optional<std::string> err) {
std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37mundeclared varible '" << err.value() << "'\033[0m" << std::endl;
}
inline void nofile() {
std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37mno input files\033[0m" << std::endl;
}
inline void help() {
std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37mhelp no implemented... just... uhm... look at main.cxx?\033[0m" << std::endl;
}
}

View File

@ -3,6 +3,7 @@
#include <sstream>
#include <optional>
#include <vector>
#include <getopt.h>
#include "arena.hxx"
#include "parser.hxx"
@ -10,15 +11,52 @@
#include "tokenization.hxx"
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "xlang: \033[1;31merror:\033[0m \033[1;37mno input files\033[0m" << std::endl; // yes i spent 10 minutes to emulate clangs output...
std::string outfile = "out.asm";
std::optional<std::string> infile;
int opt;
struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"version", no_argument, nullptr, 'v'},
{"output", required_argument, nullptr, 'o'},
{nullptr, 0, nullptr, 0}
};
while (true) {
int option_index = 0;
opt = getopt_long(argc, argv, "hvo:", long_options, &option_index);
if (opt == -1) break;
switch (opt) {
case 'h':
err::help();
break;
case 'v':
std::cout << "Version 0.0.1a\n";
break;
case 'o':
outfile = optarg;
break;
default:
err::help();
return EXIT_FAILURE;
}
}
if (optind < argc) {
infile = argv[optind];
} else {
err::nofile();
return EXIT_FAILURE;
}
if (!infile.has_value()) {
err::nofile();
return EXIT_FAILURE;
}
std::string contents;
{
std::stringstream contents_stream;
std::fstream input(argv[1], std::ios::in);
std::fstream input(infile.value(), std::ios::in);
contents_stream << input.rdbuf();
contents = contents_stream.str();
}
@ -37,11 +75,13 @@ int main(int argc, char* argv[]) {
Generator generator(prog.value());
{
std::fstream output("out.asm", std::ios::out);
std::fstream output(outfile, std::ios::out);
output << generator.gen_prog();
}
system("nasm -felf64 out.asm");
std::stringstream nasmcmd;
nasmcmd << "nasm -felf64 " << outfile;
system(nasmcmd.str().c_str());
system("ld -o out out.o");
return EXIT_SUCCESS;