-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.h
120 lines (102 loc) · 3.75 KB
/
options.h
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
#pragma once
#include "utils.h"
#include <boost/program_options.hpp>
const boost::program_options::options_description &
getClientOptionsDescription() {
using namespace boost::program_options;
static options_description clientOptionsDescription("Client options");
static bool initialized = false;
if (!initialized) {
clientOptionsDescription.add_options()(
"help,h", "Display this help message"
)("gui-address,d", value<std::string>()->required(),
"The address of the GUI server"
)("player-name,n", value<std::string>()->required(),
"The name identifying you in the game"
)("port,p", value<port_t>()->required(),
"The port on which the client will be listening"
)("server-address,s", value<std::string>()->required(),
"The address of the game server");
initialized = true;
}
return clientOptionsDescription;
}
const boost::program_options::options_description &
getServerOptionsDescription() {
using namespace boost::program_options;
static options_description serverOptionsDescription("Server options");
static bool initialized = false;
if (!initialized) {
serverOptionsDescription.add_options()(
"help,h", "Display this help message"
)("bomb-timer,b", value<uint16_t>()->required(),
"The number of turns after which a bomb explodes"
)("players-count,c", value<uint16_t>()->required(), "The number of players"
)("turn-duration,d", value<uint64_t>()->required(),
"The duration of one turn in milliseconds"
)("explosion-radius,e", value<uint16_t>()->required(),
"The radius of explosions"
)("initial-blocks,k", value<uint16_t>()->required(),
"The initial number of blocks on the board"
)("game-length,l", value<uint16_t>()->required(),
"The length of the game in turns"
)("server-name,n", value<std::string>()->required(),
"The name of the server"
)("port,p", value<port_t>()->required(),
"The port on which the server will be listening"
)("seed,s", value<uint32_t>()->default_value(uint32_t(std::chrono::system_clock::now().time_since_epoch().count())),
"The seed to be used during randomization (default is 0)"
)("size-x,x", value<uint16_t>()->required(),
"The horizontal size of the board"
)("size-y,y", value<uint16_t>()->required(),
"The vertical size of the board");
initialized = true;
}
return serverOptionsDescription;
}
boost::program_options::variables_map parseOptions(
int argc, char ** argv,
const boost::program_options::options_description & optionsDescription
) {
using namespace boost::program_options;
variables_map options;
/* Parse the arguments, check argument validity. */
store(
command_line_parser(argc, argv).options(optionsDescription).run(), options
);
return options;
}
void notifyOptions(boost::program_options::variables_map & options) {
/* Finalize argument parsing, prepare argument values. */
boost::program_options::notify(options);
}
boost::program_options::variables_map handleOptions(
int argc, char ** argv,
const boost::program_options::options_description & optionsDescription
) {
boost::program_options::variables_map options;
/* Parse options. Check argument validity. */
try {
options = parseOptions(argc, argv, optionsDescription);
} catch (std::exception & e) {
throw RobotsException(
"Error: " + std::string(e.what()) + "\nRun " + std::string(argv[0]) +
" --help for usage.\n"
);
}
/* Print help message if requested. */
if (options.contains("help")) {
throw NeedHelp();
}
/* Finalize parsing, prepare argument values, including checking the
* existence of required arguments. */
try {
notifyOptions(options);
} catch (std::exception & e) {
throw RobotsException(
"Error: " + std::string(e.what()) + "\nRun " + std::string(argv[0]) +
" --help for usage.\n"
);
}
return options;
}