diff --git a/lib/commandLineParser.cpp b/lib/commandLineParser.cpp index 2a1c4fc..b443b52 100644 --- a/lib/commandLineParser.cpp +++ b/lib/commandLineParser.cpp @@ -14,21 +14,49 @@ CLI::CLI(int argc, char **argv) { input_file = argv[1]; // checks that file has correct file extension - if (!(CLI::IsMarkupFile(input_file))) { + if (!(IsMarkupFile(input_file))) { throw std::invalid_argument( "Error: Invalid file extension. Expected a '.md' (Markdown) file."); } - // stores remaining arguments - for (int i = 1; i < argc; i++) { - args.push_back(argv[i]); - } + + ParseArgs(argc, argv); + + } catch (const std::invalid_argument &e) { - std::cerr << e.what() << "\n"; - PrintHelp(); - std::exit(EXIT_FAILURE); + std::cerr << e.what() << "\n"; + PrintHelp(); + std::exit(EXIT_FAILURE); } } +void CLI::ParseArgs(int argc, char** argv){ + for (int i = 2; i < argc; i++) { + std::string arg = argv[i]; + + if (arg == "-w" || arg == "--watch") { + watchdog_enabled = true; + continue; + } + + if ((arg == "-o" || arg == "--output") && i + 1 < argc) { + output_file = argv[++i]; + + if (!(IsHtmlFile(output_file))) { + throw std::invalid_argument("Error: Invalid file extension. Expected a '.html' (HTML) file."); + } + + continue; + } + + if (arg == "-o" || arg == "--output") { + throw std::invalid_argument("Error: Missing filename after '-o' or '--output'."); + } + + std::cerr << "Warning: Unrecognized argument '" << arg << "' ignored.\n"; + } + +} + bool CLI::IsMarkupFile(const std::string &filename) { // markdown file extension const std::string requiredExtension = ".md"; @@ -51,11 +79,17 @@ bool CLI::IsMarkupFile(const std::string &filename) { requiredExtension; } -// place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going -// to get each method to run -void CLI::RunCommands() { - for (size_t i = 0; i < args.size(); i++) { - } +bool CLI::IsHtmlFile(const std::string &filename) { + // HTML file extension + const std::string requiredExtension = ".html"; + + // Ensure the filename is long enough to contain the extension + if (filename.size() <= requiredExtension.size()) { + return false; + } + + // Check if the filename ends with ".html" + return filename.substr(filename.size() - requiredExtension.size()) == requiredExtension; } void CLI::PrintHelp() const { diff --git a/lib/commandLineParser.h b/lib/commandLineParser.h index 55ad1fd..f66253c 100644 --- a/lib/commandLineParser.h +++ b/lib/commandLineParser.h @@ -52,6 +52,14 @@ public: */ std::string GetInputFile() const { return input_file; } + + /** + * @brief Returns if watchdog is enabled + * + * @author Preston Shultz (shultzp1@my.erau.edu) + */ + bool WatchDogEnabled() {return watchdog_enabled;} + /** * @brief Returns output file * @@ -60,20 +68,37 @@ public: std::string GetOutputFile() const { return output_file; } private: + /** * @brief Ensures the provided filename has a .markup extension * - * @param filename The file name to validate * @return true if file ends with .markup * @return false otherwise * @author Preston Shultz (shultzp1@my.erau.edu) */ static bool IsMarkupFile(const std::string &filename); + + /** + * @brief Ensures the provided filename has a .html extension + * + * @return true if file ends with .html + * @return false otherwise + * @author Preston Shultz (shultzp1@my.erau.edu) + */ + static bool IsHtmlFile(const std::string &filename); + + /** + * @brief Parses the commands + * + * @author Preston Shultz (shultzp1@my.erau.edu) + */ + void ParseArgs(int argc, char** argv); + std::string program_name; - std::vector args; std::string input_file; - std::string output_file; + std::string output_file = ""; + bool watchdog_enabled = false; }; #endif diff --git a/lib/documentConverter.h b/lib/documentConverter.h index 29ce2f4..082ecf2 100644 --- a/lib/documentConverter.h +++ b/lib/documentConverter.h @@ -3,16 +3,20 @@ #include "parser.h" #include "watchdog.h" +#include "commandLineParser.h" #include class DocumentConverter { private: + CLI cli; Parser parser; Watchdog watchdog; public: - DocumentConverter(std::string input, std::string output = "") - : parser(input, output), watchdog(input) {}; + DocumentConverter(int argc, char** argv) + : cli(argc, argv), + parser(cli.GetInputFile(), cli.GetOutputFile()), + watchdog(cli.GetInputFile()) {} void Convert() { this->parser.ParseDocument(); @@ -26,7 +30,9 @@ public: this->parser.ParseDocument(); this->parser.WriteOutput(); }; - this->watchdog.Start(callback); + if (cli.WatchDogEnabled()){ + this->watchdog.Start(callback); + } } }; diff --git a/src/main.cpp b/src/main.cpp index 0a584e2..5607075 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,8 +68,22 @@ void test_input(int argc, char **argv) { } void test_document_converter() { - DocumentConverter dc("test/input.md"); - dc.ConvertWatcher(); + // Simulate command line: + // program name, input file, watch flag, output file + const char* argv[] = { + "program", // argv[0] - program name + "test/input.md", // argv[1] - input file + "-w", // argv[2] - enable watchdog + "-o", // argv[3] - output flag + "test/output.html" // argv[4] - output filename + }; + int argc = 5; + + // Construct DocumentConverter with simulated command line + DocumentConverter dc(argc, const_cast(argv)); + + // Run the watcher (or use dc.Convert() for single conversion) + dc.ConvertWatcher(); } int main(int argc, char **argv) { test_document_converter(); }