llarp::OptionDefinition

The primary type-aware implementation of OptionDefinitionBase, this templated class allows for implementations which can use fmt::format for conversion to string and std::istringstream for input from string. More...

#include <definition.hpp>

Inherits from llarp::OptionDefinitionBase

Public Functions

Name
template <typename... Options,std::enable_if_t<(config::is_option< T, Options > &&...), int > =0>
OptionDefinition(std::string section_, std::string name_, Options &&... opts)
Constructor.
template <typename U >
void
extractEnv(U && envValue)
template <typename U >
void
extractDefault(U && defaultValue_)
Extracts a default value from an config::Default or an array of defaults (for multi-valued options with multi-value default); ignores anything else.
template <typename U >
void
extractAcceptor(U && acceptor_)
Extracts an acceptor (i.e.
template <typename U >
void
extractComments(U && comment)
Extracts option Comments and forwards them addOptionComments.
std::optional< T > getValue() const
Returns the first parsed value, if available.
virtual size_t getNumberFound() const override
Returns the number of values found.
virtual std::vector< std::string > defaultValuesAsString() override
Subclasses should provide their default value as a string.
virtual void parseValue(const std::string & input) override
Subclasses should parse and store the provided input.
T fromString(const std::string & input)
virtual std::vector< std::string > valuesAsString() override
Subclasess should write their parsed values as strings.
virtual void tryAccept() const override
Attempts to call the acceptor function, if present.
bool fromString(const std::string & input)
Specialization for bool types.

Public Attributes

Name
std::vector< T > defaultValues
std::vector< T > parsedValues
std::vector< T > envValues
std::function< void(T)> acceptor

Additional inherited members

Public Functions inherited from llarp::OptionDefinitionBase

Name
template <typename... T>
OptionDefinitionBase(std::string section_, std::string name_, const T & ...)
virtual ~OptionDefinitionBase() =default

Public Attributes inherited from llarp::OptionDefinitionBase

Name
std::string section
std::string name
bool required
bool multiValued
bool deprecated
bool hidden
bool relayOnly
bool clientOnly
std::vector< std::string > comments

Detailed Description

template <typename T >
struct llarp::OptionDefinition;

The primary type-aware implementation of OptionDefinitionBase, this templated class allows for implementations which can use fmt::format for conversion to string and std::istringstream for input from string.

Note that types (T) used as template parameters here must be used verbatim when calling ConfigDefinition::getConfigValue(). Similar types such as uint32_t and int32_t cannot be mixed.

Public Functions Documentation

function OptionDefinition

template <typename... Options,
std::enable_if_t<(config::is_option< T, Options > &&...), int >  =0>
inline OptionDefinition(
    std::string section_,
    std::string name_,
    Options &&... opts
)

Constructor.

Parameters:

  • defaultValue_ is used in the following situations: 1) as the return value for getValue() if there is no parsed value and required==false 2) as the output in defaultValuesAsString(), used to generate config files 3) as the output in valueAsString(), used to generate config files
  • opts - 0 or more of config::Required, config::Hidden, config::Default{...}, etc. tagged options or an invocable acceptor validate and internalize input (e.g. copy it for runtime use). The acceptor should throw an exception with a useful message if it is not acceptable. Parameters may be passed in any order.

Arguments are passed directly to OptionDefinitionBase.

function extractEnv

template <typename U >
inline void extractEnv(
    U && envValue
)

function extractDefault

template <typename U >
inline void extractDefault(
    U && defaultValue_
)

Extracts a default value from an config::Default or an array of defaults (for multi-valued options with multi-value default); ignores anything else.

function extractAcceptor

template <typename U >
inline void extractAcceptor(
    U && acceptor_
)

Extracts an acceptor (i.e.

something callable with a T) from options; ignores anything that isn't callable.

function extractComments

template <typename U >
inline void extractComments(
    U && comment
)

Extracts option Comments and forwards them addOptionComments.

function getValue

inline std::optional< T > getValue() const

Returns the first parsed value, if available.

Return: an optional with the parsed value, the (first) default value, or no value.

Otherwise, provides the (first) default value if the option is not required. Otherwise, returns an empty optional.

function getNumberFound

inline virtual size_t getNumberFound() const override

Returns the number of values found.

Return: number of values found

Reimplements: llarp::OptionDefinitionBase::getNumberFound

function defaultValuesAsString

inline virtual std::vector< std::string > defaultValuesAsString() override

Subclasses should provide their default value as a string.

Return: the option's default value represented as a string

Reimplements: llarp::OptionDefinitionBase::defaultValuesAsString

function parseValue

inline virtual void parseValue(
    const std::string & input
) override

Subclasses should parse and store the provided input.

Parameters:

  • input is the string input to interpret

Reimplements: llarp::OptionDefinitionBase::parseValue

function fromString

inline T fromString(
    const std::string & input
)

function valuesAsString

inline virtual std::vector< std::string > valuesAsString() override

Subclasess should write their parsed values as strings.

Return: the option's value(s) as strings

Reimplements: llarp::OptionDefinitionBase::valuesAsString

function tryAccept

inline virtual void tryAccept() const override

Attempts to call the acceptor function, if present.

Exceptions:

  • if required and no value present or if the acceptor throws

Reimplements: llarp::OptionDefinitionBase::tryAccept

This function may throw if the value is not acceptable. Additionally, tryAccept should not be called if the option is required and no value has been provided.

function fromString

bool fromString(
    const std::string & input
)

Specialization for bool types.

We don't want to use stringstream parsing in this case because we want to accept "truthy" and "falsy" string values (e.g. "off" == false)

Public Attributes Documentation

variable defaultValues

std::vector< T > defaultValues;

variable parsedValues

std::vector< T > parsedValues;

variable envValues

std::vector< T > envValues;

variable acceptor

std::function< void(T)> acceptor;

Updated on 2026-01-10 at 22:49:45 +0000