-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
119 lines (106 loc) · 3.95 KB
/
Copy pathmain.cpp
File metadata and controls
119 lines (106 loc) · 3.95 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
106
107
108
109
110
111
112
113
114
115
116
117
/* main.cpp */
#include "Commands.h"
#include "json_utils.h"
void verbose_print(const std::string& msg, bool verbose) {
if (verbose) std::cout << BLUE << "VERBOSE: " << RESET << msg << std::endl;
}
// WARNING: DO NOT USE TABS!
// using tabs will not align the descriptions correctly.
// use spaces instead.
void help_command() {
std::cout <<
R"(USAGE: rsc [OPTIONS] [COMMAND]
Robust Server Controller (rsc)
provides simple functionallity to save server information and automatically find IPv4 addresses.
COMMANDS:
$ rsc # lists all saved server information.
$ rsc --addservers # adds servers from LAN.
$ rsc --addipv4 # adds a server by its IPv4 address.
$ rsc --remove <HOSTNAME> # removes <HOSTNAME> from the saved servers list.
$ rsc --changename <HOSTNAME> --newname <NEWNAME> # changes <HOSTNAME> to <NEWNAME>.
FLAGS:
-h, --help help for rsc.
-v, --verbose provides verbose output for rsc.
-a, --addservers add servers from online servers on LAN.
-rm, --remove remove a saved server profile.
-cn, --changename changes a profiles name.
DESCRIPTION:
rsc is a sever controller written in c++. it provides simple functionallity
for managing LAN servers whose addresses may change due to a NAT.)" << std::endl;
}
bool verbose = false;
namespace po = boost::program_options;
int main(int argc, char *argv[]) {
try {
AddServers add;
ControlWorker cmd;
Commands update;
po::options_description desc("options");
desc.add_options()
("help,h", "produce help messege")
("verbose,v", "run in verbose mode")
("addservers,a", "add new servers")
("changename,cn", po::value<std::string>(), "change a hosts name")
("newname", po::value<std::string>(), "new host name")
("addipv4", "add a host by IPv4")
("remove,rm", po::value<std::string>(), "removes a server");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
help_command();
return 0;
}
if (vm.count("verbose")) {
verbose = true;
}
if (vm.count("changename") && vm.count("newname")) {
std::string name = vm["changename"].as<std::string>();
std::string newName = vm["NEW-NAME"].as<std::string>();
update.changeHostName(name, newName);
} else if (vm.count("changename") && !vm.count("newname")) {
throw std::invalid_argument("Invalid argument: forgot to input the new name, --newname <name>");
} else if (!vm.count("changename") && vm.count("newname")) {
throw std::invalid_argument("Invalid argument: what new name? usage: --changename <old_name> --newname <name>");
}
if (vm.count("addipv4")) {
add.SaveByIPv4();
return 0;
}
if (!vm.count("addservers") && !vm.count("remove")) {
cmd.Start();
} else if(vm.count("addservers") && !vm.count("remove")) {
add.Start();
return 0;
} else if(!vm.count("addservers") && vm.count("remove")) {
std::string name = vm["remove"].as<std::string>();
update.RemoveHost(name);
return 0;
} else {
throw std::invalid_argument("Invalid argument");
}
}
catch (std::invalid_argument &e) {
std::cout << RED <<"[rsc::main()] INVALID ARGUEMENT ERROR: " << RESET << e.what() << std::endl;
help_command();
return 0;
}
catch(boost::wrapexcept<boost::program_options::invalid_command_line_syntax> const& e) {
std::cout << RED <<"[rsc::main()] INVALID ARGUEMENT ERROR: " << RESET << e.what() << std::endl;
help_command();
return 0;
}
catch (std::runtime_error &e) {
std::cout << RED << "[rsc::main()] RUNTIME ERROR: " << RESET << e.what() << std::endl;
return 0;
}
catch(po::error const& e) {
std::cout << RED << "[rsc::main()] CLI ARGUEMENT PARSER ERROR: " << RESET << e.what() << std::endl;
return 0;
}
catch(std::exception &e) {
std::cout << RED << "[rsc::main()] UNKOWN ERROR: " << RESET << e.what() << std::endl;
return 0;
}
return 0;
}