Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
Args.cpp
Go to the documentation of this file.
1#include "Args.hpp"
2
3#include <algorithm>
4#include <iostream>
5#include <random>
6
7#include "clap/App.hpp"
8
9namespace {
10
11 void failWith(const std::string& msg, const char* prog)
12 {
13 std::cerr << msg << "\nTry '" << prog << " --help' for more information.\n";
14 }
15
16} // namespace
17
18Args::Args(int argc, char** argv)
19{
20 clap::App app(argv[0], "Zappy server");
21
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)")
29 .default_value(-1);
30
31 if (argc == 1) {
32 app.print_help();
34 return;
35 }
36
37 try {
38 app.parse(argc, argv);
39 } catch (const clap::HelpRequested&) {
41 return;
42 } catch (const clap::ClapException& e) {
43 failWith(e.what(), argv[0]);
45 return;
46 }
47
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]);
52 return;
53 }
54
55 int p = port.get(), w = width.get(), h = height.get();
56 int c = clientsNb.get(), f = freq.get(), s = seed.get();
57
58 if (p < 1 || p > 65535) {
59 failWith("Port must be between 1 and 65535.", argv[0]);
61 return;
62 }
63 if (w < 1 || h < 1) {
64 failWith("Map dimensions must be positive.", argv[0]);
66 return;
67 }
68 if (c < 1) {
69 failWith("Clients per team must be positive.", argv[0]);
71 return;
72 }
73 if (f < 1) {
74 failWith("Frequency must be positive.", argv[0]);
76 return;
77 }
78
79 config.port = p;
80 config.width = w;
81 config.height = h;
82 config.teamNames = names;
83 config.clientsNb = c;
84 config.freq = f;
85 config.seed = (s < 0) ? std::random_device{}() : static_cast<unsigned int>(s);
87}
88
89bool Args::isValid() const { return result == ParseResult::Success; }
92
int exitCode() const
Gets the exit code for the application.
Definition Args.cpp:93
static constexpr int SUCCESS
Definition Args.hpp:74
ServerConfig config
Definition Args.hpp:77
Args(int argc, char **argv)
Definition Args.cpp:18
ParseResult result
Definition Args.hpp:78
ServerConfig getConfig() const
Gets the configuration for the server application.
Definition Args.cpp:91
static constexpr int ERROR
Definition Args.hpp:75
bool isHelpRequested() const
Checks if the help flag was provided in the arguments.
Definition Args.cpp:90
bool isValid() const
Checks if the parsed arguments are valid and if the application should run.
Definition Args.cpp:89
void failWith(const std::string &msg, const char *prog)
Definition Args.cpp:11
Represents the configuration for the server application, described by:
Definition Args.hpp:17
int height
Definition Args.hpp:20
int clientsNb
Definition Args.hpp:22
std::vector< std::string > teamNames
Definition Args.hpp:21
int width
Definition Args.hpp:19
unsigned int seed
Definition Args.hpp:24