From e5124b1725bb47c270d4391755926c200abdb7b7 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 13 Dec 2021 09:47:05 -0800 Subject: [PATCH 1/5] update the range error output to be able to be used by more types, and better printouts in some situations. --- include/CLI/Validators.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/include/CLI/Validators.hpp b/include/CLI/Validators.hpp index ba3cbe3cf..5a9745d36 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -480,10 +480,12 @@ class Range : public Validator { func_ = [min_val, max_val](std::string &input) { T val; bool converted = detail::lexical_cast(input, val); - if((!converted) || (val < min_val || val > max_val)) - return std::string("Value ") + input + " not in range " + std::to_string(min_val) + " to " + - std::to_string(max_val); - + if((!converted) || (val < min_val || val > max_val)) { + std::stringstream out; + out << "Value " << input << " not in range ["; + out << min_val << " - " << max_val<<"]"; + return out.str(); + } return std::string{}; }; } From 024f5b2d1e960eb4dba322cec47070f7358321bb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Dec 2021 17:59:00 +0000 Subject: [PATCH 2/5] style: pre-commit.ci fixes --- include/CLI/Validators.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/CLI/Validators.hpp b/include/CLI/Validators.hpp index 5a9745d36..63422cb7a 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -483,7 +483,7 @@ class Range : public Validator { if((!converted) || (val < min_val || val > max_val)) { std::stringstream out; out << "Value " << input << " not in range ["; - out << min_val << " - " << max_val<<"]"; + out << min_val << " - " << max_val << "]"; return out.str(); } return std::string{}; From 78ab9785c4148365ecda3638896eaa5ce0a43236 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 13 Dec 2021 09:59:10 -0800 Subject: [PATCH 3/5] add test --- tests/AppTest.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 3e39a7c9b..30726081b 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -1858,6 +1858,25 @@ TEST_CASE_METHOD(TApp, "RangeDouble", "[app]") { run(); } +TEST_CASE_METHOD(TApp, "NonNegative", "[app]") { + + std::string res; + /// Note that this must be a double in Range, too + app.add_option("--one", res)->check(CLI::NonNegativeNumber); + + args = {"--one=crazy"}; + try { + // this should throw + run(); + CHECK(false); + } + catch(const CLI::ValidationError &e) { + std::string emess = e.what(); + CHECK(emess.size() < 40U); + } + +} + TEST_CASE_METHOD(TApp, "typeCheck", "[app]") { /// Note that this must be a double in Range, too From 1b4aab8038890e78b6354941ab250bfd2cf60649 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Dec 2021 18:00:09 +0000 Subject: [PATCH 4/5] style: pre-commit.ci fixes --- tests/AppTest.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 30726081b..4619a72c5 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -1869,12 +1869,10 @@ TEST_CASE_METHOD(TApp, "NonNegative", "[app]") { // this should throw run(); CHECK(false); - } - catch(const CLI::ValidationError &e) { + } catch(const CLI::ValidationError &e) { std::string emess = e.what(); CHECK(emess.size() < 40U); } - } TEST_CASE_METHOD(TApp, "typeCheck", "[app]") { From c8efeebf13630587e1c47475b270843cbd7fede8 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 13 Dec 2021 10:10:33 -0800 Subject: [PATCH 5/5] fix the test --- tests/AppTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 4619a72c5..9268925f0 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -1871,7 +1871,7 @@ TEST_CASE_METHOD(TApp, "NonNegative", "[app]") { CHECK(false); } catch(const CLI::ValidationError &e) { std::string emess = e.what(); - CHECK(emess.size() < 40U); + CHECK(emess.size() < 70U); } }