AccountOptHandler for parsing commandline options
This commit is contained in:
parent
5999a45e2b
commit
03d84ef9ee
@ -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)
|
@ -5,7 +5,6 @@
|
||||
#include "dateMoney.h"
|
||||
|
||||
|
||||
|
||||
bool DateMoney::operator<(const DateMoney &rhs) const {
|
||||
return mktime(date) < mktime(rhs.date);
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ public:
|
||||
DateMoney(const int *value, tm *date);
|
||||
|
||||
private:
|
||||
const int* value;
|
||||
const int *value;
|
||||
tm *date;
|
||||
};
|
||||
|
||||
|
13
src/main.cpp
13
src/main.cpp
@ -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")) {
|
||||
|
@ -21,8 +21,9 @@ public:
|
||||
Account();
|
||||
|
||||
int getValue();
|
||||
std::vector<DateMoney> getTimeline();
|
||||
|
||||
std::vector<DateMoney> getTimeline();
|
||||
|
||||
|
||||
private:
|
||||
std::list<Transaction> transactions;
|
||||
|
@ -12,8 +12,9 @@ public:
|
||||
const int value;
|
||||
|
||||
explicit Earning(int value, std::tm date);
|
||||
tm *getDate();
|
||||
|
||||
tm *getDate();
|
||||
|
||||
private:
|
||||
std::tm date;
|
||||
};
|
||||
|
@ -15,6 +15,6 @@ Receipt &Transaction::getReceipt() {
|
||||
return receipt;
|
||||
}
|
||||
|
||||
tm * Transaction::getDate() {
|
||||
tm *Transaction::getDate() {
|
||||
return &date;
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ public:
|
||||
|
||||
Receipt &getReceipt();
|
||||
|
||||
tm * getDate();
|
||||
tm *getDate();
|
||||
|
||||
|
||||
private:
|
||||
|
72
src/optHandlers/accountOptHandler.cpp
Normal file
72
src/optHandlers/accountOptHandler.cpp
Normal 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) {}
|
||||
|
41
src/optHandlers/accountOptHandler.h
Normal file
41
src/optHandlers/accountOptHandler.h
Normal 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
|
20
src/optHandlers/optHandler.cpp
Normal file
20
src/optHandlers/optHandler.cpp
Normal 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();
|
||||
}
|
33
src/optHandlers/optHandler.h
Normal file
33
src/optHandlers/optHandler.h
Normal 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
|
Loading…
x
Reference in New Issue
Block a user