cli#

namespace heph
namespace cli#
class ProgramDescription#
#include “hephaestus/cli/program_options.h”

Program description and command line parsing utility. Works with ProgramOptions class.

Features:

  • Enforces that every supported command line option is described exactly once

  • Throws if unsupported options are specified on the command line

  • Throws if required options are not specified on the command line

  • Throws if value types are mismatched between where they are defined and where they are used

  • Ensures ‘help’ is always available See also ProgramOptions

Public Functions

explicit ProgramDescription(std::string brief)#

Creates object.

Parameters:

brief – A brief text describing the program

template<StringStreamable T>
ProgramDescription &defineOption(const std::string &key, const std::string &description)#

Defines a required option (&#8212;key=value) on the command line.

Template Parameters:

T – Value type

Parameters:
  • key – Key of the key-value pair

  • description – A brief text describing the option

Returns:

Reference to the object. Enables daisy-chained calls

template<StringStreamable T>
ProgramDescription &defineOption(const std::string &key, char short_key, const std::string &description)#

Defines a required option (&#8212;key=value) on the command line.

Template Parameters:

T – Value type

Parameters:
  • key – Key of the key-value pair

  • short_key – Single char (can be used as alias for &#8212;key)

  • description – A brief text describing the option

Returns:

Reference to the object. Enables daisy-chained calls

template<StringStreamable T>
ProgramDescription &defineOption(const std::string &key, const std::string &description, const T &default_value)#

Defines a command line option (&#8212;key=value) that is optional.

Template Parameters:

T – Value type

Parameters:
  • key – Key of the key-value pair

  • description – A brief text describing the option

  • default_value – Default value to use if the option is not specified on the command line

Returns:

Reference to the object. Enables daisy-chained calls

template<StringStreamable T>
ProgramDescription &defineOption(const std::string &key, char short_key, const std::string &description, const T &default_value)#

Defines a command line option (&#8212;key=value) that is optional.

Template Parameters:

T – Value type

Parameters:
  • key – Key of the key-value pair

  • short_key – Single char (can be used as alias for &#8212;key)

  • description – A brief text describing the option

  • default_value – Default value to use if the option is not specified on the command line

Returns:

Reference to the object. Enables daisy-chained calls

ProgramDescription &defineFlag(const std::string &key, char short_key, const std::string &description)#

Defines a boolean option (flag) (&#8212;key=value) on the command line. If the flag is passed the value of the option is true, false otherwise.

Parameters:
  • key – Key of the key-value pair

  • short_key – Single char (can be used as alias for &#8212;key)

  • description – A brief text describing the option

Returns:

Reference to the object. Enables daisy-chained calls

ProgramDescription &defineFlag(const std::string &key, const std::string &description)#

Defines a boolean option (flag) (&#8212;key=value) on the command line. If the flag is passed the value of the option is true, false otherwise.

Parameters:
  • key – Key of the key-value pair

  • description – A brief text describing the option

Returns:

Reference to the object. Enables daisy-chained calls

ProgramOptions parse(int argc, const char **argv) &&#

Builds the container to parse command line options.

Note

: The resources in this object is moved into the returned object, making this object invalid.

Parameters:
  • argc – Number of arguments on the command line

  • argv – array of C-style strings

Returns:

Object containing command line options

ProgramOptions parse(const std::vector<std::string> &args) &&#

Builds the container to parse command line options.

Note

: The resources in this object is moved into the returned object, making this object invalid.

Parameters:

args – List of element passed to the program

Returns:

Object containing command line options

class ProgramOptions#
#include “hephaestus/cli/program_options.h”

Container for command line options for a program. To be used with ProgramDescription class.

struct Option#
#include “hephaestus/cli/program_options.h”

Holds program options and their details.

Public Members

std::string key#
char short_key = {}#
std::string description#
std::string value_type#
std::string value#
bool is_required = {false}#
bool is_specified = {false}#

Public Functions

bool hasOption(const std::string &option) const#

Check whether an option was specified on the command line

Parameters:

option – The command line option (without the ‘&#8212;’).

Returns:

true if option is found

template<StringStreamable T>
inline T getOption(const std::string &option) const#

Get the value specified for a command line option

Parameters:

option – The command line option (without the ‘&#8212;’).

Returns:

The value of the specified option.

namespace heph
namespace cli

Functions

bool kbhit()#
Returns:

true if key was pressed on the terminal console. use getch() to read the key stroke.

int getch()#
Returns:

Code for the last keypress from the terminal console without blocking