From 77961bf9a4886bddc3ab567aeb1093943ff816ca Mon Sep 17 00:00:00 2001 From: Pavel Belikov Date: Tue, 25 Jun 2019 21:43:00 +0300 Subject: Fixes #75 : Check istringstream state in ValueParser --- args.hxx | 9 +++++++-- test.cxx | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/args.hxx b/args.hxx index 09c1919..563169c 100644 --- a/args.hxx +++ b/args.hxx @@ -3256,9 +3256,14 @@ namespace args operator ()(const std::string &name, const std::string &value, T &destination) { std::istringstream ss(value); - ss >> destination >> std::ws; + bool failed = !(ss >> destination); - if (ss.rdbuf()->in_avail() > 0) + if (!failed) + { + ss >> std::ws; + } + + if (ss.rdbuf()->in_avail() > 0 || failed) { #ifdef ARGS_NOEXCEPT (void)name; diff --git a/test.cxx b/test.cxx index b46007d..363ca59 100644 --- a/test.cxx +++ b/test.cxx @@ -1173,6 +1173,8 @@ TEST_CASE("ValueParser works as expected", "[args]") args::ValueFlag f(p, "name", "description", {'f'}); args::ValueFlag b(p, "name", "description", {'b'}); args::ValueFlag i(p, "name", "description", {'i'}); + args::ValueFlag d(p, "name", "description", {'d'}); + args::PositionalList ds(p, "name", "description"); REQUIRE_NOTHROW(p.ParseArgs(std::vector{"-f", "a b"})); REQUIRE(args::get(f) == "a b"); @@ -1185,6 +1187,11 @@ TEST_CASE("ValueParser works as expected", "[args]") REQUIRE_NOTHROW(p.ParseArgs(std::vector{"-i", " 12"})); REQUIRE(args::get(i) == 12); + + REQUIRE_THROWS_AS(p.ParseArgs(std::vector{"-i", "a"}), args::ParseError); + REQUIRE_THROWS_AS(p.ParseArgs(std::vector{"-d", "b"}), args::ParseError); + REQUIRE_THROWS_AS(p.ParseArgs(std::vector{"c"}), args::ParseError); + REQUIRE_THROWS_AS(p.ParseArgs(std::vector{"s"}), args::ParseError); } TEST_CASE("ActionFlag works as expected", "[args]") -- cgit v1.2.1