1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
// IWYU pragma: private, include "CLI/CLI.hpp"
#include "Error.hpp"
#include "Macros.hpp"
#include "StringTools.hpp"
#include "TypeTools.hpp"
// [CLI11:public_includes:set]
#include <cmath>
#include <cstdint>
#include <functional>
#include <iostream>
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include <vector>
// [CLI11:public_includes:end]
// [CLI11:validators_hpp_filesystem:verbatim]
#if defined CLI11_HAS_FILESYSTEM && CLI11_HAS_FILESYSTEM > 0
#include <filesystem> // NOLINT(build/include)
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
// [CLI11:validators_hpp_filesystem:end]
namespace CLI {
// [CLI11:validators_hpp:verbatim]
class Option;
/// @defgroup validator_group Validators
/// @brief Some validators that are provided
///
/// These are simple `std::string(const std::string&)` validators that are useful. They return
/// a string if the validation fails. A custom struct is provided, as well, with the same user
/// semantics, but with the ability to provide a new type name.
/// @{
///
class Validator {
protected:
/// This is the description function, if empty the description_ will be used
std::function<std::string()> desc_function_{[]() { return std::string{}; }};
/// This is the base function that is to be called.
/// Returns a string error message if validation fails.
std::function<std::string(std::string &)> func_{[](std::string &) { return std::string{}; }};
/// The name for search purposes of the Validator
std::string name_{};
/// A Validator will only apply to an indexed value (-1 is all elements)
int application_index_ = -1;
/// Enable for Validator to allow it to be disabled if need be
bool active_{true};
/// specify that a validator should not modify the input
bool non_modifying_{false};
Validator(std::string validator_desc, std::function<std::string(std::string &)> func)
: desc_function_([validator_desc]() { return validator_desc; }), func_(std::move(func)) {}
public:
Validator() = default;
/// Construct a Validator with just the description string
explicit Validator(std::string validator_desc) : desc_function_([validator_desc]() { return validator_desc; }) {}
/// Construct Validator from basic information
Validator(std::function<std::string(std::string &)> op, std::string validator_desc, std::string validator_name = "")
: desc_function_([validator_desc]() { return validator_desc; }), func_(std::move(op)),
name_(std::move(validator_name)) {}
/// Set the Validator operation function
Validator &operation(std::function<std::string(std::string &)> op) {
func_ = std::move(op);
return *this;
}
/// This is the required operator for a Validator - provided to help
/// users (CLI11 uses the member `func` directly)
std::string operator()(std::string &str) const;
/// This is the required operator for a Validator - provided to help
/// users (CLI11 uses the member `func` directly)
std::string operator()(const std::string &str) const {
std::string value = str;
return (active_) ? func_(value) : std::string{};
}
/// Specify the type string
Validator &description(std::string validator_desc) {
desc_function_ = [validator_desc]() { return validator_desc; };
return *this;
}
/// Specify the type string
CLI11_NODISCARD Validator description(std::string validator_desc) const;
/// Generate type description information for the Validator
CLI11_NODISCARD std::string get_description() const {
if(active_) {
return desc_function_();
}
return std::string{};
}
/// Specify the type string
Validator &name(std::string validator_name) {
name_ = std::move(validator_name);
return *this;
}
/// Specify the type string
CLI11_NODISCARD Validator name(std::string validator_name) const {
Validator newval(*this);
newval.name_ = std::move(validator_name);
return newval;
}
/// Get the name of the Validator
CLI11_NODISCARD const std::string &get_name() const { return name_; }
/// Specify whether the Validator is active or not
Validator &active(bool active_val = true) {
active_ = active_val;
return *this;
}
/// Specify whether the Validator is active or not
CLI11_NODISCARD Validator active(bool active_val = true) const {
Validator newval(*this);
newval.active_ = active_val;
return newval;
}
/// Specify whether the Validator can be modifying or not
Validator &non_modifying(bool no_modify = true) {
non_modifying_ = no_modify;
return *this;
}
/// Specify the application index of a validator
Validator &application_index(int app_index) {
application_index_ = app_index;
return *this;
}
/// Specify the application index of a validator
CLI11_NODISCARD Validator application_index(int app_index) const {
Validator newval(*this);
newval.application_index_ = app_index;
return newval;
}
/// Get the current value of the application index
CLI11_NODISCARD int get_application_index() const { return application_index_; }
/// Get a boolean if the validator is active
CLI11_NODISCARD bool get_active() const { return active_; }
/// Get a boolean if the validator is allowed to modify the input returns true if it can modify the input
CLI11_NODISCARD bool get_modifying() const { return !non_modifying_; }
/// Combining validators is a new validator. Type comes from left validator if function, otherwise only set if the
/// same.
Validator operator&(const Validator &other) const;
/// Combining validators is a new validator. Type comes from left validator if function, otherwise only set if the
/// same.
Validator operator|(const Validator &other) const;
/// Create a validator that fails when a given validator succeeds
Validator operator!() const;
private:
void _merge_description(const Validator &val1, const Validator &val2, const std::string &merger);
};
/// Alias for Validator for custom Validator for clarity
using CustomValidator = Validator;
// The implementation of the built in validators is using the Validator class;
// the user is only expected to use the const (static) versions (since there's no setup).
// Therefore, this is in detail.
namespace detail {
/// CLI enumeration of different file types
enum class path_type : std::uint8_t { nonexistent, file, directory };
/// get the type of the path from a file name
CLI11_INLINE path_type check_path(const char *file) noexcept;
// Static is not needed here, because global const implies static.
/// Check for an existing file (returns error message if check fails)
class ExistingFileValidator : public Validator {
public:
ExistingFileValidator();
};
/// Check for an existing directory (returns error message if check fails)
class ExistingDirectoryValidator : public Validator {
public:
ExistingDirectoryValidator();
};
/// Check for an existing path
class ExistingPathValidator : public Validator {
public:
ExistingPathValidator();
};
/// Check for an non-existing path
class NonexistentPathValidator : public Validator {
public:
NonexistentPathValidator();
};
class EscapedStringTransformer : public Validator {
public:
EscapedStringTransformer();
};
} // namespace detail
/// Check for existing file (returns error message if check fails)
const detail::ExistingFileValidator ExistingFile;
/// Check for an existing directory (returns error message if check fails)
const detail::ExistingDirectoryValidator ExistingDirectory;
/// Check for an existing path
const detail::ExistingPathValidator ExistingPath;
/// Check for an non-existing path
const detail::NonexistentPathValidator NonexistentPath;
/// convert escaped characters into their associated values
const detail::EscapedStringTransformer EscapedString;
/// Modify a path if the file is a particular default location, can be used as Check or transform
/// with the error return optionally disabled
class FileOnDefaultPath : public Validator {
public:
explicit FileOnDefaultPath(std::string default_path, bool enableErrorReturn = true);
};
/// Produce a range (factory). Min and max are inclusive.
class Range : public Validator {
public:
/// This produces a range with min and max inclusive.
///
/// Note that the constructor is templated, but the struct is not, so C++17 is not
/// needed to provide nice syntax for Range(a,b).
template <typename T>
Range(T min_val, T max_val, const std::string &validator_name = std::string{}) : Validator(validator_name) {
if(validator_name.empty()) {
std::stringstream out;
out << detail::type_name<T>() << " in [" << min_val << " - " << max_val << "]";
description(out.str());
}
func_ = [min_val, max_val](std::string &input) {
using CLI::detail::lexical_cast;
T val;
bool converted = lexical_cast(input, 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{};
};
}
/// Range of one value is 0 to value
template <typename T>
explicit Range(T max_val, const std::string &validator_name = std::string{})
: Range(static_cast<T>(0), max_val, validator_name) {}
};
/// Check for a non negative number
const Range NonNegativeNumber((std::numeric_limits<double>::max)(), "NONNEGATIVE");
/// Check for a positive valued number (val>0.0), <double>::min here is the smallest positive number
const Range PositiveNumber((std::numeric_limits<double>::min)(), (std::numeric_limits<double>::max)(), "POSITIVE");
namespace detail {
// the following suggestion was made by Nikita Ofitserov(@himikof)
// done in templates to prevent compiler warnings on negation of unsigned numbers
/// Do a check for overflow on signed numbers
template <typename T>
inline typename std::enable_if<std::is_signed<T>::value, T>::type overflowCheck(const T &a, const T &b) {
if((a > 0) == (b > 0)) {
return ((std::numeric_limits<T>::max)() / (std::abs)(a) < (std::abs)(b));
}
return ((std::numeric_limits<T>::min)() / (std::abs)(a) > -(std::abs)(b));
}
/// Do a check for overflow on unsigned numbers
template <typename T>
inline typename std::enable_if<!std::is_signed<T>::value, T>::type overflowCheck(const T &a, const T &b) {
return ((std::numeric_limits<T>::max)() / a < b);
}
/// Performs a *= b; if it doesn't cause integer overflow. Returns false otherwise.
template <typename T> typename std::enable_if<std::is_integral<T>::value, bool>::type checked_multiply(T &a, T b) {
if(a == 0 || b == 0 || a == 1 || b == 1) {
a *= b;
return true;
}
if(a == (std::numeric_limits<T>::min)() || b == (std::numeric_limits<T>::min)()) {
return false;
}
if(overflowCheck(a, b)) {
return false;
}
a *= b;
return true;
}
/// Performs a *= b; if it doesn't equal infinity. Returns false otherwise.
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, bool>::type checked_multiply(T &a, T b) {
T c = a * b;
if(std::isinf(c) && !std::isinf(a) && !std::isinf(b)) {
return false;
}
a = c;
return true;
}
/// Split a string into a program name and command line arguments
/// the string is assumed to contain a file name followed by other arguments
/// the return value contains is a pair with the first argument containing the program name and the second
/// everything else.
CLI11_INLINE std::pair<std::string, std::string> split_program_name(std::string commandline);
} // namespace detail
/// @}
// [CLI11:validators_hpp:end]
} // namespace CLI
#ifndef CLI11_COMPILE
#include "impl/Validators_inl.hpp" // IWYU pragma: export
#endif
|