1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-05 22:53:52 +00:00

fix: update the range error output (#690)

* update the range error output to be able to be used by more types, and better printouts in some situations.

* style: pre-commit.ci fixes

* add test

* style: pre-commit.ci fixes

* fix the test

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Philip Top 2022-01-05 12:36:15 -08:00 committed by GitHub
parent 9ec0ba78f6
commit 5ba63620dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View File

@ -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{};
};
}

View File

@ -1858,6 +1858,23 @@ 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() < 70U);
}
}
TEST_CASE_METHOD(TApp, "typeCheck", "[app]") {
/// Note that this must be a double in Range, too