You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+9-10Lines changed: 9 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -296,44 +296,43 @@ For the complete code see [examples/libenvpp_range_example.cpp](examples/libenvp
296
296
297
297
### Option Variables
298
298
299
-
Another frequent use-case is that a value is one of a given set of options. For this an environment variable can be registered with `register_[required]_option`, which takes a list of valid options, against which the value is checked. For example:
299
+
Another frequent use-case is that a value is one of a given set of options. For this an environment variable can be registered with `register_[required]_option`, which takes a list of either pairs of strings and corresponding options, or just valid options, against which the value is checked. For example:
const auto parsed_and_validated_pre = pre.parse_and_validate();
317
316
318
317
if (parsed_and_validated_pre.ok()) {
319
-
const auto opt = parsed_and_validated_pre.get_or(option_id, option::default_choice);
318
+
const auto opt = parsed_and_validated_pre.get_or(option_id, option::default);
320
319
}
321
320
}
322
321
```
323
322
324
-
This registers an `enum class` option, where only a subset of all possible values is considered valid, so that `option::default_choice` can be used as the value if the variable is not set.
323
+
This registers an `enum class` option, where only a subset of all possible values is considered valid, so that `option::default` can be used as the value if the variable is not set.
325
324
326
325
_Note:_ The list of options provided when registering must not be empty, and must not contain duplicates.
327
326
328
327
_Note:_ As with range variables, the default value given with `get_or` is not enforced to be within the list of options given when registering the option variable.
329
328
330
-
_Note:_ Since C++ does not provide any way to automatically parse `enum class` types from string, the example above additionally requires a specialized `default_parser` for the `enum class` type.
329
+
_Note:_ For the variant where no mapping to strings is provided, a specialized `default_parser` for the `enum class` type must exist.
331
330
332
331
_Note:_ Options are mostly intended to be used with `enum class` types, but this is in no way a requirement. Any type can be used as an option, and `enum class` types can also just be normal environment variables.
333
332
334
333
#### Option Variables - Code
335
334
336
-
For the full code, including the parser for the enum class, see [examples/libenvpp_option_example.cpp](examples/libenvpp_option_example.cpp).
335
+
For the full code, which features both the simple case shown above and a case with a custom parser, see [examples/libenvpp_option_example.cpp](examples/libenvpp_option_example.cpp).
0 commit comments