From cfe810e78efb8a570602606157d28d108f5f4b4b Mon Sep 17 00:00:00 2001 From: Pavel Belikov Date: Sat, 18 Nov 2017 10:53:22 +0300 Subject: fix ValueReader for types assignable to std::string --- args.hxx | 45 ++++++++++++++++++++------------------------- test.cxx | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/args.hxx b/args.hxx index 7af24c4..5169d13 100644 --- a/args.hxx +++ b/args.hxx @@ -2579,16 +2579,18 @@ namespace args /** A default Reader class for argument classes * - * Simply uses a std::istringstream to read into the destination type, and + * If destination type is assignable to std::string it uses an assignment to std::string. + * Otherwise ValueReader simply uses a std::istringstream to read into the destination type, and * raises a ParseError if there are any characters left. */ - template struct ValueReader { - bool operator ()(const std::string &name, const std::string &value, T &destination) + template + typename std::enable_if::value, bool>::type + operator ()(const std::string &name, const std::string &value, T &destination) { std::istringstream ss(value); - ss >> destination; + ss >> destination >> std::ws; if (ss.rdbuf()->in_avail() > 0) { @@ -2603,19 +2605,12 @@ namespace args } return true; } - }; - /** std::string specialization for ValueReader - * - * By default, stream extraction into a string splits on white spaces, and - * it is more efficient to ust copy a string into the destination. - */ - template <> - struct ValueReader - { - bool operator()(const std::string &, const std::string &value, std::string &destination) + template + typename std::enable_if::value, bool>::type + operator()(const std::string &, const std::string &value, T &destination) { - destination.assign(value); + destination = value; return true; } }; @@ -2627,7 +2622,7 @@ namespace args */ template < typename T, - typename Reader = ValueReader> + typename Reader = ValueReader> class ValueFlag : public ValueFlagBase { protected: @@ -2682,7 +2677,7 @@ namespace args */ template < typename T, - typename Reader = ValueReader> + typename Reader = ValueReader> class ImplicitValueFlag : public ValueFlag { protected: @@ -2741,7 +2736,7 @@ namespace args template < typename T, template class List = std::vector, - typename Reader = ValueReader> + typename Reader = ValueReader> class NargsValueFlag : public FlagBase { protected: @@ -2843,7 +2838,7 @@ namespace args template < typename T, template class List = std::vector, - typename Reader = ValueReader> + typename Reader = ValueReader> class ValueFlagList : public ValueFlagBase { private: @@ -2948,7 +2943,7 @@ namespace args template < typename K, typename T, - typename Reader = ValueReader, + typename Reader = ValueReader, template class Map = std::unordered_map> class MapFlag : public ValueFlagBase { @@ -3023,7 +3018,7 @@ namespace args typename K, typename T, template class List = std::vector, - typename Reader = ValueReader, + typename Reader = ValueReader, template class Map = std::unordered_map> class MapFlagList : public ValueFlagBase { @@ -3139,7 +3134,7 @@ namespace args */ template < typename T, - typename Reader = ValueReader> + typename Reader = ValueReader> class Positional : public PositionalBase { private: @@ -3188,7 +3183,7 @@ namespace args template < typename T, template class List = std::vector, - typename Reader = ValueReader> + typename Reader = ValueReader> class PositionalList : public PositionalBase { private: @@ -3295,7 +3290,7 @@ namespace args template < typename K, typename T, - typename Reader = ValueReader, + typename Reader = ValueReader, template class Map = std::unordered_map> class MapPositional : public PositionalBase { @@ -3362,7 +3357,7 @@ namespace args typename K, typename T, template class List = std::vector, - typename Reader = ValueReader, + typename Reader = ValueReader, template class Map = std::unordered_map> class MapPositionalList : public PositionalBase { diff --git a/test.cxx b/test.cxx index 1189f97..c73c692 100644 --- a/test.cxx +++ b/test.cxx @@ -1066,6 +1066,39 @@ TEST_CASE("HelpParams work as expected", "[args]") } +struct StringAssignable +{ +public: + StringAssignable() = default; + StringAssignable(const std::string &p) : path(p) {} + std::string path; + + friend std::istream &operator >> (std::istream &s, StringAssignable &a) + { return s >> a.path; } +}; + +TEST_CASE("ValueParser works as expected", "[args]") +{ + static_assert(std::is_assignable::value, "StringAssignable must be assignable to std::string"); + + args::ArgumentParser p("parser"); + args::ValueFlag f(p, "name", "description", {'f'}); + args::ValueFlag b(p, "name", "description", {'b'}); + args::ValueFlag i(p, "name", "description", {'i'}); + + REQUIRE_NOTHROW(p.ParseArgs(std::vector{"-f", "a b"})); + REQUIRE(args::get(f) == "a b"); + + REQUIRE_NOTHROW(p.ParseArgs(std::vector{"-b", "a b"})); + REQUIRE(args::get(b).path == "a b"); + + REQUIRE_NOTHROW(p.ParseArgs(std::vector{"-i", "42 "})); + REQUIRE(args::get(i) == 42); + + REQUIRE_NOTHROW(p.ParseArgs(std::vector{"-i", " 12"})); + REQUIRE(args::get(i) == 12); +} + #undef ARGS_HXX #define ARGS_TESTNAMESPACE #define ARGS_NOEXCEPT -- cgit v1.2.1 From 184a6beb581448a84720ed6f74c6a04558e2aaa2 Mon Sep 17 00:00:00 2001 From: "Taylor C. Richberger" Date: Sun, 19 Nov 2017 14:09:05 -0700 Subject: Fix broken test I dropped a closing brace by mistake when merging --- test.cxx | 1 + 1 file changed, 1 insertion(+) diff --git a/test.cxx b/test.cxx index 46a7b4c..97d2c37 100644 --- a/test.cxx +++ b/test.cxx @@ -1097,6 +1097,7 @@ TEST_CASE("ValueParser works as expected", "[args]") REQUIRE_NOTHROW(p.ParseArgs(std::vector{"-i", " 12"})); REQUIRE(args::get(i) == 12); +} TEST_CASE("ActionFlag works as expected", "[args]") { -- cgit v1.2.1