---
title: llarp::OptionDefinition
summary: 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. 

---

# llarp::OptionDefinition



The primary type-aware implementation of [OptionDefinitionBase](Classes/structllarp_1_1OptionDefinitionBase.md), this templated class allows for implementations which can use fmt::format for conversion to string and std::istringstream for input from string.  [More...](#detailed-description)


`#include <definition.hpp>`

Inherits from [llarp::OptionDefinitionBase](Classes/structllarp_1_1OptionDefinitionBase.md)

## Public Functions

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

## Public Attributes

|                | Name           |
| -------------- | -------------- |
| std::vector< T > | **[defaultValues](Classes/structllarp_1_1OptionDefinition.md#variable-defaultvalues)**  |
| std::vector< T > | **[parsedValues](Classes/structllarp_1_1OptionDefinition.md#variable-parsedvalues)**  |
| std::function< void(T)> | **[acceptor](Classes/structllarp_1_1OptionDefinition.md#variable-acceptor)**  |

## Additional inherited members

**Public Functions inherited from [llarp::OptionDefinitionBase](Classes/structllarp_1_1OptionDefinitionBase.md)**

|                | Name           |
| -------------- | -------------- |
| template <typename... T\> <br>| **[OptionDefinitionBase](Classes/structllarp_1_1OptionDefinitionBase.md#function-optiondefinitionbase)**(std::string section_, std::string name_, const T & ...) |
| virtual | **[~OptionDefinitionBase](Classes/structllarp_1_1OptionDefinitionBase.md#function-~optiondefinitionbase)**() =default |

**Public Attributes inherited from [llarp::OptionDefinitionBase](Classes/structllarp_1_1OptionDefinitionBase.md)**

|                | Name           |
| -------------- | -------------- |
| std::string | **[section](Classes/structllarp_1_1OptionDefinitionBase.md#variable-section)**  |
| std::string | **[name](Classes/structllarp_1_1OptionDefinitionBase.md#variable-name)**  |
| bool | **[required](Classes/structllarp_1_1OptionDefinitionBase.md#variable-required)**  |
| bool | **[multiValued](Classes/structllarp_1_1OptionDefinitionBase.md#variable-multivalued)**  |
| bool | **[deprecated](Classes/structllarp_1_1OptionDefinitionBase.md#variable-deprecated)**  |
| bool | **[hidden](Classes/structllarp_1_1OptionDefinitionBase.md#variable-hidden)**  |
| bool | **[relayOnly](Classes/structllarp_1_1OptionDefinitionBase.md#variable-relayonly)**  |
| bool | **[clientOnly](Classes/structllarp_1_1OptionDefinitionBase.md#variable-clientonly)**  |
| std::vector< std::string > | **[comments](Classes/structllarp_1_1OptionDefinitionBase.md#variable-comments)**  |


## Detailed Description

```cpp
template <typename T >
struct llarp::OptionDefinition;
```

The primary type-aware implementation of [OptionDefinitionBase](Classes/structllarp_1_1OptionDefinitionBase.md), 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()](Classes/structllarp_1_1ConfigDefinition.md#function-getconfigvalue). Similar types such as uint32_t and int32_t cannot be mixed. 

## Public Functions Documentation

### function OptionDefinition

```cpp
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()](Classes/structllarp_1_1OptionDefinition.md#function-getvalue) if there is no parsed value and required==false 2) as the output in [defaultValuesAsString()](Classes/structllarp_1_1OptionDefinition.md#function-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](Classes/structllarp_1_1config_1_1Default.md){...}, 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](Classes/structllarp_1_1OptionDefinitionBase.md).


### function extractDefault

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

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

### function extractAcceptor

```cpp
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

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

Extracts option Comments and forwards them addOptionComments. 

### function getValue

```cpp
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

```cpp
inline virtual size_t getNumberFound() const override
```

Returns the number of values found. 

**Return**: number of values found 

**Reimplements**: [llarp::OptionDefinitionBase::getNumberFound](Classes/structllarp_1_1OptionDefinitionBase.md#function-getnumberfound)


### function defaultValuesAsString

```cpp
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](Classes/structllarp_1_1OptionDefinitionBase.md#function-defaultvaluesasstring)


### function parseValue

```cpp
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](Classes/structllarp_1_1OptionDefinitionBase.md#function-parsevalue)


### function fromString

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


### function valuesAsString

```cpp
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](Classes/structllarp_1_1OptionDefinitionBase.md#function-valuesasstring)


### function tryAccept

```cpp
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](Classes/structllarp_1_1OptionDefinitionBase.md#function-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

```cpp
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

```cpp
std::vector< T > defaultValues;
```


### variable parsedValues

```cpp
std::vector< T > parsedValues;
```


### variable acceptor

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


-------------------------------

Updated on 2023-01-09 at 16:43:38 +0000