Quaternity
Functions
opts.cpp File Reference

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.

Functions

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...
 

Detailed Description

This file defines the command line argument parsing function and some helper functions.

Definition in file opts.cpp.

Function Documentation

◆ 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
argcNumber of command line arguments.
argvCommand line arguments.

Definition at line 85 of file opts.cpp.

86 {
87  int SET_SIZE = DEFAULT_SET_SIZE;
88  int NUM_SETS = DEFAULT_NUM_SETS;
89  int NUM_PLAYERS = DEFAULT_NUM_PLAYERS;
90 
91  int opt;
92  while ((opt = getopt(argc, (char* const*)argv, "s:n:p:h")) != -1) {
93  switch (opt) {
94  case 's':
95  SET_SIZE = atoi(optarg);
96  break;
97  case 'n':
98  NUM_SETS = atoi(optarg);
99  break;
100  case 'p':
101  NUM_PLAYERS = atoi(optarg);
102  break;
103  case 'h':
104  usage(std::cout, argv);
105  exit(EXIT_SUCCESS);
106  default: // '?'
107  usage(std::cerr, argv);
108  exit(EXIT_FAILURE);
109  }
110  }
111 
112  Settings settings = {SET_SIZE, NUM_SETS, NUM_PLAYERS};
113  if (!valid(settings, argv)) {
114  usage(std::cerr, argv);
115  exit(EXIT_FAILURE);
116  }
117  return settings;
118 }
void usage(std::ostream &out, const char **argv)
Print the usage information for this program on the screen.
Definition: opts.cpp:20
bool valid(const Settings &settings, const char **argv)
Validate the settings given.
Definition: opts.cpp:49
constexpr int DEFAULT_SET_SIZE
The default value for SET_SIZE.
Definition: settings.h:11
constexpr int DEFAULT_NUM_SETS
The default value for NUM_SETS.
Definition: settings.h:14
constexpr int DEFAULT_NUM_PLAYERS
The default value for NUM_PLAYERS.
Definition: settings.h:17
The settings which characterize a game.
Definition: settings.h:22

◆ usage()

void usage ( std::ostream &  out,
const char **  argv 
)

Print the usage information for this program on the screen.

Parameters
argvCommand line arguments.

Definition at line 20 of file opts.cpp.

21 {
22  out
23  << "Usage: " << argv[0] << " [OPTIONS]" << std::endl
24  << std::endl
25  << " A flexible engine for imaginary quartet!" << std::endl
26  << 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
30  << 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
34  << " -p [int] Number of players. [default: " << DEFAULT_NUM_PLAYERS << "]" << std::endl
35  << " -h Show this help and exit." << std::endl;
36 
37 }

◆ 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
argvCommand line arguments.

Definition at line 49 of file opts.cpp.

50 {
51  if (settings.SET_SIZE < 1) {
52  std::cerr << argv[0] << ": the size of one set has to be at least one" << std::endl;
53  return false;
54  }
55 
56  if (settings.NUM_SETS < 1) {
57  std::cerr << argv[0] << ": the number of sets has to be at least one" << std::endl;
58  return false;
59  }
60 
61  if (settings.NUM_PLAYERS < 2) {
62  std::cerr << argv[0] << ": the number of players has to be at least two" << std::endl;
63  return false;
64  }
65 
66  if ((settings.NUM_SETS * settings.SET_SIZE) % settings.NUM_PLAYERS != 0) {
67  std::cerr << argv[0] << ": enforces (n * s) mod p == 0" << std::endl;
68  return false;
69  }
70 
71  return true;
72 }
const int NUM_SETS
The number of sets.
Definition: settings.h:32
const int SET_SIZE
The size of one set, aka the number of cards in one set.
Definition: settings.h:27
const int NUM_PLAYERS
The number of players playing the game.
Definition: settings.h:37