11 void failWith(
const std::string& msg,
const char* prog)
13 std::cerr << msg <<
"\nTry '" << prog <<
" --help' for more information.\n";
20 clap::App app(argv[0],
"Zappy server");
22 auto& port = app.option<
int>(
"-p",
"Listening port").required();
23 auto& width = app.option<
int>(
"-x",
"Map width").required();
24 auto& height = app.option<
int>(
"-y",
"Map height").required();
25 auto& teams = app.multi_option<std::string>(
"-n",
"Team names").required();
26 auto& clientsNb = app.option<
int>(
"-c",
"Clients per team (default: 10)").default_value(10);
27 auto& freq = app.option<
int>(
"-f",
"Time unit frequency (default: 100)").default_value(100);
28 auto& seed = app.option<
int>(
"-s,--seed",
"RNG seed for reproducible games (default: random)")
38 app.parse(argc, argv);
39 }
catch (
const clap::HelpRequested&) {
42 }
catch (
const clap::ClapException& e) {
43 failWith(e.what(), argv[0]);
48 auto names = teams.get();
49 if (std::find(names.begin(), names.end(),
"GRAPHIC") != names.end()) {
50 failWith(
"Team name 'GRAPHIC' is reserved and cannot be used.", argv[0]);
55 int p = port.get(), w = width.get(), h = height.get();
56 int c = clientsNb.get(), f = freq.get(), s = seed.get();
58 if (p < 1 || p > 65535) {
59 failWith(
"Port must be between 1 and 65535.", argv[0]);
64 failWith(
"Map dimensions must be positive.", argv[0]);
69 failWith(
"Clients per team must be positive.", argv[0]);
74 failWith(
"Frequency must be positive.", argv[0]);
85 config.
seed = (s < 0) ? std::random_device{}() :
static_cast<unsigned int>(s);
int exitCode() const
Gets the exit code for the application.
static constexpr int SUCCESS
Args(int argc, char **argv)
ServerConfig getConfig() const
Gets the configuration for the server application.
static constexpr int ERROR
bool isHelpRequested() const
Checks if the help flag was provided in the arguments.
bool isValid() const
Checks if the parsed arguments are valid and if the application should run.
void failWith(const std::string &msg, const char *prog)
Represents the configuration for the server application, described by:
std::vector< std::string > teamNames