tgen
Loading...
Searching...
No Matches
Registering and Opts

Registering and opts parsing. More...

Functions

bool tgen::has_opt (std::size_t index)
 Checks if opt at some index exists.
bool tgen::has_opt (const std::string &key)
 Checks if opt with with some key exists.
template<typename T, typename Key>
tgen::opt (const Key &key, std::optional< T > default_value=std::nullopt)
 Gets opt by key.
void tgen::register_gen (int argc, char **argv)
 Sets up the generator.

Detailed Description

Registering and opts parsing.

Registering

The first line of your generator should be:

#include "tgen.h"
int main(int argc, char** argv) {
tgen::register_gen(argc, argv);
// Rest of the generator.
}
void register_gen(int argc, char **argv)
Sets up the generator.
Definition tgen.h:595

This does two things:

  1. Sets up the seed for random generator based on the arguments (excluding the executable name). That is, ./a.out -n 10 and ./b.out -n 10 will use the same seed.
  2. Parses the opts (given in the arguments).

Opts

Opts are a list of either named or positional options.

Named options is given in one of the following formats:

  1. -keyname=value or –keyname=value (ex. -n=10, –test-count=20)
  2. -keyname value or –keyname value (ex. -n 10, –test-count 20)

Positional options are numbered from 0 sequentially.

For example, if you compile your code to a.out, calling ./a.out -n 10 str –q=5 -1.1e2 results in the following opts:

  • Named
    • n: 10
    • q: 5
  • Positional
    • 0: str
    • 1: -110

Consuming opts

You can consume the opts by using opt<>:

int n = tgen::opt<int>("n");
std::string s = tgen::opt<std::string>(0, ""); // Default value, if opt is not found.
T opt(const Key &key, std::optional< T > default_value=std::nullopt)
Gets opt by key.
Definition tgen.h:573

Function Documentation

◆ has_opt() [1/2]

bool tgen::has_opt ( const std::string & key)
inline

Checks if opt with with some key exists.

Parameters
keyKey of named opt.
Returns
If there is named opt with key.

Examples

// Checks if there is an opt with "q".
if (tgen::has_opt("q")) std::cout << "q found!" << std::endl;
bool has_opt(std::size_t index)
Checks if opt at some index exists.
Definition tgen.h:559

Definition at line 565 of file tgen.h.

◆ has_opt() [2/2]

bool tgen::has_opt ( std::size_t index)
inline

Checks if opt at some index exists.

Parameters
indexIndex of positional opt.
Returns
If there is positional opt at index.

Examples

// Checks if there is an opt at 0.
if (tgen::has_opt(0)) std::cout << "opt found!" << std::endl;

Definition at line 559 of file tgen.h.

◆ opt()

template<typename T, typename Key>
T tgen::opt ( const Key & key,
std::optional< T > default_value = std::nullopt )

Gets opt by key.

Template Parameters
TType of the opt.
Parameters
keyKey to fetch. If it is of type int, considered to be an index. Otherwise, considered to be a std::string named opt key.
default_valueDefault value to be returned, if key is not found.
Returns
The value of opt at/with key (index or name). If not found, returns default_value.
Exceptions
std::runtime_errorif opt is not found and default_value is not given.

Examples

// Consumes opt with "q".
int q = tgen::opt<int>("q");

Definition at line 573 of file tgen.h.

◆ register_gen()

void tgen::register_gen ( int argc,
char ** argv )
inline

Sets up the generator.

Parameters
argcArgument count, from the main function.
argvArgument vector, from the main function.

Sets up the opts, and the seed of the random number generator. The seed is dependent on the arguments (excluding the executable name).

Examples

#include "tgen.h"
int main(int argc, char** argv) {
// Register generator.
tgen::register_gen(argc, argv);
}

Definition at line 595 of file tgen.h.