parse arguments

This commit is contained in:
2024-10-23 20:48:18 +02:00
parent 39eb113385
commit 3207de80dd
3 changed files with 47 additions and 5 deletions

3
.gitignore vendored
View File

@@ -5,5 +5,6 @@ build/
# MacOS Cache
.DS_Store
test.bin
.vscode
a.out

View File

@@ -1,13 +1,51 @@
#include <argparse/argparse.hpp>
#include <iostream>
#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;

View File

@@ -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