Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions include/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,17 @@ namespace argparse
class Converter
{
public:
auto from_string(std::string const & s, T & t) const -> bool
auto from_string(std::string const & s) const -> std::optional<T>
{
auto iss = std::istringstream(s);
auto t = T();
iss >> t;

return !iss.fail() && (iss.eof() || iss.peek() == std::istringstream::traits_type::eof());
if (!iss.fail() && (iss.eof() || iss.peek() == std::istringstream::traits_type::eof()))
{
return t;
}
return std::nullopt;
}

auto to_string(T const & t) const -> std::string
Expand All @@ -126,10 +131,10 @@ namespace argparse
};

template<typename T>
inline auto from_string(std::string const & s, T & t) -> bool
inline auto from_string(std::string const & s) -> std::optional<T>
{
auto const conv = Converter<T>();
return conv.from_string(s, t);
return conv.from_string(s);
}

template<typename T>
Expand Down Expand Up @@ -546,10 +551,9 @@ namespace argparse
}
else
{
auto value = T();
if (argparse::from_string(string, value))
if (auto const optvalue = argparse::from_string<T>(string); optvalue.has_value())
{
return std::any(value);
return std::any(*optvalue);
}
else
{
Expand Down Expand Up @@ -990,7 +994,7 @@ namespace argparse

static auto is_negative_number(std::string const & token) -> bool
{
if (auto num = double(); from_string(token, num))
if (auto const parsed = from_string<double>(token); parsed.has_value())
{
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions test/unittest/custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ template<>
class Converter<foo::Custom>
{
public:
auto from_string(std::string const & s, foo::Custom & t) const -> bool
auto from_string(std::string const & s) const -> std::optional<foo::Custom>
{
t = foo::Custom(s);
return true;
auto t = foo::Custom(s);
return t;
}

auto to_string(foo::Custom const & t) const -> std::string
Expand Down
5 changes: 3 additions & 2 deletions tutorial/custom1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ template<>
class Converter<geometry::Point>
{
public:
auto from_string(std::string const & s, geometry::Point & p) const -> bool
auto from_string(std::string const & s) const -> std::optional<geometry::Point>
{
std::istringstream iss(s);
auto p = geometry::Point();
char comma;
iss >> p.x >> comma >> p.y;
return true;
return p;
}

auto to_string(geometry::Point const & p) const -> std::string
Expand Down
7 changes: 5 additions & 2 deletions tutorial/custom2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ template<>
class Converter<geometry::Point>
{
public:
auto from_string(std::string const & s, geometry::Point & p) const -> bool
auto from_string(std::string const & s) const -> std::optional<geometry::Point>
{
std::istringstream iss(s);
auto p = geometry::Point();
char comma;
iss >> p.x >> comma >> p.y;
return !iss.fail();
return !iss.fail()
? std::optional<geometry::Point>(p)
: std::nullopt;
}

auto to_string(geometry::Point const & p) const -> std::string
Expand Down
7 changes: 5 additions & 2 deletions tutorial/custom3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ template<>
class Converter<geometry::Point>
{
public:
auto from_string(std::string const & s, geometry::Point & p) const -> bool
auto from_string(std::string const & s) const -> std::optional<geometry::Point>
{
std::istringstream iss(s);
auto p = geometry::Point();
char comma;
iss >> p.x >> comma >> p.y;
return !iss.fail();
return !iss.fail()
? std::optional<geometry::Point>(p)
: std::nullopt;
}

auto to_string(geometry::Point const & p) const -> std::string
Expand Down
21 changes: 14 additions & 7 deletions tutorial/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -958,12 +958,13 @@ template<>
class Converter<geometry::Point>
{
public:
auto from_string(std::string const & s, geometry::Point & p) const -> bool
auto from_string(std::string const & s) const -> std::optional<geometry::Point>
{
std::istringstream iss(s);
auto p = geometry::Point();
char comma;
iss >> p.x >> comma >> p.y;
return true;
return p;
}

auto to_string(geometry::Point const & p) const -> std::string
Expand Down Expand Up @@ -1005,7 +1006,7 @@ optional arguments:
$ custom1 0,0 1,1
The distance is 1.41421
```
The return value of `argparse::Converter::from_string` indicates whether the conversion succeeded. You can use it to your advantage (`custom2.cpp`):
The `argparse::Converter::from_string` function should return an empty optional to indicate conversion failure. You can use it to your advantage (`custom2.cpp`):
```c++
#include "argparse.hpp"
#include <string>
Expand Down Expand Up @@ -1033,12 +1034,15 @@ template<>
class Converter<geometry::Point>
{
public:
auto from_string(std::string const & s, geometry::Point & p) const -> bool
auto from_string(std::string const & s) const -> std::optional<geometry::Point>
{
std::istringstream iss(s);
auto p = geometry::Point();
char comma;
iss >> p.x >> comma >> p.y;
return !iss.fail();
return !iss.fail()
? std::optional<geometry::Point>(p)
: std::nullopt;
}

auto to_string(geometry::Point const & p) const -> std::string
Expand Down Expand Up @@ -1110,12 +1114,15 @@ template<>
class Converter<geometry::Point>
{
public:
auto from_string(std::string const & s, geometry::Point & p) const -> bool
auto from_string(std::string const & s) const -> std::optional<geometry::Point>
{
std::istringstream iss(s);
auto p = geometry::Point();
char comma;
iss >> p.x >> comma >> p.y;
return !iss.fail();
return !iss.fail()
? std::optional<geometry::Point>(p)
: std::nullopt;
}

auto to_string(geometry::Point const & p) const -> std::string
Expand Down