-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.cpp
More file actions
73 lines (64 loc) · 2.04 KB
/
book.cpp
File metadata and controls
73 lines (64 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Main file for BookTracker CLI application.
* Handles command-line argument parsing and command execution.
*
* Author: Harry Bevins
*/
#include "src/book.h"
#include "src/commands.h"
#include "src/database.h"
#include "src/plot.h"
#include "src/stats.h"
#include "src/version.h"
#include "src/help.h"
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
/* Main function for BookTracker CLI application.
* Parses command-line arguments and executes corresponding commands.
* Params:
* argc - number of command-line arguments
* argv - array of command-line argument strings
*/
const vector<string> allowed_commands{"add", "ls", "list", "del",
"delete", "mod", "modify", "help",
"show", "version", "plot", "stats"};
string command = argv[1];
if (find(allowed_commands.begin(), allowed_commands.end(), command) ==
allowed_commands.end()) {
cout << "Unknown command: " << command << endl;
return 1;
}
string homeDir = getenv("HOME");
vector<Book> books = loadBooks(homeDir);
vector<int> ids;
for (Book b : books) {
ids.push_back(b.id);
}
int maxId = ids.size() > 0 ? *max_element(ids.begin(), ids.end()) : 0;
if (command == "add") {
addBook(argv[2], maxId);
} else if (command == "delete" || command == "del") {
int id = stoi(argv[2]);
deleteBook(id, books);
} else if (command == "list" || command == "ls") {
list(books);
} else if (command == "show") {
int id = stoi(argv[2]);
showBook(id, books);
} else if (command == "mod" || command == "modify") {
int id = stoi(argv[2]);
modifyBook(id, argv[3], argv[4], books);
} else if (command == "help") {
displayHelp();
} else if (command == "version") {
cout << "BookTracker version " << BOOKTRACKER_VERSION << endl;
} else if (command == "plot") {
plot(books);
} else if (command == "stats") {
stats(books);
}
return 0;
}