From 94e63e8f760e7c9fb56bcbf8f9a491b1dd0f87ef Mon Sep 17 00:00:00 2001 From: Quentin Snow Date: Sun, 15 Jan 2023 19:37:49 -0600 Subject: [PATCH] mainOptHandler handles all paths --- README.md | 6 +- src/main.cpp | 2 +- src/optHandlers/mainOptHandler.cpp | 264 ++++++++++++++++++++++++++--- src/optHandlers/mainOptHandler.h | 11 ++ 4 files changed, 256 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 8a01df5..66e9522 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,13 @@ Actions: -c --create<=STRING> Creates a new account with NAME. -e --earn<=STRING> Add an earning to an account. -p --payment<=STRING> Add a payment to an account. -Account Options: [-dDv] +Account Options: [-dvD] -d --delete Deletes specified account. --force-delete Deletes the specified account without confirmation. -v --value Gets the current value of the account. - -D --description Changes the description of the account. + -D --description<=STRING> Changes the description of the account. Create Options: [-d] - -d --description Sets a description for an account. + -d --description<=STRING> Sets a description for an account. Earn Options: -v [-drD] -v --value= Value for earning. -d --description= Description for earning. diff --git a/src/main.cpp b/src/main.cpp index 354135f..f924e29 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,6 @@ void createRequiredFolders() { int main(int argc, char *argv[]) { std::vector args(argv, argv + argc); - + OptHandlers::MainOptHandler moh(args); return 0; } diff --git a/src/optHandlers/mainOptHandler.cpp b/src/optHandlers/mainOptHandler.cpp index a9a122f..8c4b827 100644 --- a/src/optHandlers/mainOptHandler.cpp +++ b/src/optHandlers/mainOptHandler.cpp @@ -3,11 +3,228 @@ // #include "mainOptHandler.h" #include +#include using namespace Budget::OptHandlers; -MainOptHandler::MainOptHandler(const std::vector &argv) { +MainOptHandler::MainOptHandler(const std::vector &_argv) : argv(_argv){ + struct option actionLongOpts[] = { + {"help", no_argument, nullptr, 'h'}, + {"account", required_argument, nullptr, 'a'}, + {"create", required_argument, nullptr, 'c'}, + {"earn", required_argument, nullptr, 'e'}, + {"payment", required_argument, nullptr, 'p'} + }; + + while (true) { + int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:", actionLongOpts, nullptr); + if (opt == -1) + break; + + switch (opt) { + case 'h': + help(); + exit(0); + case 'a': + accountOptHandler(optarg); + break; + case 'c': + createOptHandler(optarg); + break; + case 'e': + earnOptHandler(optarg); + break; + case 'p': + paymentOptHandler(optarg); + break; + case '?': + help(); + exit(0); + default: + break; + } + } +} +void MainOptHandler::accountOptHandler(std::string account) { + struct option accountLongOpts[] = { + {"help", no_argument, nullptr, 'h'}, + {"account", required_argument, nullptr, 'a'}, + {"create", required_argument, nullptr, 'c'}, + {"earn", required_argument, nullptr, 'e'}, + {"payment", required_argument, nullptr, 'p'}, + + {"delete", no_argument, nullptr, 'd'}, + {"force-delete", no_argument, nullptr, 'F'}, + {"value", no_argument, nullptr, 'v'}, + {"description", no_argument, nullptr, 'D'}, + }; + + while (true) { + int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:dFvD", accountLongOpts, nullptr); + if (opt == -1) + break; + + switch (opt) { + case 'h': + help(); + exit(0); + case 'a': + case 'c': + case 'e': + case 'p': + optind--; + return; + case 'd': + break; + case 'F': + break; + case 'v': + break; + case 'D': + break; + case '?': + help(); + exit(0); + default: + break; + } + } +} + +void MainOptHandler::createOptHandler(std::string account) { + struct option createLongOpts[] = { + {"help", no_argument, nullptr, 'h'}, + {"account", required_argument, nullptr, 'a'}, + {"create", required_argument, nullptr, 'c'}, + {"earn", required_argument, nullptr, 'e'}, + {"payment", required_argument, nullptr, 'p'}, + + {"description", required_argument, nullptr, 'd'}, + }; + + while (true) { + int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:d:", createLongOpts, nullptr); + if (opt == -1) + break; + + switch (opt) { + case 'h': + help(); + exit(0); + case 'a': + case 'c': + case 'e': + case 'p': + optind--; + return; + case 'd': + break; + case '?': + help(); + exit(0); + default: + break; + } + } +} + +void MainOptHandler::earnOptHandler(std::string account) { + struct option earnLongOpts[] = { + {"help", no_argument, nullptr, 'h'}, + {"account", required_argument, nullptr, 'a'}, + {"create", required_argument, nullptr, 'c'}, + {"earn", required_argument, nullptr, 'e'}, + {"payment", required_argument, nullptr, 'p'}, + + {"value", required_argument, nullptr, 'v'}, + {"description", required_argument, nullptr, 'd'}, + {"receipt", required_argument, nullptr, 'r'}, + {"date", required_argument, nullptr, 'D'}, + {"time", required_argument, nullptr, 't'} + }; + + while (true) { + int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:v:d:r:d:t:", earnLongOpts, nullptr); + if (opt == -1) + break; + + switch (opt) { + case 'h': + help(); + exit(0); + case 'a': + case 'c': + case 'e': + case 'p': + optind--; + return; + case 'v': + break; + case 'd': + break; + case 'r': + break; + case 'D': + break; + case 't': + break; + case '?': + help(); + exit(0); + default: + break; + } + } +} + +void MainOptHandler::paymentOptHandler(std::string account) { + struct option paymentLongOpts[] = { + {"help", no_argument, nullptr, 'h'}, + {"account", required_argument, nullptr, 'a'}, + {"create", required_argument, nullptr, 'c'}, + {"earn", required_argument, nullptr, 'e'}, + {"payment", required_argument, nullptr, 'p'}, + + {"value", required_argument, nullptr, 'v'}, + {"description", required_argument, nullptr, 'd'}, + {"receipt", required_argument, nullptr, 'r'}, + {"date", required_argument, nullptr, 'D'}, + {"time", required_argument, nullptr, 't'} + }; + + while (true) { + int opt = getopt_long(argv.size(), argv.data(), "ha:c:e:p:v:d:r:D:t", paymentLongOpts, nullptr); + if (opt == -1) + break; + + switch (opt) { + case 'h': + help(); + exit(0); + case 'a': + case 'c': + case 'e': + case 'p': + optind--; + return; + case 'v': + break; + case 'd': + break; + case 'r': + break; + case 'D': + break; + case 't': + break; + case '?': + help(); + exit(0); + default: + break; + } + } } void MainOptHandler::help() { @@ -15,32 +232,33 @@ void MainOptHandler::help() { std::cout << "Usage:" << std::endl; std::cout << "\tbudget [options] ..." << std::endl; std::cout << "Actions:" << std::endl; - std::cout << "\t-h --help\t\t\t\tPrints this." << std::endl; - std::cout << "\t-a --account<=STRING>\tManagement tools for an account." << std::endl; - std::cout << "\t-c --create<=STRING>\tCreates a new account with NAME." << std::endl; - std::cout << "\t-e --earn<=STRING>\t\tAdd an earning to an account." << std::endl; - std::cout << "\t-p --payment<=STRING>\tAdd a payment to an account." << std::endl; - std::cout << "Account Options: [-dDv]" << std::endl; - std::cout << "\t-d --delete\t\t\t\tDeletes specified account." << std::endl; - std::cout << "\t--force-delete\t\t\tDeletes the specified account without confirmation." << std::endl; - std::cout << "\t-v --value\t\t\t\tGets the current value of the account." << std::endl; - std::cout << "\t-D --description\t\tChanges the description of the account." << std::endl; + std::cout << "\t-h --help Prints this." << std::endl; + std::cout << "\t-a --account<=STRING> Management tools for an account." << std::endl; + std::cout << "\t-c --create<=STRING> Creates a new account with NAME." << std::endl; + std::cout << "\t-e --earn<=STRING> Add an earning to an account." << std::endl; + std::cout << "\t-p --payment<=STRING> Add a payment to an account." << std::endl; + std::cout << "Account Options: [-dvD]" << std::endl; + std::cout << "\t-d --delete Deletes specified account." << std::endl; + std::cout << "\t--force-delete Deletes the specified account without confirmation." << std::endl; + std::cout << "\t-v --value Gets the current value of the account." << std::endl; + std::cout << "\t-D --description<=STRING> Changes the description of the account." << std::endl; std::cout << "Create Options: [-d]" << std::endl; - std::cout << "\t-d --description\t\tSets a description for an account." << std::endl; + std::cout << "\t-d --description<=STRING> Sets a description for an account." << std::endl; std::cout << "Earn Options: -v [-drD]" << std::endl; - std::cout << "\t-v --value=\t\tValue for earning." << std::endl; - std::cout << "\t-d --description=\tDescription for earning." << std::endl; - std::cout << "\t-r --receipt=\t\tPath to file to store in DB as receipt." << std::endl; - std::cout << "\t-D --date=
\tDate as dd/mm/yyyy. Default will be today." << std::endl; - std::cout << "\t-t --time=\t\t24 hour time as hh/mm/ss. Default will be now." << std::endl; + std::cout << "\t-v --value= Value for earning." << std::endl; + std::cout << "\t-d --description= Description for earning." << std::endl; + std::cout << "\t-r --receipt= Path to file to store in DB as receipt." << std::endl; + std::cout << "\t-D --date=
Date as dd/mm/yyyy. Default will be today." << std::endl; + std::cout << "\t-t --time= 24 hour time as hh/mm/ss. Default will be now." << std::endl; std::cout << "Payment Options: -v [-drD]" << std::endl; - std::cout << "\t-v --value=\t\tValue for payment." << std::endl; - std::cout << "\t-d --description=\tDescription for payment." << std::endl; - std::cout << "\t-r --receipt=\t\tPath to file to store in DB as receipt." << std::endl; - std::cout << "\t-D --date=\t\tDate as dd/mm/yyyy. Default will be today." << std::endl; - std::cout << "\t-t --time=\t\t24 hour time as hh/mm/ss. Default will be now." << std::endl; + std::cout << "\t-v --value= Value for payment." << std::endl; + std::cout << "\t-d --description= Description for payment." << std::endl; + std::cout << "\t-r --receipt= Path to file to store in DB as receipt." << std::endl; + std::cout << "\t-D --date= Date as dd/mm/yyyy. Default will be today." << std::endl; + std::cout << "\t-t --time= 24 hour time as hh/mm/ss. Default will be now." << std::endl; std::cout << std::endl; - std::cout << "Arguments are processed like blocks with each one terminated by the next Action. For example" << std::endl; + std::cout << "Arguments are processed like blocks with each one terminated by the next Action. For example" + << std::endl; std::cout << std::endl; std::cout << R"(budget -cAcct -eAcct -v10.00 -r"./receipt.pdf" -pAcct -v5.50 -r"./payment.pdf")" << std::endl; std::cout << std::endl; diff --git a/src/optHandlers/mainOptHandler.h b/src/optHandlers/mainOptHandler.h index b48026f..ad49859 100644 --- a/src/optHandlers/mainOptHandler.h +++ b/src/optHandlers/mainOptHandler.h @@ -6,6 +6,7 @@ #define BUDGET_MAINOPTHANDLER_H #include +#include namespace Budget::OptHandlers{ class MainOptHandler { @@ -13,6 +14,16 @@ namespace Budget::OptHandlers{ explicit MainOptHandler(const std::vector &argv); void help(); + + private: + void accountOptHandler(std::string account); + + void createOptHandler(std::string account); + + void earnOptHandler(std::string account); + + void paymentOptHandler(std::string account); + const std::vector &argv; }; }