-
Notifications
You must be signed in to change notification settings - Fork 2
/
commandline.cpp
65 lines (59 loc) · 2.01 KB
/
commandline.cpp
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
#include <ssod/commandline.h>
#include <getopt.h>
#include <unistd.h>
#include <ssod/ssod.h>
#include <iostream>
#include <cstdlib>
namespace commandline {
commandline_config parse(int argc, char const *argv[]) {
struct option long_opts[] = {
{ "clusterid", required_argument, nullptr, 'c' },
{ "maxclusters", required_argument, nullptr, 'm' },
{ "showcommands", optional_argument, nullptr, 's' },
{ nullptr, 0, nullptr, 0 }
};
int index{0};
int arg;
bool clusters_defined{false}, show_commands{false};
uint32_t cluster_id{0};
uint32_t max_clusters{1};
/**
* BIG FAT WARNING: https://nullprogram.com/blog/2014/10/12/
* getopt_long_only maybe not thread safe. This is only called before the bot is initialised, so
* it's generally "ok-ish".
*/
opterr = 0;
while ((arg = getopt_long_only(argc, (char* const*)argv, "", long_opts, &index)) != -1) {
switch (arg) {
case 0:
break;
case 'c':
/* Cluster id */
cluster_id = atoi(optarg);
clusters_defined = true;
break;
case 'm':
/* Number of clusters */
max_clusters = atoi(optarg);
break;
case 's':
/* Number of clusters */
show_commands = true;
break;
case '?':
default:
std::cerr << "Unknown parameter '" << argv[optind - 1] << "'\n";
std::cerr << "Usage: " << argv[0] << " [-clusterid <n>] [-maxclusters <n>]\n\n";
std::cerr << "-clusterid <n>: The current cluster id to identify for, must be set with -maxclusters\n";
std::cerr << "-maxclusters <n>: The maximum number of clusters the bot is running, must be set with -clusterid\n";
std::cerr << "-showcommands: Output JSON definitions of all application commands\n";
exit(1);
}
}
if (clusters_defined && max_clusters == 0) {
std::cerr << "ERROR: You have defined a cluster id with -clusterid but no cluster count with -maxclusters.\n";
exit(2);
}
return commandline_config{.cluster_id = cluster_id, .max_clusters = max_clusters, .display_commands = show_commands};
}
}