Compare commits

...

1 Commits

Author SHA1 Message Date
Jarryd Beck
642bdfc05b Fix ordering of parse_value functions
Fixes #419. Put the definitions of parse_value functions before they
are called so that the right ones are chosen.
2024-02-20 19:30:24 +11:00
2 changed files with 28 additions and 24 deletions

View File

@ -1062,6 +1062,28 @@ parse_value(const std::string& text, T& value) {
stringstream_parser(text, value); stringstream_parser(text, value);
} }
#ifdef CXXOPTS_HAS_OPTIONAL
template <typename T>
void
parse_value(const std::string& text, std::optional<T>& value)
{
T result;
parse_value(text, result);
value = std::move(result);
}
#endif
inline
void parse_value(const std::string& text, char& c)
{
if (text.length() != 1)
{
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
c = text[0];
}
template <typename T> template <typename T>
void void
parse_value(const std::string& text, std::vector<T>& value) parse_value(const std::string& text, std::vector<T>& value)
@ -1097,28 +1119,6 @@ add_value(const std::string& text, std::vector<T>& value)
value.emplace_back(std::move(v)); value.emplace_back(std::move(v));
} }
#ifdef CXXOPTS_HAS_OPTIONAL
template <typename T>
void
parse_value(const std::string& text, std::optional<T>& value)
{
T result;
parse_value(text, result);
value = std::move(result);
}
#endif
inline
void parse_value(const std::string& text, char& c)
{
if (text.length() != 1)
{
throw_or_mimic<exceptions::incorrect_argument_type>(text);
}
c = text[0];
}
template <typename T> template <typename T>
struct type_is_container struct type_is_container
{ {

View File

@ -709,11 +709,13 @@ TEST_CASE("std::vector", "[vector]") {
#ifdef CXXOPTS_HAS_OPTIONAL #ifdef CXXOPTS_HAS_OPTIONAL
TEST_CASE("std::optional", "[optional]") { TEST_CASE("std::optional", "[optional]") {
std::optional<std::string> optional; std::optional<std::string> optional;
std::optional<bool> opt_bool;
cxxopts::Options options("optional", " - tests optional"); cxxopts::Options options("optional", " - tests optional");
options.add_options() options.add_options()
("optional", "an optional option", cxxopts::value<std::optional<std::string>>(optional)); ("optional", "an optional option", cxxopts::value<std::optional<std::string>>(optional))
("optional_bool", "an boolean optional", cxxopts::value<std::optional<bool>>(opt_bool)->default_value("false"));
Argv av({"optional", "--optional", "foo"}); Argv av({"optional", "--optional", "foo", "--optional_bool", "true"});
auto** argv = av.argv(); auto** argv = av.argv();
auto argc = av.argc(); auto argc = av.argc();
@ -722,6 +724,8 @@ TEST_CASE("std::optional", "[optional]") {
REQUIRE(optional.has_value()); REQUIRE(optional.has_value());
CHECK(*optional == "foo"); CHECK(*optional == "foo");
CHECK(opt_bool.has_value());
CHECK(*opt_bool);
} }
#endif #endif