Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle float type in positive number (CLIUtils#328) #342

Merged
merged 6 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -343,24 +343,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 @@ -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;

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