From ac48146db3d2a834d720d8f4d76c1c7d05c25b8f Mon Sep 17 00:00:00 2001 From: Christos Tsolakis Date: Mon, 11 Nov 2019 21:16:53 -0500 Subject: [PATCH 1/6] fix https://github.com/CLIUtils/CLI11/issues/328 * use same assumptions about the size (ie double is enough) as in Number validator --- include/CLI/Validators.hpp | 2 +- tests/HelpersTest.cpp | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/CLI/Validators.hpp b/include/CLI/Validators.hpp index 053990955..ff84b7160 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -348,7 +348,7 @@ class PositiveNumber : public Validator { public: PositiveNumber() : Validator("POSITIVE") { func_ = [](std::string &number_str) { - int number; + double number; if(!detail::lexical_cast(number_str, number)) { return "Failed parsing number: (" + number_str + ')'; } diff --git a/tests/HelpersTest.cpp b/tests/HelpersTest.cpp index bb318e9b3..43b3d35c0 100644 --- a/tests/HelpersTest.cpp +++ b/tests/HelpersTest.cpp @@ -312,8 +312,12 @@ TEST(Validators, PositiveValidator) { EXPECT_TRUE(CLI::PositiveNumber(num).empty()); num = "0"; EXPECT_TRUE(CLI::PositiveNumber(num).empty()); + num = "+0.5"; + EXPECT_TRUE(CLI::PositiveNumber(num).empty()); num = "-1"; EXPECT_FALSE(CLI::PositiveNumber(num).empty()); + num = "-1.5"; + EXPECT_FALSE(CLI::PositiveNumber(num).empty()); num = "a"; EXPECT_FALSE(CLI::PositiveNumber(num).empty()); } From 2b36ce2fd1213ea9c5b1eeee1f1692cbbb18be9a Mon Sep 17 00:00:00 2001 From: Christos Tsolakis Date: Mon, 11 Nov 2019 21:18:44 -0500 Subject: [PATCH 2/6] fix spelling in error message --- 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 ff84b7160..e54539d39 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -353,7 +353,7 @@ class PositiveNumber : public Validator { return "Failed parsing number: (" + number_str + ')'; } if(number < 0) { - return "Number less then 0: (" + number_str + ')'; + return "Number less than 0: (" + number_str + ')'; } return std::string(); }; From 4d6a5b70b93bd4fd9c7ed7ccc507ddf3ba6c95f7 Mon Sep 17 00:00:00 2001 From: Christos Tsolakis Date: Mon, 11 Nov 2019 21:19:35 -0500 Subject: [PATCH 3/6] fix class description comment --- 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 e54539d39..f1c715b24 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -360,7 +360,7 @@ class PositiveNumber : public Validator { } }; -/// Validate the argument is a number and greater than or equal to 0 +/// Validate the argument is a number class Number : public Validator { public: Number() : Validator("NUMBER") { From 365a62204935400c82a593525418b31ed8fe8cf1 Mon Sep 17 00:00:00 2001 From: Christos Tsolakis Date: Mon, 11 Nov 2019 21:27:43 -0500 Subject: [PATCH 4/6] PositiveNumber accepts now >0 while NonNegative >=0 --- include/CLI/Validators.hpp | 21 ++++++++++++++++++++- tests/HelpersTest.cpp | 21 ++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/CLI/Validators.hpp b/include/CLI/Validators.hpp index f1c715b24..16c92fda5 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -343,10 +343,26 @@ class IPV4Validator : public Validator { } }; -/// Validate the argument is a number and greater than or equal to 0 +/// Validate the argument is a number and greater than 0 class PositiveNumber : public Validator { public: PositiveNumber() : Validator("POSITIVE") { + func_ = [](std::string &number_str) { + double number; + if(!detail::lexical_cast(number_str, number)) { + return "Failed parsing number: (" + number_str + ')'; + } + if(number <= 0) { + return "Number less or equal to 0: (" + number_str + ')'; + } + return std::string(); + }; + } +}; +/// Validate the argument is a number and greater than or equal to 0 +class NonNegativeNumber : public Validator { + public: + NonNegativeNumber() : Validator("NONNEGATIVE") { func_ = [](std::string &number_str) { double number; if(!detail::lexical_cast(number_str, number)) { @@ -396,6 +412,9 @@ const detail::IPV4Validator ValidIPV4; /// Check for a positive number const detail::PositiveNumber PositiveNumber; +/// Check for a non-negative number +const detail::NonNegativeNumber NonNegativeNumber; + /// Check for a number const detail::Number Number; diff --git a/tests/HelpersTest.cpp b/tests/HelpersTest.cpp index 43b3d35c0..ca54e02bd 100644 --- a/tests/HelpersTest.cpp +++ b/tests/HelpersTest.cpp @@ -311,7 +311,7 @@ TEST(Validators, PositiveValidator) { num = "10000"; EXPECT_TRUE(CLI::PositiveNumber(num).empty()); num = "0"; - EXPECT_TRUE(CLI::PositiveNumber(num).empty()); + EXPECT_FALSE(CLI::PositiveNumber(num).empty()); num = "+0.5"; EXPECT_TRUE(CLI::PositiveNumber(num).empty()); num = "-1"; @@ -322,6 +322,25 @@ TEST(Validators, PositiveValidator) { EXPECT_FALSE(CLI::PositiveNumber(num).empty()); } +TEST(Validators, NonNegativeValidator) { + std::string num = "1.1.1.1"; + EXPECT_FALSE(CLI::NonNegativeNumber(num).empty()); + num = "1"; + EXPECT_TRUE(CLI::NonNegativeNumber(num).empty()); + num = "10000"; + EXPECT_TRUE(CLI::NonNegativeNumber(num).empty()); + num = "0"; + EXPECT_TRUE(CLI::NonNegativeNumber(num).empty()); + num = "+0.5"; + EXPECT_TRUE(CLI::NonNegativeNumber(num).empty()); + num = "-1"; + EXPECT_FALSE(CLI::NonNegativeNumber(num).empty()); + num = "-1.5"; + EXPECT_FALSE(CLI::NonNegativeNumber(num).empty()); + num = "a"; + EXPECT_FALSE(CLI::NonNegativeNumber(num).empty()); +} + TEST(Validators, NumberValidator) { std::string num = "1.1.1.1"; EXPECT_FALSE(CLI::Number(num).empty()); From 19760be0a34852263c887a0daae2e0ea8a03dc60 Mon Sep 17 00:00:00 2001 From: Christos Tsolakis Date: Mon, 11 Nov 2019 21:36:52 -0500 Subject: [PATCH 5/6] update README for PositiveNumber and NonNegativeNumber --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bbaa21bad..dcea3f4b2 100644 --- a/README.md +++ b/README.md @@ -381,7 +381,8 @@ CLI11 has several Validators built-in that perform some common checks - `CLI::NonexistentPath`: Requires that the path does not exist. - `CLI::Range(min,max)`: Requires that the option be between min and max (make sure to use floating point if needed). Min defaults to 0. - `CLI::Bounded(min,max)`: 🆕 Modify the input such that it is always between min and max (make sure to use floating point if needed). Min defaults to 0. Will produce an error if conversion is not possible. -- `CLI::PositiveNumber`: 🆕 Requires the number be greater or equal to 0 +- `CLI::PositiveNumber`: 🆕 Requires the number be greater to 0 +- `CLI::NonNegativeNumber`: 🆕 Requires the number be greater or equal to 0 - `CLI::Number`: 🆕 Requires the input be a number. - `CLI::ValidIPV4`: 🆕 Requires that the option be a valid IPv4 string e.g. `'255.255.255.255'`, `'10.1.1.7'`. @@ -404,7 +405,7 @@ will produce a check to ensure a value is between 0 and 10 or 20 and 30. ->check(!CLI::PositiveNumber); ``` -will produce a check for a number less than 0. +will produce a check for a number less than or equal to 0. ##### Transforming Validators There are a few built in Validators that let you transform values if used with the `transform` function. If they also do some checks then they can be used `check` but some may do nothing in that case. From 367924e5853c5a941d04829131a1c595cd212f18 Mon Sep 17 00:00:00 2001 From: Christos Tsolakis Date: Mon, 11 Nov 2019 21:48:29 -0500 Subject: [PATCH 6/6] spelling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dcea3f4b2..fa1efb51b 100644 --- a/README.md +++ b/README.md @@ -381,7 +381,7 @@ CLI11 has several Validators built-in that perform some common checks - `CLI::NonexistentPath`: Requires that the path does not exist. - `CLI::Range(min,max)`: Requires that the option be between min and max (make sure to use floating point if needed). Min defaults to 0. - `CLI::Bounded(min,max)`: 🆕 Modify the input such that it is always between min and max (make sure to use floating point if needed). Min defaults to 0. Will produce an error if conversion is not possible. -- `CLI::PositiveNumber`: 🆕 Requires the number be greater to 0 +- `CLI::PositiveNumber`: 🆕 Requires the number be greater than 0 - `CLI::NonNegativeNumber`: 🆕 Requires the number be greater or equal to 0 - `CLI::Number`: 🆕 Requires the input be a number. - `CLI::ValidIPV4`: 🆕 Requires that the option be a valid IPv4 string e.g. `'255.255.255.255'`, `'10.1.1.7'`.