mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 12:13:52 +00:00
Handle float type in positive number (CLIUtils#328) (#342)
* fix https://github.com/CLIUtils/CLI11/issues/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
This commit is contained in:
parent
a8d597dae4
commit
51a395ec9f
@ -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'`.
|
||||
|
||||
@ -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.
|
||||
|
@ -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") {
|
||||
@ -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;
|
||||
|
||||
|
@ -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());
|
||||
|
Loading…
x
Reference in New Issue
Block a user