This file defines the command line argument parsing function and some helper functions.
More...
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include "opts.h"
#include "settings.h"
Go to the source code of this file.
|
void | usage (std::ostream &out, const char **argv) |
| Print the usage information for this program on the screen. More...
|
|
bool | valid (const Settings &settings, const char **argv) |
| Validate the settings given. More...
|
|
Settings | options (const int argc, const char **argv) |
| Parse the command line arguments and generate the settings from these. More...
|
|
This file defines the command line argument parsing function and some helper functions.
Definition in file opts.cpp.
◆ options()
Settings options |
( |
const int |
argc, |
|
|
const char ** |
argv |
|
) |
| |
Parse the command line arguments and generate the settings from these.
This function parses the command line arguments and generates the corresponding settings. If no argument is given for a specific value then the default value will be used.
- Parameters
-
argc | Number of command line arguments. |
argv | Command line arguments. |
Definition at line 85 of file opts.cpp.
92 while ((opt = getopt(argc, (
char*
const*)argv,
"s:n:p:h")) != -1) {
95 SET_SIZE = atoi(optarg);
98 NUM_SETS = atoi(optarg);
101 NUM_PLAYERS = atoi(optarg);
104 usage(std::cout, argv);
107 usage(std::cerr, argv);
112 Settings settings = {SET_SIZE, NUM_SETS, NUM_PLAYERS};
113 if (!
valid(settings, argv)) {
114 usage(std::cerr, argv);
void usage(std::ostream &out, const char **argv)
Print the usage information for this program on the screen.
bool valid(const Settings &settings, const char **argv)
Validate the settings given.
constexpr int DEFAULT_SET_SIZE
The default value for SET_SIZE.
constexpr int DEFAULT_NUM_SETS
The default value for NUM_SETS.
constexpr int DEFAULT_NUM_PLAYERS
The default value for NUM_PLAYERS.
The settings which characterize a game.
◆ usage()
void usage |
( |
std::ostream & |
out, |
|
|
const char ** |
argv |
|
) |
| |
Print the usage information for this program on the screen.
- Parameters
-
argv | Command line arguments. |
Definition at line 20 of file opts.cpp.
23 <<
"Usage: " << argv[0] <<
" [OPTIONS]" << std::endl
25 <<
" A flexible engine for imaginary quartet!" << std::endl
27 <<
"Note:" << std::endl
28 <<
" To ensure an equal distribution of the cards" << std::endl
29 <<
" we enforce: (n * s) mod p == 0." << std::endl
31 <<
"Options:" << std::endl
32 <<
" -s [int] Size of one set. [default: " <<
DEFAULT_SET_SIZE <<
"]" << std::endl
33 <<
" -n [int] Number of sets. [default: " <<
DEFAULT_NUM_SETS <<
"]" << std::endl
35 <<
" -h Show this help and exit." << std::endl;
◆ valid()
bool valid |
( |
const Settings & |
settings, |
|
|
const char ** |
argv |
|
) |
| |
Validate the settings given.
This function checks if all of the settings have their minimum value: 1 for SET_SIZE (s), 1 for NUM_SETS (n) and 2 for NUM_PLAYERS (p). It also checks if the condition (n * s) mod p == 0
holds. This condition is necessary to allow an equal distribution of the cards.
- Parameters
-
argv | Command line arguments. |
Definition at line 49 of file opts.cpp.
52 std::cerr << argv[0] <<
": the size of one set has to be at least one" << std::endl;
57 std::cerr << argv[0] <<
": the number of sets has to be at least one" << std::endl;
62 std::cerr << argv[0] <<
": the number of players has to be at least two" << std::endl;
67 std::cerr << argv[0] <<
": enforces (n * s) mod p == 0" << std::endl;
const int NUM_SETS
The number of sets.
const int SET_SIZE
The size of one set, aka the number of cards in one set.
const int NUM_PLAYERS
The number of players playing the game.