mainOptHandler handles all paths

This commit is contained in:
Quentin Snow 2023-01-15 19:37:49 -06:00
parent 365b24e651
commit 94e63e8f76
4 changed files with 256 additions and 27 deletions

View File

@ -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=<FlOAT> Value for earning.
-d --description=<STRING> Description for earning.

View File

@ -25,6 +25,6 @@ void createRequiredFolders() {
int main(int argc, char *argv[]) {
std::vector<char *> args(argv, argv + argc);
OptHandlers::MainOptHandler moh(args);
return 0;
}

View File

@ -3,11 +3,228 @@
//
#include "mainOptHandler.h"
#include <iostream>
#include <getopt.h>
using namespace Budget::OptHandlers;
MainOptHandler::MainOptHandler(const std::vector<char *> &argv) {
MainOptHandler::MainOptHandler(const std::vector<char *> &_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 <action> [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=<FlOAT>\t\tValue for earning." << std::endl;
std::cout << "\t-d --description=<STRING>\tDescription for earning." << std::endl;
std::cout << "\t-r --receipt=<PATH>\t\tPath to file to store in DB as receipt." << std::endl;
std::cout << "\t-D --date=<dd/mm/yyyy>\tDate as dd/mm/yyyy. Default will be today." << std::endl;
std::cout << "\t-t --time=<hh/mm/ss>\t\t24 hour time as hh/mm/ss. Default will be now." << std::endl;
std::cout << "\t-v --value=<FlOAT> Value for earning." << std::endl;
std::cout << "\t-d --description=<STRING> Description for earning." << std::endl;
std::cout << "\t-r --receipt=<PATH> Path to file to store in DB as receipt." << std::endl;
std::cout << "\t-D --date=<dd/mm/yyyy> Date as dd/mm/yyyy. Default will be today." << std::endl;
std::cout << "\t-t --time=<hh/mm/ss> 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=<FlOAT>\t\tValue for payment." << std::endl;
std::cout << "\t-d --description=<STRING>\tDescription for payment." << std::endl;
std::cout << "\t-r --receipt=<PATH>\t\tPath to file to store in DB as receipt." << std::endl;
std::cout << "\t-D --date=<NUM/NUM/NUM>\t\tDate as dd/mm/yyyy. Default will be today." << std::endl;
std::cout << "\t-t --time=<hh/mm/ss>\t\t24 hour time as hh/mm/ss. Default will be now." << std::endl;
std::cout << "\t-v --value=<FlOAT> Value for payment." << std::endl;
std::cout << "\t-d --description=<STRING> Description for payment." << std::endl;
std::cout << "\t-r --receipt=<PATH> Path to file to store in DB as receipt." << std::endl;
std::cout << "\t-D --date=<NUM/NUM/NUM> Date as dd/mm/yyyy. Default will be today." << std::endl;
std::cout << "\t-t --time=<hh/mm/ss> 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;

View File

@ -6,6 +6,7 @@
#define BUDGET_MAINOPTHANDLER_H
#include <vector>
#include <string>
namespace Budget::OptHandlers{
class MainOptHandler {
@ -13,6 +14,16 @@ namespace Budget::OptHandlers{
explicit MainOptHandler(const std::vector<char *> &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<char*> &argv;
};
}