-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCLIParser.hpp
More file actions
339 lines (321 loc) · 12.1 KB
/
Copy pathCLIParser.hpp
File metadata and controls
339 lines (321 loc) · 12.1 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright 2025 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <csignal>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <fastdds/dds/log/Log.hpp>
#pragma once
class CLIParser
{
public:
CLIParser() = delete;
struct example_config
{
std::string config_file_path = "";
std::string action_name = "fibonacci";
bool announce_server = true;
uint32_t timeout = 30;
std::string persistence_path = "";
std::string goals_path = "";
uint32_t expected_requests = 0;
uint32_t request_initial_wait = 0;
bool cancel_requests = false;
};
/**
* @brief Print usage help message and exit with the given return code
*
* @param return_code return code to exit with
*
* @warning This method finishes the execution of the program with the input return code
*/
static void print_help(
uint8_t return_code)
{
std::cout << "Usage: ddsenabler_example_action <client|server> [options]"
<< std::endl;
std::cout << ""
<< std::endl;
std::cout << "--config <str> Path to the configuration file"
<< std::endl;
std::cout << " (optional, if not provided default config is used)"
<< std::endl;
std::cout << "--action-name <str> Name of the action to be registered"
<< std::endl;
std::cout << " (Default: 'fibonacci')"
<< std::endl;
std::cout << "--timeout <num> Time (seconds) to wait before stopping the"
<< std::endl;
std::cout << " program if expectations are not met"
<< std::endl;
std::cout << " (Default: 30)"
<< std::endl;
std::cout << "--persistence-path <str> Path to the persistence directory"
<< std::endl;
std::cout << " (No default value as it is required)"
<< std::endl;
std::cout << "\n-------------------------------------SERVER OPTIONS------------------------------------\n"
<< std::endl;
std::cout << "--expected-requests <num> Number of requests expected to be received"
<< std::endl;
std::cout << " (Default: 0, meaning infinite)"
<< std::endl;
std::cout << "\n-------------------------------------CLIENT OPTIONS------------------------------------\n"
<< std::endl;
std::cout << "--request-initial-wait <num> Time (seconds) to wait before starting"
<< std::endl;
std::cout << " requests publication since server matching"
<< std::endl;
std::cout << " (Default: 0)"
<< std::endl;
std::cout << "--cancel-requests <bool> Whether to cancel requests after sending them"
<< std::endl;
std::cout << " (Default: false)"
<< std::endl;
std::cout << "--goals-path <str> Directory containing goal JSON files"
<< std::endl;
std::cout << " (No default value as it is required for client mode)"
<< std::endl;
std::exit(return_code);
}
/**
* @brief Parse the command line options and return the configuration_config object
*
* @param argc number of arguments
* @param argv array of arguments
* @return configuration_config object with the parsed options
*
* @warning This method finishes the execution of the program if the input arguments are invalid
*/
static example_config parse_cli_options(
int argc,
char* argv[])
{
example_config config;
if (argc < 2)
{
std::cerr << "Mode is required as the first argument: 'client' or 'server'" << std::endl;
print_help(EXIT_FAILURE);
}
bool client_flag = false;
bool server_flag = false;
// First positional argument must be the mode
std::string mode = argv[1];
if (mode == "-h" || mode == "--help")
{
print_help(EXIT_SUCCESS);
}
else if (mode == "client")
{
client_flag = true;
config.announce_server = false;
}
else if (mode == "server")
{
server_flag = true;
config.announce_server = true;
}
else
{
std::cerr << "Invalid mode '" << mode << "'. First argument must be 'client' or 'server'" << std::endl;
print_help(EXIT_FAILURE);
}
// Parse optional arguments starting from index 2
for (int i = 2; i < argc; ++i)
{
std::string arg = argv[i];
if (arg == "-h" || arg == "--help")
{
print_help(EXIT_SUCCESS);
}
else if (arg == "--config")
{
if (++i < argc)
{
config.config_file_path = argv[i];
if (!std::filesystem::exists(config.config_file_path))
{
std::cerr << "Invalid configuration file path: " << config.config_file_path << std::endl;
print_help(EXIT_FAILURE);
}
}
else
{
std::cerr << "Failed to parse --config argument" << std::endl;
print_help(EXIT_FAILURE);
}
}
else if (arg == "--action-name")
{
if (++i < argc)
{
config.action_name = argv[i];
}
else
{
std::cerr << "Failed to parse --action-name argument" << std::endl;
print_help(EXIT_FAILURE);
}
}
else if (arg == "--timeout")
{
if (++i < argc)
{
try
{
config.timeout = static_cast<uint32_t>(std::stoi(argv[i]));
}
catch (const std::exception& e)
{
std::cerr << "Invalid --timeout argument " << argv[i] << ": " << e.what() << std::endl;
print_help(EXIT_FAILURE);
}
}
else
{
std::cerr << "Failed to parse --timeout argument" << std::endl;
print_help(EXIT_FAILURE);
}
}
else if (arg == "--persistence-path")
{
if (++i < argc)
{
config.persistence_path = argv[i];
}
else
{
std::cerr << "Failed to parse --persistence-path argument" << std::endl;
print_help(EXIT_FAILURE);
}
}
else if (arg == "--expected-requests")
{
if (++i < argc)
{
try
{
config.expected_requests = static_cast<uint32_t>(std::stoi(argv[i]));
}
catch (const std::exception& e)
{
std::cerr << "Invalid --expected-requests argument " << argv[i] << ": " << e.what()
<< std::endl;
print_help(EXIT_FAILURE);
}
}
else
{
std::cerr << "Failed to parse --expected-requests argument" << std::endl;
print_help(EXIT_FAILURE);
}
}
else if (arg == "--request-initial-wait")
{
if (++i < argc)
{
try
{
config.request_initial_wait = static_cast<uint32_t>(std::stoi(argv[i]));
}
catch (const std::exception& e)
{
std::cerr << "Invalid --request-initial-wait argument " << argv[i] << ": " << e.what()
<< std::endl;
print_help(EXIT_FAILURE);
}
}
else
{
std::cerr << "Failed to parse --request-initial-wait argument" << std::endl;
print_help(EXIT_FAILURE);
}
}
else if (arg == "--cancel-requests")
{
if (++i < argc)
{
config.cancel_requests = (std::string(argv[i]) == "true") ||
(std::string(argv[i]) == "True") ||
(std::string(argv[i]) == "1");
}
else
{
std::cerr << "Failed to parse --cancel-requests argument" << std::endl;
print_help(EXIT_FAILURE);
}
}
else if (arg == "--goals-path")
{
if (++i < argc)
{
config.goals_path = argv[i];
if (!std::filesystem::exists(config.goals_path) ||
!std::filesystem::is_directory(config.goals_path))
{
std::cerr << "Invalid --goals-path directory: " << config.goals_path << std::endl;
print_help(EXIT_FAILURE);
}
}
else
{
std::cerr << "Failed to parse --goals-path argument" << std::endl;
print_help(EXIT_FAILURE);
}
}
else
{
std::cerr << "Failed to parse unknown argument: " << arg << std::endl;
print_help(EXIT_FAILURE);
}
}
// Sanity check (should be set from the positional mode)
if (!client_flag && !server_flag)
{
std::cerr << "Either 'client' or 'server' must be specified as the first argument" << std::endl;
print_help(EXIT_FAILURE);
}
if (client_flag && config.goals_path.empty())
{
std::cerr << "--goals-path is required in client mode" << std::endl;
print_help(EXIT_FAILURE);
}
return config;
}
/**
* @brief Parse the signal number into the signal name
*
* @param signum signal number
* @return std::string signal name
*/
static std::string parse_signal(
const int& signum)
{
switch (signum)
{
case SIGINT:
return "SIGINT";
case SIGTERM:
return "SIGTERM";
#ifndef _WIN32
case SIGQUIT:
return "SIGQUIT";
case SIGHUP:
return "SIGHUP";
#endif // _WIN32
default:
return "UNKNOWN SIGNAL";
}
}
};