Merge pull request '(FEAT): Added command parser' (#35) from feature/cmdparse into main

Reviewed-on: #35
Reviewed-by: Hayden Hargreaves <hayden@gophernest.net>
This commit is contained in:
Hayden Hargreaves 2025-10-31 20:11:45 -07:00
commit c7ce87eb7a
4 changed files with 100 additions and 21 deletions

View File

@ -14,21 +14,49 @@ CLI::CLI(int argc, char **argv) {
input_file = argv[1]; input_file = argv[1];
// checks that file has correct file extension // checks that file has correct file extension
if (!(CLI::IsMarkupFile(input_file))) { if (!(IsMarkupFile(input_file))) {
throw std::invalid_argument( throw std::invalid_argument(
"Error: Invalid file extension. Expected a '.md' (Markdown) file."); "Error: Invalid file extension. Expected a '.md' (Markdown) file.");
} }
// stores remaining arguments
for (int i = 1; i < argc; i++) { ParseArgs(argc, argv);
args.push_back(argv[i]);
}
} catch (const std::invalid_argument &e) { } catch (const std::invalid_argument &e) {
std::cerr << e.what() << "\n"; std::cerr << e.what() << "\n";
PrintHelp(); PrintHelp();
std::exit(EXIT_FAILURE); 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) { bool CLI::IsMarkupFile(const std::string &filename) {
// markdown file extension // markdown file extension
const std::string requiredExtension = ".md"; const std::string requiredExtension = ".md";
@ -51,11 +79,17 @@ bool CLI::IsMarkupFile(const std::string &filename) {
requiredExtension; requiredExtension;
} }
// place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going bool CLI::IsHtmlFile(const std::string &filename) {
// to get each method to run // HTML file extension
void CLI::RunCommands() { const std::string requiredExtension = ".html";
for (size_t i = 0; i < args.size(); i++) {
} // 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 { void CLI::PrintHelp() const {

View File

@ -52,6 +52,14 @@ public:
*/ */
std::string GetInputFile() const { return input_file; } 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 * @brief Returns output file
* *
@ -60,20 +68,37 @@ public:
std::string GetOutputFile() const { return output_file; } std::string GetOutputFile() const { return output_file; }
private: private:
/** /**
* @brief Ensures the provided filename has a .markup extension * @brief Ensures the provided filename has a .markup extension
* *
* @param filename The file name to validate
* @return true if file ends with .markup * @return true if file ends with .markup
* @return false otherwise * @return false otherwise
* @author Preston Shultz (shultzp1@my.erau.edu) * @author Preston Shultz (shultzp1@my.erau.edu)
*/ */
static bool IsMarkupFile(const std::string &filename); 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::string program_name;
std::vector<std::string> args;
std::string input_file; std::string input_file;
std::string output_file; std::string output_file = "";
bool watchdog_enabled = false;
}; };
#endif #endif

View File

@ -3,16 +3,20 @@
#include "parser.h" #include "parser.h"
#include "watchdog.h" #include "watchdog.h"
#include "commandLineParser.h"
#include <functional> #include <functional>
class DocumentConverter { class DocumentConverter {
private: private:
CLI cli;
Parser parser; Parser parser;
Watchdog watchdog; Watchdog watchdog;
public: public:
DocumentConverter(std::string input, std::string output = "") DocumentConverter(int argc, char** argv)
: parser(input, output), watchdog(input) {}; : cli(argc, argv),
parser(cli.GetInputFile(), cli.GetOutputFile()),
watchdog(cli.GetInputFile()) {}
void Convert() { void Convert() {
this->parser.ParseDocument(); this->parser.ParseDocument();
@ -26,7 +30,9 @@ public:
this->parser.ParseDocument(); this->parser.ParseDocument();
this->parser.WriteOutput(); this->parser.WriteOutput();
}; };
this->watchdog.Start(callback); if (cli.WatchDogEnabled()){
this->watchdog.Start(callback);
}
} }
}; };

View File

@ -68,8 +68,22 @@ void test_input(int argc, char **argv) {
} }
void test_document_converter() { void test_document_converter() {
DocumentConverter dc("test/input.md"); // Simulate command line:
dc.ConvertWatcher(); // 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<char**>(argv));
// Run the watcher (or use dc.Convert() for single conversion)
dc.ConvertWatcher();
} }
int main(int argc, char **argv) { test_document_converter(); } int main(int argc, char **argv) { test_document_converter(); }