forked from christophgysin/addp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.cpp
164 lines (129 loc) · 4.93 KB
/
options.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "options.h"
#include <addp/types.h>
#include <iostream>
namespace addpc {
options::options(int argc, char *argv[]) {
_usage = std::string() + "Usage: %s [options...] <action> [args...]\n" + "\n" + " actions:\n" +
" discover [device]\n" + " reboot <device> [passwd]\n" +
" config <device> <ip> <netmask> <gateway> [passwd]\n" +
" dhcp <device> <on|off> [passwd]\n";
opt_parse(argc, argv);
}
void options::opt_parse(int argc, char *argv[]) {
addp::options::parse(argc, argv);
if (_vm.count("action") == 0) {
std::cerr << "No action given" << std::endl << std::endl;
usage();
std::exit(1);
}
/*
* discover [device]
* reboot <device> [passwd]
* config <device> <ip> <netmask> <gateway> [passwd]
* dhcp <device> <on|off> [passwd]
*/
size_t min = 0;
size_t max = 0;
const std::string &action = this->action();
if (action == "discover") {
min = 0;
max = 0;
} else {
// must have a specific device address for further actions
if (addp::parse_mac_str(mac()) == addp::MAC_ADDR_BROADCAST) {
std::cerr << "Please select a device's mac address" << std::endl << std::endl;
usage();
std::exit(1);
}
if (action == "reboot") {
min = 0;
max = 1;
} else if (action == "config") {
min = 3;
max = 4;
} else if (action == "dhcp") {
min = 1;
max = 2;
} else {
std::cerr << "Unknown action \"" << action << "\"" << std::endl << std::endl;
usage();
std::exit(1);
}
}
size_t count = args().size();
if (count < min || count > max) {
usage();
std::exit(1);
}
// optional password is always the last argument
_password_index = max - 1;
}
boost::program_options::options_description options::addpc_options() const {
const std::string usage = "ADDP client options";
boost::program_options::options_description addpc_opts(usage);
addpc_opts.add_options()("listen,L",
boost::program_options::value<std::string>()->default_value("0.0.0.0"),
"ip address to listen")(
"timeout,t", boost::program_options::value<size_t>()->default_value(addp::DEFAULT_TIMEOUT),
"response timeout (in ms)")(
"max_count,c",
boost::program_options::value<size_t>()->default_value(addp::DEFAULT_MAX_COUNT),
"stop after receiving n responses");
return addpc_opts;
}
boost::program_options::options_description options::addpc_hidden_options() const {
boost::program_options::options_description hidden_opts;
hidden_opts.add_options()("action", boost::program_options::value<std::string>(),
"action (discover/static/reboot/dhcp)")(
"mac", boost::program_options::value<std::string>()->default_value("ff:ff:ff:ff:ff:ff"),
"mac address of target device")("args",
boost::program_options::value<std::vector<std::string>>()
->default_value(std::vector<std::string>(), "")
->multitoken(),
"action arguments");
return hidden_opts;
}
boost::program_options::options_description options::visible_options() const {
boost::program_options::options_description opts = addp::options::all_options();
opts.add(addpc_options());
return opts;
}
boost::program_options::options_description options::all_options() const {
boost::program_options::options_description opts = addp::options::all_options();
opts.add(addpc_options());
opts.add(addpc_hidden_options());
return opts;
}
boost::program_options::positional_options_description options::positional_options() const {
boost::program_options::positional_options_description positional =
addp::options::positional_options();
positional.add("action", 1).add("mac", 1).add("args", -1);
return positional;
}
std::string options::listen() const { return _vm["listen"].as<std::string>(); }
size_t options::timeout() const { return _vm["timeout"].as<size_t>(); }
size_t options::max_count() const { return _vm["max_count"].as<size_t>(); }
std::string options::action() const { return _vm["action"].as<std::string>(); }
std::string options::mac() const { return _vm["mac"].as<std::string>(); }
std::vector<std::string> options::args() const {
return _vm["args"].as<std::vector<std::string>>();
}
std::string options::password() const {
if (args().size() <= _password_index)
return addp::DEFAULT_PASSWORD;
return args()[_password_index];
}
std::string options::ip() const { return args()[0]; }
std::string options::subnet() const { return args()[1]; }
std::string options::gateway() const { return args()[2]; }
bool options::dhcp() const {
std::string value = args()[0];
if (value == "on")
return true;
if (value == "off")
return false;
std::cerr << "illegal value for dhcp: \"" << value << "\"" << std::endl << std::endl;
usage();
std::exit(1);
}
} // namespace addpc