-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (92 loc) · 2.15 KB
/
main.cpp
File metadata and controls
105 lines (92 loc) · 2.15 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <nlohmann/json.hpp>
#include <getopt.h>
#include "collection.h"
using std::string;
using namespace std::literals;
static void usage()
{
auto criteria_list = std::accumulate(next(Collection::sort_keys.begin()),
Collection::sort_keys.end(),
*Collection::sort_keys.begin(), [](auto a, auto b) { return a + ", " + b; });
std::cout << "Usage: cppdiscogs [options]\n"
"Options:\n"
" -t, --token <token> Discogs API token\n"
" -u, --user <user> Discogs user name\n"
" -l, --list-by <"+criteria_list+"> List criteria\n"
" -d, --dump-collection Dump collection\n";
}
int main(int argc, char *argv[])
{
// Parse command line arguments with getpot
option long_options[] = {
{"token", required_argument, 0, 't'},
{"user", required_argument, 0, 'u'},
// List collection by: titles, artists, years
{"list-by", required_argument, 0, 'l'},
// Dump collection
{"dump-collection", no_argument, 0, 'd'},
{0, 0, 0, 0}
};
auto token = "";
auto user = ""s;
auto criteria = ""s;
auto dump_collection = false;
while (1) {
int option_index = 0;
int c = getopt_long(argc, argv, "t:u:l:d", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 't':
token = optarg;
break;
case 'u':
user = optarg;
break;
case 'l':
criteria = optarg;
if (!Collection::sort_keys.contains(criteria))
{
std::cout << "Invalid criteria: " << criteria << "\n";
usage();
return 1;
}
break;
case 'd':
dump_collection = true;
break;
default:
usage();
return 1;
}
}
if (user.empty())
{
usage();
return 1;
}
auto client = Collection(token, user, criteria);
if (!client.init())
{
std::cout << "Failed to initialize client\n";
return 1;
}
if (dump_collection)
{
std::cout << "Parsed JSON: " << std::setw(4) << client.getJson() << "\n";
return 0;
}
do
{
printf("\nType `Enter` for next page (%d) or `q` to quit: ", client.getCurrentPage());
std::string line;
std::getline(std::cin, line);
if (line == "q")
break;
for (auto title : client.listReleases())
std::cout << title << std::endl;
}
while (client.downloadNextPage());
return 0;
}