From 918909c1fa46a390f286325671c891992e56dca2 Mon Sep 17 00:00:00 2001 From: Trianta <56975502+Trimutex@users.noreply.github.com> Date: Thu, 7 Dec 2023 21:36:51 -0600 Subject: [PATCH] Added checking arguments and a help message --- src/generator.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/generator.hpp | 19 ++++++++++++++++--- src/main.cpp | 4 ++++ 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/generator.cpp b/src/generator.cpp index e69de29..3f32cdb 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -0,0 +1,40 @@ +#include "generator.hpp" +#include + +void Generator::SetArguments(int argc, char* argv[]) { + std::string tempStr; + for (int i = 1; i < argc; ++i) { + tempStr.assign(argv[i]); + if (tempStr == "-i") { + setup.isFileSet = true; + ++i; + setup.filename.assign(argv[i]); + } + if (tempStr == "-k") { + setup.isPrefixSet = true; + ++i; + setup.prefixLength = std::stoi(argv[i]); + } + if (tempStr == "-n") { + setup.isOutputSet = true; + ++i; + setup.outputLength = std::stoi(argv[i]); + } + if (tempStr == "-h") { + PrintUsage(); + exit(0); + } + } + if (!setup.isFileSet) { std::cerr << "[Setup - Error] Filename not specified" << std::endl; } + if (!setup.prefixLength) { std::cerr << "[Setup - Error] Prefix length not specified" << std::endl; } + if (!setup.outputLength) { std::cerr << "[Setup - Error] Output length not specified" << std::endl; } + if (!setup.isFileSet || !setup.isPrefixSet || !setup.isOutputSet) { PrintUsage(); } +} + +void PrintUsage(void) { + std::cout << "Usage: markov -i input_file -k prefix_length -n output_length" << std::endl; + std::cout << " -i: Direct path to input file for basis" << std::endl; + std::cout << " -k: Prefix length for Markov chain" << std::endl; + std::cout << " -n: Length of output to be generated (words)" << std::endl; + std::cout << " -h: Prints this usage text" << std::endl; +} diff --git a/src/generator.hpp b/src/generator.hpp index b4d95bd..a50313b 100644 --- a/src/generator.hpp +++ b/src/generator.hpp @@ -1,13 +1,26 @@ #ifndef GENERATOR_HPP #define GENERATOR_HPP +#include + +void PrintUsage(void); + +struct ArgumentList { + std::string filename = ""; + bool isFileSet = false; + int prefixLength = 0; + bool isPrefixSet = false; + int outputLength = 0; + bool isOutputSet = false; +}; + struct Generator { public: - Generator(void); + Generator(void) = default; + ~Generator(void) = default; void SetArguments(int argc, char* argv[]); private: - int prefixLength = 0; - int outputLength = 0; + ArgumentList setup; }; #endif // !GENERATOR_HPP diff --git a/src/main.cpp b/src/main.cpp index aba9d35..6997419 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,10 @@ #include "generator.hpp" int main(int argc, char* argv[]) { + if (argc == 1) { + PrintUsage(); + exit(0); + } Generator markovChain; markovChain.SetArguments(argc, argv); return 0;