Quaternity
Functions
opts.h File Reference

This file defines the command line argument parsing function header. More...

#include "settings.h"

Go to the source code of this file.

Functions

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

Definition in file opts.h.

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