From 640988eb2ea622603fc9bfc5e29d3e331c254058 Mon Sep 17 00:00:00 2001 From: ChUrl Date: Mon, 20 Mar 2023 17:50:38 +0100 Subject: [PATCH] Parse program arguments using Boost::program_options and read the specified input file --- src/main.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index bc8f460..c5ff3a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,46 @@ #include +#include +#include +#include +#include "lexer/Lexer.h" + +namespace po = boost::program_options; + +int main(int argc, char **argv) { + // Argument parsing straight from the Boost manual: https://www.boost.org/doc/libs/1_60_0/doc/html/program_options/tutorial.html + + // Declare the supported options. + po::options_description desc("Allowed options"); + + // TODO: What the fuck is this syntax? + desc.add_options() + ("help,h", "Show this help message") + ("input,i", po::value(), "Input file") + ("output,o", po::value(), "Output file"); + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help")) { + std::cout << desc << std::endl; + return 1; + } + + // Read the input file + std::ifstream ifs; + std::string input_string; + ifs.open(vm["input"].as(), std::ios_base::in); + if (!ifs) { + std::cout << "Failed to read input file!" << std::endl; + return -1; + } + while (!ifs.eof()) { + std::string line; + getline(ifs, line); + input_string += line + '\n'; + } + ifs.close(); int main() { std::cout << "Hello, World!" << std::endl;