goptions
Loading...
Searching...
No Matches
optional_scalar.cc
Go to the documentation of this file.
1
5
6#include "goptions.h"
7
8#include <optional>
9#include <string>
10
11namespace {
12
14 GOptions options;
15 options.defineOption(
16 GVariable("optional_string", std::nullopt, "optional string used by this test"),
17 "An unset value must be returned as std::nullopt.");
18 options.defineOption(
19 GVariable("optional_int", std::nullopt, "optional integer used by this test"),
20 "An unset value must be returned as std::nullopt.");
21 options.defineOption(GVariable("required_string", "configured", "required string used by this test"), "");
22 options.defineOption(GVariable("required_int", 7, "required integer used by this test"), "");
23 options.defineOption(GVariable("required_double", 1.5, "required double used by this test"), "");
24 return options;
25}
26
27} // namespace
28
29int main(int argc, char* argv[]) {
30 std::optional<std::string> expected;
31 std::optional<int> expectedInt;
32 const std::string option_prefix = "-optional_string=";
33 const std::string int_option_prefix = "-optional_int=";
34 for (int index = 1; index < argc; ++index) {
35 const std::string argument = argv[index];
36 if (argument.rfind(option_prefix, 0) == 0) {
37 expected = argument.substr(option_prefix.size());
38 }
39 if (argument.rfind(int_option_prefix, 0) == 0) {
40 expectedInt = std::stoi(argument.substr(int_option_prefix.size()));
41 }
42 }
43
44 GOptions options(argc, argv, defineOptions());
45 const auto value = options.getOptionalScalarString("optional_string");
46 const auto intValue = options.getOptionalScalarInt("optional_int");
47 if (options.getRequiredScalarString("required_string") != "configured" ||
48 options.getRequiredScalarInt("required_int") != 7 ||
49 options.getRequiredScalarDouble("required_double") != 1.5) {
50 return 1;
51 }
52
53 if (expected) {
54 if (!value || *value != *expected) return 1;
55 }
56 else if (value) return 1;
57 return intValue == expectedInt ? 0 : 1;
58}
Parses, stores, and exposes command-line options and YAML configuration values.
Definition goptions.h:47
double getRequiredScalarDouble(const std::string &tag) const
Retrieves the required value of a scalar double option.
Definition goptions.cc:310
void defineOption(const GVariable &gvar, const std::string &help)
Defines and adds a scalar option.
Definition goptions.cc:255
std::optional< std::string > getOptionalScalarString(const std::string &tag) const
Retrieves the optional value of a scalar string option.
Definition goptions.cc:355
std::string getRequiredScalarString(const std::string &tag) const
Retrieves the required value of a scalar string option.
Definition goptions.cc:338
std::optional< int > getOptionalScalarInt(const std::string &tag) const
Definition goptions.cc:297
int getRequiredScalarInt(const std::string &tag) const
Retrieves the required value of a scalar integer option.
Definition goptions.cc:281
Public interface for GOptions : the YAML + command-line configuration manager.
int main(int argc, char *argv[])
Describes a schema entry: key name, default value, and user-facing description.
Definition goption.h:35
GOptions defineOptions()