AccountOptHandler for parsing commandline options

This commit is contained in:
quentin 2022-08-13 16:35:01 -05:00
parent 5999a45e2b
commit 03d84ef9ee
12 changed files with 187 additions and 9 deletions

View File

@ -10,4 +10,6 @@ add_executable(${PROJECT_NAME} src/main.cpp src/main.h
src/data/data.tpp src/data/data.h
src/data/accountData.cpp src/data/accountData.h
src/money/earning.cpp src/money/earning.h
src/data/dateMoney.cpp src/data/dateMoney.h)
src/data/dateMoney.cpp src/data/dateMoney.h
src/optHandlers/optHandler.cpp src/optHandlers/optHandler.h
src/optHandlers/accountOptHandler.cpp src/optHandlers/accountOptHandler.h)

View File

@ -5,7 +5,6 @@
#include "dateMoney.h"
bool DateMoney::operator<(const DateMoney &rhs) const {
return mktime(date) < mktime(rhs.date);
}

View File

@ -21,7 +21,7 @@ public:
DateMoney(const int *value, tm *date);
private:
const int* value;
const int *value;
tm *date;
};

View File

@ -4,13 +4,16 @@
#include "main.h"
#include "data/accountData.h"
#include "optHandlers/accountOptHandler.h"
#include <pwd.h>
#include <unistd.h>
#include <string>
#include <filesystem>
#include <list>
#include <iostream>
#include <getopt.h>
using namespace Budget;
const std::string homeDirectory = getpwuid(getuid())->pw_dir;
@ -21,8 +24,14 @@ void createRequiredFolders() {
std::filesystem::create_directories(homeDirectory + "/.local/share/budget/receipts");
}
int main() {
int main(int argc, char *argv[]) {
std::vector<char *> args(argv, argv + argc);
OptHandlers::AccountOptHandler accountOptHandler(args);
accountOptHandler.parse();
createRequiredFolders();
// Read all accounts saved and store them in accounts
std::list<AccountData> accounts;
for (const auto &file : std::filesystem::directory_iterator(
homeDirectory + "/.local/share/budget/accounts")) {

View File

@ -21,8 +21,9 @@ public:
Account();
int getValue();
std::vector<DateMoney> getTimeline();
std::vector<DateMoney> getTimeline();
private:
std::list<Transaction> transactions;

View File

@ -12,8 +12,9 @@ public:
const int value;
explicit Earning(int value, std::tm date);
tm *getDate();
tm *getDate();
private:
std::tm date;
};

View File

@ -15,6 +15,6 @@ Receipt &Transaction::getReceipt() {
return receipt;
}
tm * Transaction::getDate() {
tm *Transaction::getDate() {
return &date;
}

View File

@ -17,7 +17,7 @@ public:
Receipt &getReceipt();
tm * getDate();
tm *getDate();
private:

View File

@ -0,0 +1,72 @@
//
// Created by quentin on 8/13/22.
//
#include <iostream>
#include "accountOptHandler.h"
using namespace Budget::OptHandlers;
void AccountOptHandler::parse() {
std::string test;
struct option longOpts[] = {
{"help", no_argument, nullptr, 'h'},
{"list", no_argument, nullptr, 'l'},
{"delete", required_argument, nullptr, 'd'},
{"create", required_argument, nullptr, 'c'},
{"value", required_argument, nullptr, 'v'},
{nullptr}
};
while (true) {
int opt = getopt_long(getArgc(), getArgv(), "hl::d::c::v", longOpts, nullptr);
if (opt == -1) {
break;
}
switch (opt) {
case 'h':
setOpts.help = true;
break;
case 'l':
setOpts.list = true;
break;
case 'd':
setOpts.del = true;
setOpts.delAccount = optarg;
break;
case 'c':
setOpts.create = true;
setOpts.createAccount = optarg;
break;
case 'v':
setOpts.value = true;
setOpts.valueAccount = optarg;
break;
case '?':
setOpts.help = true;
setOpts.helpOut = stderr;
break;
default:
break;
}
}
}
void AccountOptHandler::help() {
fprintf(setOpts.helpOut, "Help budget -a {-cvd account|-hl}\n");
fprintf(setOpts.helpOut, " -h --help Output this message.\n");
fprintf(setOpts.helpOut, " -l --list List available accounts.\n");
fprintf(setOpts.helpOut, " -d --delete account Delete the specified account.\n");
fprintf(setOpts.helpOut, " -c --create account Create a new account.\n");
fprintf(setOpts.helpOut, " -v --value account Print the current value of account.\n");
}
const SetOpts *AccountOptHandler::getSetOpts() const {
return &setOpts;
}
AccountOptHandler::AccountOptHandler(const std::vector<char *> &argv) : OptHandler(argv) {}

View File

@ -0,0 +1,41 @@
//
// Created by quentin on 8/13/22.
//
#ifndef BUDGET_ACCOUNTOPTHANDLER_H
#define BUDGET_ACCOUNTOPTHANDLER_H
#include "optHandler.h"
namespace Budget::OptHandlers {
class AccountOptHandler : public OptHandler {
struct SetOpts {
FILE *helpOut = stdout;
bool help = false;
bool list = false;
bool del = false;
char *delAccount{};
bool create = false;
char *createAccount{};
bool value = false;
char *valueAccount{};
};
public:
explicit AccountOptHandler(const std::vector<char *> &argv);
void parse() override;
void help() override;
[[nodiscard]] const SetOpts *getSetOpts() const;
private:
SetOpts setOpts;
};
}
#endif //BUDGET_ACCOUNTOPTHANDLER_H

View File

@ -0,0 +1,20 @@
//
// Created by quentin on 8/13/22.
//
#include "optHandler.h"
#include <utility>
using namespace Budget::OptHandlers;
OptHandler::OptHandler(std::vector<char *> argv) : argv(std::move(argv)) {}
int OptHandler::getArgc() {
return argv.size();
}
char **OptHandler::getArgv() {
return argv.data();
}

View File

@ -0,0 +1,33 @@
//
// Created by quentin on 8/13/22.
//
#ifndef BUDGET_OPTHANDLER_H
#define BUDGET_OPTHANDLER_H
#include <getopt.h>
#include <cstdio>
#include <string>
#include <vector>
namespace Budget::OptHandlers {
class OptHandler {
public:
explicit OptHandler(std::vector<char *> argv);
virtual void parse() = 0;
int getArgc();
char **getArgv();
private:
std::vector<char *> argv;
virtual void help() = 0;
};
}
#endif //BUDGET_OPTHANDLER_H