Skip to content

Commit

Permalink
Handle float type in positive number (#328) (#342)
Browse files Browse the repository at this point in the history
* fix #328

 * use same assumptions about the size (ie double is enough) as in Number validator

* fix spelling in error message

* fix class description comment

* PositiveNumber accepts now >0 while NonNegative >=0

* update README for PositiveNumber and NonNegativeNumber

* spelling
  • Loading branch information
ChristosT authored and henryiii committed Nov 25, 2019
1 parent a8d597d commit 51a395e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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'`.

Expand All @@ -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.
Expand Down
27 changes: 23 additions & 4 deletions include/CLI/Validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,24 +391,40 @@ 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) {
int number;
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)) {
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();
};
}
};

/// 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") {
Expand Down Expand Up @@ -444,6 +460,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;

Expand Down
23 changes: 23 additions & 0 deletions tests/HelpersTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,36 @@ TEST(Validators, PositiveValidator) {
num = "10000";
EXPECT_TRUE(CLI::PositiveNumber(num).empty());
num = "0";
EXPECT_FALSE(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());
}

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());
Expand Down

0 comments on commit 51a395e

Please sign in to comment.