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

update the range error output #690

Merged
merged 5 commits into from
Jan 5, 2022
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
10 changes: 6 additions & 4 deletions include/CLI/Validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
};
}
Expand Down
17 changes: 17 additions & 0 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down