mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-30 12:43:52 +00:00
Making validators run before and with a reference
This commit is contained in:
parent
94b70c8f91
commit
3006f82bf6
@ -148,7 +148,7 @@ class Option : public OptionBase<Option> {
|
|||||||
bool changeable_{false};
|
bool changeable_{false};
|
||||||
|
|
||||||
/// A list of validators to run on each value parsed
|
/// 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
|
/// A list of options that are required with this option
|
||||||
std::set<Option *> requires_;
|
std::set<Option *> requires_;
|
||||||
@ -220,7 +220,7 @@ class Option : public OptionBase<Option> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a validator
|
/// Adds a validator
|
||||||
Option *check(std::function<bool(std::string)> validator) {
|
Option *check(std::function<bool(std::string &)> validator) {
|
||||||
|
|
||||||
validators_.push_back(validator);
|
validators_.push_back(validator);
|
||||||
return this;
|
return this;
|
||||||
@ -417,7 +417,16 @@ class Option : public OptionBase<Option> {
|
|||||||
///@{
|
///@{
|
||||||
|
|
||||||
/// Process the callback
|
/// 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;
|
bool local_result;
|
||||||
// If take_last, only operate on the final item
|
// If take_last, only operate on the final item
|
||||||
if(last_) {
|
if(last_) {
|
||||||
@ -429,13 +438,6 @@ class Option : public OptionBase<Option> {
|
|||||||
|
|
||||||
if(local_result)
|
if(local_result)
|
||||||
throw ConversionError("Could not convert: " + get_name() + "=" + detail::join(results_));
|
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)
|
/// If options share any of the same names, they are equal (not counting positional)
|
||||||
|
@ -23,7 +23,7 @@ namespace CLI {
|
|||||||
/// @{
|
/// @{
|
||||||
|
|
||||||
/// Check for an existing file
|
/// Check for an existing file
|
||||||
inline bool ExistingFile(std::string filename) {
|
inline bool ExistingFile(const std::string &filename) {
|
||||||
struct stat buffer;
|
struct stat buffer;
|
||||||
bool exist = stat(filename.c_str(), &buffer) == 0;
|
bool exist = stat(filename.c_str(), &buffer) == 0;
|
||||||
bool is_dir = (buffer.st_mode & S_IFDIR) != 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
|
/// Check for an existing directory
|
||||||
inline bool ExistingDirectory(std::string filename) {
|
inline bool ExistingDirectory(const std::string &filename) {
|
||||||
struct stat buffer;
|
struct stat buffer;
|
||||||
bool exist = stat(filename.c_str(), &buffer) == 0;
|
bool exist = stat(filename.c_str(), &buffer) == 0;
|
||||||
bool is_dir = (buffer.st_mode & S_IFDIR) != 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
|
/// Check for a non-existing path
|
||||||
inline bool NonexistentPath(std::string filename) {
|
inline bool NonexistentPath(const std::string &filename) {
|
||||||
struct stat buffer;
|
struct stat buffer;
|
||||||
bool exist = stat(filename.c_str(), &buffer) == 0;
|
bool exist = stat(filename.c_str(), &buffer) == 0;
|
||||||
if(!exist) {
|
if(!exist) {
|
||||||
@ -67,7 +67,7 @@ inline bool NonexistentPath(std::string filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Produce a range validator function
|
/// 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) {
|
return [min, max](std::string input) {
|
||||||
T val;
|
T val;
|
||||||
detail::lexical_cast(input, 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
|
/// 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); }
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user