Compare commits

..

No commits in common. "c7ce87eb7addc7d2fb775c5949a47eb9b7dd4e0f" and "1ede2cc2be3670d9f2daa3903e7f17905cc0811b" have entirely different histories.

4 changed files with 22 additions and 101 deletions

View File

@ -14,47 +14,19 @@ 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 (!(IsMarkupFile(input_file))) { if (!(CLI::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
ParseArgs(argc, argv); for (int i = 1; i < argc; i++) {
args.push_back(argv[i]);
} catch (const std::invalid_argument &e) {
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";
} }
} catch (const std::invalid_argument &e) {
std::cerr << e.what() << "\n";
PrintHelp();
std::exit(EXIT_FAILURE);
}
} }
bool CLI::IsMarkupFile(const std::string &filename) { bool CLI::IsMarkupFile(const std::string &filename) {
@ -79,17 +51,11 @@ bool CLI::IsMarkupFile(const std::string &filename) {
requiredExtension; requiredExtension;
} }
bool CLI::IsHtmlFile(const std::string &filename) { // place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going
// HTML file extension // to get each method to run
const std::string requiredExtension = ".html"; void CLI::RunCommands() {
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,14 +52,6 @@ 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
* *
@ -68,37 +60,20 @@ 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,20 +3,16 @@
#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(int argc, char** argv) DocumentConverter(std::string input, std::string output = "")
: cli(argc, argv), : parser(input, output), watchdog(input) {};
parser(cli.GetInputFile(), cli.GetOutputFile()),
watchdog(cli.GetInputFile()) {}
void Convert() { void Convert() {
this->parser.ParseDocument(); this->parser.ParseDocument();
@ -30,9 +26,7 @@ public:
this->parser.ParseDocument(); this->parser.ParseDocument();
this->parser.WriteOutput(); this->parser.WriteOutput();
}; };
if (cli.WatchDogEnabled()){ this->watchdog.Start(callback);
this->watchdog.Start(callback);
}
} }
}; };

View File

@ -68,22 +68,8 @@ void test_input(int argc, char **argv) {
} }
void test_document_converter() { void test_document_converter() {
// Simulate command line: DocumentConverter dc("test/input.md");
// program name, input file, watch flag, output file dc.ConvertWatcher();
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(); }