From 3207de80dd9c236bf0d6a2ac9e728686a6452981 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 23 Oct 2024 20:48:18 +0200 Subject: [PATCH] parse arguments --- .gitignore | 5 +++-- src/main.cpp | 44 +++++++++++++++++++++++++++++++++++++++++--- xmake.lua | 3 +++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 257d815..6475bd8 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ build/ # MacOS Cache .DS_Store -test.bin -.vscode \ No newline at end of file +.vscode + +a.out \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index d12f32f..c2d3e30 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,51 @@ +#include #include #include "IO.h" int main(int argc, char** argv) { - try { - auto output = ParseFile("test.asm"); + argparse::ArgumentParser program("Assembleur"); + + std::string inputFileName; + program.add_argument("file") + .help("The assembly file to compile") + .store_into(inputFileName); + + std::string outputFileName; + program.add_argument("-o", "--output") + .help("Specify the output file.") + .metavar("file") + .default_value(std::string("a.out")) + .store_into(outputFileName); + + std::string formatType; + program.add_argument("-f", "--format") + .help("Type of the output. [bin|int|binint]") + .metavar("type") + .default_value(std::string("binint")) + .choices("bin", "int", "binint") + .store_into(formatType); + + try { + program.parse_args(argc, argv); + } catch (const std::exception& err) { + std::cerr << err.what() << std::endl; + std::cerr << program; + std::exit(1); + } + + try { + auto output = ParseFile(inputFileName); + + if (formatType == "bin") { + OutputFileBinary(output, outputFileName); + } else if (formatType == "int") { + OutputFileIntegers(output, outputFileName); + } else { + OutputFileBinIntegers(output, outputFileName); + } - OutputFileBinary(output, "test.bin"); } catch (std::runtime_error& e) { std::cerr << e.what() << std::endl; return 1; diff --git a/xmake.lua b/xmake.lua index a66d418..eb130e3 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,9 +1,12 @@ add_rules("mode.debug", "mode.release") +add_requires("argparse") + target("Assembleur") set_kind("binary") add_files("src/*.cpp") set_rundir(".") + add_packages("argparse") -- -- If you want to known more usage about xmake, please see https://xmake.io