-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
127 lines (120 loc) · 3.56 KB
/
config.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "config.hpp"
#include <cstdint>
#include <format>
#include <iostream>
#include <map>
#include <optional>
#include <string>
#include <getopt.h>
#include <unistd.h>
#include "functions.hpp"
#include "status_manager.hpp"
#include "status_writing.hpp"
static struct option long_options[] = {
{"no-output", no_argument, nullptr, 'n'},
{"no-fork", no_argument, nullptr, 'f'},
{"nslots", required_argument, nullptr, 'N'},
{"separate-stderr", no_argument, nullptr, 'E'},
{"label", required_argument, nullptr, 'L'},
{"list", no_argument, nullptr, 'l'},
{"info", required_argument, nullptr, 'i'},
{"db-path", no_argument, nullptr, 0},
{"gh-summary", no_argument, nullptr, 0},
{"list-failed", no_argument, nullptr, 0},
{"list-queued", no_argument, nullptr, 0},
{"list-running", no_argument, nullptr, 0},
{"stdout", required_argument, nullptr, 'o'},
{"stderr", required_argument, nullptr, 'e'},
{"rerun", required_argument, nullptr, 'r'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}};
namespace tsp {
Config::Config(int argc, char *argv[])
: bool_vars{{"disappear_output", false},
{"do_fork", true},
{"separate_stderr", false}},
int_vars{
{"nslots", 1},
{"rerun", -1},
},
str_vars{} {
if (argc == 1) {
do_action(Action::list);
}
opterr = 0;
int c;
int option_index;
while ((c = getopt_long(argc, argv, "+nfL:N:Ei:lho:e:r:", long_options,
&option_index)) != -1) {
switch (c) {
case 'n':
// Pipe stdout and stderr of forked process to /dev/null
bool_vars["disappear_output"] = true;
break;
case 'f':
bool_vars["do_fork"] = false;
break;
case 'N':
int_vars["nslots"] = std::stoul(optarg);
break;
case 'E':
bool_vars["separate_stderr"] = true;
break;
case 'L':
str_vars["category"] = std::string{optarg};
break;
case 'r':
int_vars["rerun"] = std::stoul(optarg);
break;
case 'i':
do_action(Action::info, std::stoul(optarg));
break;
case 'o':
do_action(Action::stdout, std::stoul(optarg));
break;
case 'e':
do_action(Action::stderr, std::stoul(optarg));
break;
case 'l':
do_action(Action::list);
break;
case 'h':
std::cout << std::format(help, argv[0]) << std::endl;
exit(EXIT_SUCCESS);
break;
case '?':
switch (optopt) {
case 'i':
do_action(Action::info);
break;
case 'o':
do_action(Action::stdout);
break;
case 'e':
do_action(Action::stderr);
break;
}
case 0:
if (std::string{"db-path"} == long_options[option_index].name) {
std::cout << (get_tmp() / db_name).string() << std::endl;
exit(EXIT_SUCCESS);
}
if (std::string{"gh-summary"} == long_options[option_index].name) {
do_action(Action::github_summary);
}
if (std::string{"list-failed"} == long_options[option_index].name) {
do_action(Action::list_failed);
}
if (std::string{"list-queued"} == long_options[option_index].name) {
do_action(Action::list_queued);
}
if (std::string{"list-running"} == long_options[option_index].name) {
do_action(Action::list_running);
}
}
};
}
int32_t Config::get_int(std::string key) { return int_vars[key]; };
std::string Config::get_string(std::string key) { return str_vars[key]; };
bool Config::get_bool(std::string key) { return bool_vars[key]; };
} // namespace tsp