30#if defined CLI11_HAS_FILESYSTEM && CLI11_HAS_FILESYSTEM > 0
61 std::function<std::string(std::string &)>
func_{[](std::string &) {
return std::string{}; }};
71 Validator(std::string validator_desc, std::function<std::string(std::string &)> func)
79 Validator(std::function<std::string(std::string &)> op, std::string validator_desc, std::string validator_name =
"")
81 name_(std::move(validator_name)) {}
84 func_ = std::move(op);
94 std::string value = str;
111 return std::string{};
115 name_ = std::move(validator_name);
121 newval.
name_ = std::move(validator_name);
174 void _merge_description(
const Validator &val1,
const Validator &val2,
const std::string &merger);
253 template <
typename T>
254 Range(T min_val, T max_val,
const std::string &validator_name = std::string{}) :
Validator(validator_name) {
255 if(validator_name.empty()) {
256 std::stringstream out;
257 out << detail::type_name<T>() <<
" in [" << min_val <<
" - " << max_val <<
"]";
258 description(out.str());
261 func_ = [min_val, max_val](std::string &input) {
262 using CLI::detail::lexical_cast;
264 bool converted = lexical_cast(input, val);
265 if((!converted) || (val < min_val || val > max_val)) {
266 std::stringstream out;
267 out <<
"Value " << input <<
" not in range [";
268 out << min_val <<
" - " << max_val <<
"]";
271 return std::string{};
276 template <
typename T>
277 explicit Range(T max_val,
const std::string &validator_name = std::string{})
278 :
Range(static_cast<T>(0), max_val, validator_name) {}
286 Range PositiveNumber((std::numeric_limits<double>::min)(), (std::numeric_limits<double>::max)(),
"POSITIVE");
294inline typename std::enable_if<std::is_signed<T>::value, T>::type
overflowCheck(
const T &a,
const T &b) {
295 if((a > 0) == (b > 0)) {
296 return ((std::numeric_limits<T>::max)() / (std::abs)(a) < (std::abs)(b));
298 return ((std::numeric_limits<T>::min)() / (std::abs)(a) > -(std::abs)(b));
302inline typename std::enable_if<!std::is_signed<T>::value, T>::type
overflowCheck(
const T &a,
const T &b) {
303 return ((std::numeric_limits<T>::max)() / a < b);
307template <
typename T>
typename std::enable_if<std::is_integral<T>::value,
bool>::type
checked_multiply(T &a, T b) {
308 if(a == 0 || b == 0 || a == 1 || b == 1) {
312 if(a == (std::numeric_limits<T>::min)() || b == (std::numeric_limits<T>::min)()) {
324typename std::enable_if<std::is_floating_point<T>::value,
bool>::type
checked_multiply(T &a, T b) {
326 if(std::isinf(c) && !std::isinf(a) && !std::isinf(b)) {
345#include "impl/Validators_inl.hpp"
#define CLI11_MODULE_INLINE
Definition Macros.hpp:183
#define CLI11_INLINE
Definition Macros.hpp:176
#define CLI11_NODISCARD
Definition Macros.hpp:58
FileOnDefaultPath(std::string default_path, bool enableErrorReturn=true)
Definition Option.hpp:259
Produce a range (factory). Min and max are inclusive.
Definition Validators.hpp:247
Range(T min_val, T max_val, const std::string &validator_name=std::string{})
Definition Validators.hpp:254
Range(T max_val, const std::string &validator_name=std::string{})
Range of one value is 0 to value.
Definition Validators.hpp:277
Some validators that are provided.
Definition Validators.hpp:54
CLI11_NODISCARD int get_application_index() const
Get the current value of the application index.
Definition Validators.hpp:155
int application_index_
A Validator will only apply to an indexed value (-1 is all elements)
Definition Validators.hpp:65
Validator & non_modifying(bool no_modify=true)
Specify whether the Validator can be modifying or not.
Definition Validators.hpp:139
Validator & description(std::string validator_desc)
Specify the type string.
Definition Validators.hpp:99
std::string operator()(const std::string &str) const
Definition Validators.hpp:93
CLI11_NODISCARD Validator application_index(int app_index) const
Specify the application index of a validator.
Definition Validators.hpp:149
CLI11_NODISCARD bool get_active() const
Get a boolean if the validator is active.
Definition Validators.hpp:157
Validator operator&(const Validator &other) const
bool active_
Enable for Validator to allow it to be disabled if need be.
Definition Validators.hpp:67
bool non_modifying_
specify that a validator should not modify the input
Definition Validators.hpp:69
Validator(std::string validator_desc)
Construct a Validator with just the description string.
Definition Validators.hpp:77
Validator(std::string validator_desc, std::function< std::string(std::string &)> func)
Definition Validators.hpp:71
CLI11_NODISCARD Validator description(std::string validator_desc) const
Specify the type string.
Validator & name(std::string validator_name)
Specify the type string.
Definition Validators.hpp:114
std::string operator()(std::string &str) const
std::function< std::string()> desc_function_
This is the description function, if empty the description_ will be used.
Definition Validators.hpp:57
std::function< std::string(std::string &)> func_
Definition Validators.hpp:61
CLI11_NODISCARD std::string get_description() const
Generate type description information for the Validator.
Definition Validators.hpp:107
Validator operator!() const
Create a validator that fails when a given validator succeeds.
CLI11_NODISCARD Validator active(bool active_val=true) const
Specify whether the Validator is active or not.
Definition Validators.hpp:132
CLI11_NODISCARD const std::string & get_name() const
Get the name of the Validator.
Definition Validators.hpp:125
Validator operator|(const Validator &other) const
Validator & active(bool active_val=true)
Specify whether the Validator is active or not.
Definition Validators.hpp:127
std::string name_
The name for search purposes of the Validator.
Definition Validators.hpp:63
CLI11_NODISCARD bool get_modifying() const
Get a boolean if the validator is allowed to modify the input returns true if it can modify the input...
Definition Validators.hpp:160
CLI11_NODISCARD Validator name(std::string validator_name) const
Specify the type string.
Definition Validators.hpp:119
Validator & application_index(int app_index)
Specify the application index of a validator.
Definition Validators.hpp:144
Validator(std::function< std::string(std::string &)> op, std::string validator_desc, std::string validator_name="")
Construct Validator from basic information.
Definition Validators.hpp:79
Validator & operation(std::function< std::string(std::string &)> op)
Set the Validator operation function.
Definition Validators.hpp:83
Check for an existing directory (returns error message if check fails)
Definition Validators.hpp:200
ExistingDirectoryValidator()
Check for an existing file (returns error message if check fails)
Definition Validators.hpp:194
Check for an existing path.
Definition Validators.hpp:206
Check for an non-existing path.
Definition Validators.hpp:212
NonexistentPathValidator()
std::enable_if< std::is_signed< T >::value, T >::type overflowCheck(const T &a, const T &b)
Do a check for overflow on signed numbers.
Definition Validators.hpp:294
std::enable_if< std::is_integral< T >::value, bool >::type checked_multiply(T &a, T b)
Performs a *= b; if it doesn't cause integer overflow. Returns false otherwise.
Definition Validators.hpp:307
CLI11_INLINE path_type check_path(const char *file) noexcept
get the type of the path from a file name
CLI11_INLINE std::pair< std::string, std::string > split_program_name(std::string commandline)
path_type
CLI enumeration of different file types.
Definition Validators.hpp:186
@ nonexistent
Definition Validators.hpp:186
@ directory
Definition Validators.hpp:186
@ file
Definition Validators.hpp:186
CLI11_MODULE_INLINE const detail::NonexistentPathValidator NonexistentPath
Check for an non-existing path.
Definition Validators.hpp:234
CLI11_MODULE_INLINE const detail::EscapedStringTransformer EscapedString
convert escaped characters into their associated values
Definition Validators.hpp:237
CLI11_MODULE_INLINE const Range PositiveNumber((std::numeric_limits< double >::min)(),(std::numeric_limits< double >::max)(), "POSITIVE")
Check for a positive valued number (val>0.0), <double>min here is the smallest positive number.
Validator CustomValidator
Alias for Validator for custom Validator for clarity.
Definition Validators.hpp:178
CLI11_MODULE_INLINE const detail::ExistingFileValidator ExistingFile
Check for existing file (returns error message if check fails)
Definition Validators.hpp:225
CLI11_MODULE_INLINE const detail::ExistingPathValidator ExistingPath
Check for an existing path.
Definition Validators.hpp:231
CLI11_MODULE_INLINE const detail::ExistingDirectoryValidator ExistingDirectory
Check for an existing directory (returns error message if check fails)
Definition Validators.hpp:228
CLI11_MODULE_INLINE const Range NonNegativeNumber((std::numeric_limits< double >::max)(), "NONNEGATIVE")
Check for a non negative number.