(FEAT): Added command parser
This commit is contained in:
parent
8c60ba2361
commit
24e1bb7ba5
@ -14,14 +14,14 @@ 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();
|
||||
@ -29,6 +29,34 @@ CLI::CLI(int argc, char **argv) {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
@ -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<std::string> args;
|
||||
std::string input_file;
|
||||
std::string output_file;
|
||||
std::string output_file = "";
|
||||
bool watchdog_enabled = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -3,16 +3,20 @@
|
||||
|
||||
#include "parser.h"
|
||||
#include "watchdog.h"
|
||||
#include "commandLineParser.h"
|
||||
#include <functional>
|
||||
|
||||
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,8 +30,10 @@ public:
|
||||
this->parser.ParseDocument();
|
||||
this->parser.WriteOutput();
|
||||
};
|
||||
if (cli.WatchDogEnabled()){
|
||||
this->watchdog.Start(callback);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
16
src/main.cpp
16
src/main.cpp
@ -68,7 +68,21 @@ void test_input(int argc, char **argv) {
|
||||
}
|
||||
|
||||
void test_document_converter() {
|
||||
DocumentConverter dc("test/input.md");
|
||||
// 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<char**>(argv));
|
||||
|
||||
// Run the watcher (or use dc.Convert() for single conversion)
|
||||
dc.ConvertWatcher();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user