Class config

Class Documentation

class config

A singleton class. Use mcfp::config::instance to create and/or retrieve the single instance.

Public Functions

inline void set_usage(std::string_view usage)

Set the ‘usage’ string.

Parameters

usage – The usage message

template<typename ...Options>
inline void init(std::string_view usage, Options... options)

Initialise a config instance with a usage message and a set of options.

Parameters
  • usage – The usage message

  • options – Variadic list of options recognised by this config object, use mcfp::make_option and variants to create these

inline void set_ignore_unknown(bool ignore_unknown)

Set the ignore unknown flag.

Parameters

ignore_unknown – When true, unknown options are simply ignored instead of throwing an error

inline bool has(std::string_view name) const

Simply return true if the option with name has a value assigned.

Parameters

name – The name of the option

Returns

bool Returns true when the option has a value

inline int count(std::string_view name) const

Return how often an option with the name name was seen. Use e.g. to increase verbosity level.

Parameters

name – The name of the option to check

Returns

int The count for the named option

template<typename T>
inline auto get(std::string_view name) const

Returns the value for the option with name name. Throws an exception if the option has not value assigned.

Template Parameters

T – The type of the value requested.

Parameters

name – The name of the option requested

Returns

auto The value of the named option

template<typename T>
inline auto get(std::string_view name, std::error_code &ec) const

Returns the value for the option with name name. If the option has no value assigned or is of a wrong type, ec is set to an appropriate error.

Template Parameters

T – The type of the value requested.

Parameters
  • name – The name of the option requested

  • ec – The error status is returned in this variable

Returns

auto The value of the named option

inline std::string get(std::string_view name) const

Return the std::string value of the option with name name If no value was assigned, or the type of the option cannot be casted to a string, an exception is thrown.

Parameters

name – The name of the option value requested

Returns

std::string The value of the option

inline std::string get(std::string_view name, std::error_code &ec) const

Return the std::string value of the option with name name If no value was assigned, or the type of the option cannot be casted to a string, an error is returned in ec.

Parameters
  • name – The name of the option value requested

  • ec – The error status is returned in this variable

Returns

std::string The value of the option

inline const std::vector<std::string> &operands() const

Return the list of operands.

Returns

const std::vector<std::string>& The operand as a vector of strings

inline void parse(int argc, const char *const argv[])

Parse the argv vector containing argc elements. Throws an exception if any error was found.

Parameters
  • argc – The number of elements in argv

  • argv – The vector of command line arguments

inline void parse_config_file(std::string_view config_option, std::string_view config_file_name, std::initializer_list<std::string_view> search_dirs)

Parse a configuration file called config_file_name optionally specified on the command line with option config_option The file is searched for in each of the directories specified in search_dirs This function throws an exception if an error was found during processing.

Parameters
  • config_option – The name of the option used to specify the config file

  • config_file_name – The default name of the option file to use if the config option was not specified on the command line

  • search_dirs – The list of directories to search for the config file

inline void parse_config_file(std::string_view config_option, std::string_view config_file_name, std::initializer_list<std::string_view> search_dirs, std::error_code &ec)

Parse a configuration file called config_file_name optionally specified on the command line with option config_option The file is searched for in each of the directories specified in search_dirs If an error is found it is returned in the variable ec.

Parameters
  • config_option – The name of the option used to specify the config file

  • config_file_name – The default name of the option file to use if the config option was not specified on the command line

  • search_dirs – The list of directories to search for the config file

  • ec – The variable containing the error status

inline void parse_config_file(const std::filesystem::path &file, std::error_code &ec)

Parse a configuration file specified by file If an error is found it is returned in the variable ec.

Parameters
  • file – The path to the config file

  • ec – The variable containing the error status

inline void parse_config_file(std::istream &is, std::error_code &ec)

Parse the configuration file in is If an error is found it is returned in the variable ec.

Parameters
  • is – A std::istream for the contents of a config file

  • ec – The variable containing the error status

inline void parse(int argc, const char *const argv[], std::error_code &ec)

Parse the argv vector containing argc elements. In case of an error, the error is returned in ec.

Parameters
  • argc – The number of elements in argv

  • argv – The vector of command line arguments

  • ec – The variable receiving the error status

Public Static Functions

static inline config &instance()

Use this to retrieve the single instance of this class.

Returns

config& The singleton instance

Friends

inline friend std::ostream &operator<<(std::ostream &os, const config &conf)

Write the configuration to the std::ostream os This will print the usage string and each of the configured options along with their optional default value as well as their help string.

Parameters
  • os – The std::ostream to write to, usually std::cout or std::cerr

  • conf – The config object to write out

Returns

std::ostream& Returns the parameter os