1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Making validators run before and with a reference

This commit is contained in:
Henry Fredrick Schreiner 2017-11-23 22:32:30 -05:00 committed by Henry Schreiner
parent 94b70c8f91
commit 3006f82bf6
2 changed files with 17 additions and 15 deletions

View File

@ -148,7 +148,7 @@ class Option : public OptionBase<Option> {
bool changeable_{false};
/// A list of validators to run on each value parsed
std::vector<std::function<bool(std::string)>> validators_;
std::vector<std::function<bool(std::string &)>> validators_;
/// A list of options that are required with this option
std::set<Option *> requires_;
@ -220,7 +220,7 @@ class Option : public OptionBase<Option> {
}
/// Adds a validator
Option *check(std::function<bool(std::string)> validator) {
Option *check(std::function<bool(std::string &)> validator) {
validators_.push_back(validator);
return this;
@ -417,7 +417,16 @@ class Option : public OptionBase<Option> {
///@{
/// Process the callback
void run_callback() const {
void run_callback() {
// Run the validators (can change the string)
if(!validators_.empty()) {
for(std::string &result : results_)
for(const std::function<bool(std::string &)> &vali : validators_)
if(!vali(result))
throw ValidationError("Failed validation: " + get_name() + "=" + result);
}
bool local_result;
// If take_last, only operate on the final item
if(last_) {
@ -429,13 +438,6 @@ class Option : public OptionBase<Option> {
if(local_result)
throw ConversionError("Could not convert: " + get_name() + "=" + detail::join(results_));
if(!validators_.empty()) {
for(const std::string &result : results_)
for(const std::function<bool(std::string)> &vali : validators_)
if(!vali(result))
throw ValidationError("Failed validation: " + get_name() + "=" + result);
}
}
/// If options share any of the same names, they are equal (not counting positional)

View File

@ -23,7 +23,7 @@ namespace CLI {
/// @{
/// Check for an existing file
inline bool ExistingFile(std::string filename) {
inline bool ExistingFile(const std::string &filename) {
struct stat buffer;
bool exist = stat(filename.c_str(), &buffer) == 0;
bool is_dir = (buffer.st_mode & S_IFDIR) != 0;
@ -39,7 +39,7 @@ inline bool ExistingFile(std::string filename) {
}
/// Check for an existing directory
inline bool ExistingDirectory(std::string filename) {
inline bool ExistingDirectory(const std::string &filename) {
struct stat buffer;
bool exist = stat(filename.c_str(), &buffer) == 0;
bool is_dir = (buffer.st_mode & S_IFDIR) != 0;
@ -55,7 +55,7 @@ inline bool ExistingDirectory(std::string filename) {
}
/// Check for a non-existing path
inline bool NonexistentPath(std::string filename) {
inline bool NonexistentPath(const std::string &filename) {
struct stat buffer;
bool exist = stat(filename.c_str(), &buffer) == 0;
if(!exist) {
@ -67,7 +67,7 @@ inline bool NonexistentPath(std::string filename) {
}
/// Produce a range validator function
template <typename T> std::function<bool(std::string)> Range(T min, T max) {
template <typename T> std::function<bool(const std::string &)> Range(T min, T max) {
return [min, max](std::string input) {
T val;
detail::lexical_cast(input, val);
@ -76,7 +76,7 @@ template <typename T> std::function<bool(std::string)> Range(T min, T max) {
}
/// Range of one value is 0 to value
template <typename T> std::function<bool(std::string)> Range(T max) { return Range(static_cast<T>(0), max); }
template <typename T> std::function<bool(const std::string &)> Range(T max) { return Range(static_cast<T>(0), max); }
/// @}