8#if (defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS != 0) || \
9 (!defined(CLI11_DISABLE_EXTRA_VALIDATORS) || CLI11_DISABLE_EXTRA_VALIDATORS == 0)
14#include "StringTools.hpp"
15#include "Validators.hpp"
22#include <initializer_list>
42class IPV4Validator :
public Validator {
50template <
typename DesiredType>
class TypeValidator :
public Validator {
52 explicit TypeValidator(
const std::string &validator_name)
53 : Validator(validator_name, [](std::string &input_string) {
54 using CLI::detail::lexical_cast;
55 auto val = DesiredType();
56 if(!lexical_cast(input_string, val)) {
57 return std::string(
"Failed parsing ") + input_string +
" as a " + detail::type_name<DesiredType>();
61 TypeValidator() : TypeValidator(detail::type_name<DesiredType>()) {}
74 template <
typename T>
Bound(T min_val, T max_val) {
75 if(detail::is_nan(min_val) || detail::is_nan(max_val)) {
78 std::stringstream out;
79 out << detail::type_name<T>() <<
" bounded to [" << min_val <<
" - " << max_val <<
"]";
82 func_ = [min_val, max_val](std::string &input) {
83 using CLI::detail::lexical_cast;
85 bool converted = lexical_cast(input, val);
87 return std::string(
"Value ") + input +
" could not be converted";
89 if(detail::is_nan(val)) {
90 std::stringstream err;
91 err <<
"Value " << input <<
" cannot be bounded to [";
92 err << min_val <<
" - " << max_val <<
"]";
96 input = detail::to_string(min_val);
97 else if(val > max_val)
98 input = detail::to_string(max_val);
100 return std::string{};
105 template <
typename T>
explicit Bound(T max_val) :
Bound(static_cast<T>(0), max_val) {}
111CLI11_MODULE_INLINE
const detail::IPV4Validator ValidIPV4;
115 enable_if_t<is_copyable_ptr<typename std::remove_reference<T>::type>::value, detail::enabler> = detail::dummy>
116auto smart_deref(T value) ->
decltype(*value) {
122 enable_if_t<!is_copyable_ptr<typename std::remove_reference<T>::type>::value, detail::enabler> = detail::dummy>
123typename std::remove_reference<T>::type &smart_deref(T &value) {
128template <
typename T> std::string generate_set(
const T &set) {
129 using element_t =
typename detail::element_type<T>::type;
130 using iteration_type_t =
typename detail::pair_adaptor<element_t>::value_type;
131 std::string out(1,
'{');
132 out.append(detail::join(
133 detail::smart_deref(set),
141template <
typename T> std::string generate_map(
const T &map,
bool key_only =
false) {
142 using element_t =
typename detail::element_type<T>::type;
143 using iteration_type_t =
typename detail::pair_adaptor<element_t>::value_type;
144 std::string out(1,
'{');
145 out.append(detail::join(
146 detail::smart_deref(map),
147 [key_only](
const iteration_type_t &v) {
162 template <
typename CC,
typename VV>
163 static auto test(
int) ->
decltype(std::declval<CC>().find(std::declval<VV>()), std::true_type());
164 template <
typename,
typename>
static auto test(...) ->
decltype(std::false_type());
166 static const auto value =
decltype(test<C, V>(0))::value;
167 using type = std::integral_constant<bool, value>;
171template <typename T, typename V, enable_if_t<!has_find<T, V>::value, detail::enabler> = detail::dummy>
172auto search(
const T &set,
const V &val) -> std::pair<bool,
decltype(std::begin(detail::smart_deref(set)))> {
173 using element_t =
typename detail::element_type<T>::type;
174 auto &setref = detail::smart_deref(set);
175 auto it = std::find_if(std::begin(setref), std::end(setref), [&val](
decltype(*std::begin(setref)) v) {
178 return {(it != std::end(setref)), it};
182template <typename T, typename V, enable_if_t<has_find<T, V>::value, detail::enabler> = detail::dummy>
183auto search(
const T &set,
const V &val) -> std::pair<bool,
decltype(std::begin(detail::smart_deref(set)))> {
184 auto &setref = detail::smart_deref(set);
185 auto it = setref.find(val);
186 return {(it != std::end(setref)), it};
190template <
typename T,
typename V>
191auto search(
const T &set,
const V &val,
const std::function<V(V)> &filter_function)
192 -> std::pair<bool,
decltype(std::begin(detail::smart_deref(set)))> {
193 using element_t =
typename detail::element_type<T>::type;
195 auto res = search(set, val);
196 if((res.first) || (!(filter_function))) {
200 auto &setref = detail::smart_deref(set);
201 auto it = std::find_if(std::begin(setref), std::end(setref), [&](
decltype(*std::begin(setref)) v) {
203 a = filter_function(a);
206 return {(it != std::end(setref)), it};
213 using filter_fn_t = std::function<std::string(std::string)>;
216 template <
typename T,
typename... Args>
217 IsMember(std::initializer_list<T> values, Args &&...args)
218 :
IsMember(std::vector<T>(values), std::forward<Args>(args)...) {}
221 template <
typename T>
explicit IsMember(T &&set) :
IsMember(std::forward<T>(set), nullptr) {}
225 template <
typename T,
typename F>
explicit IsMember(T set, F filter_function) {
229 using element_t =
typename detail::element_type<T>::type;
230 using item_t =
typename detail::pair_adaptor<element_t>::first_type;
232 using local_item_t =
typename IsMemberType<item_t>::type;
236 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
239 auto shared_set = std::make_shared<T>(std::move(set));
242 desc_function_ = [shared_set]() {
return detail::generate_set(detail::smart_deref(*shared_set)); };
246 func_ = [shared_set, filter_fn](std::string &input) {
247 using CLI::detail::lexical_cast;
249 if(!lexical_cast(input, b)) {
255 auto res = detail::search(*shared_set, b, filter_fn);
263 return std::string{};
267 return input +
" not in " + detail::generate_set(detail::smart_deref(*shared_set));
272 template <
typename T,
typename... Args>
273 IsMember(T &&set, filter_fn_t filter_fn_1, filter_fn_t filter_fn_2, Args &&...other)
275 std::forward<T>(set),
276 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
281template <
typename T>
using TransformPairs = std::vector<std::pair<std::string, T>>;
286 using filter_fn_t = std::function<std::string(std::string)>;
289 template <
typename... Args>
290 Transformer(std::initializer_list<std::pair<std::string, std::string>> values, Args &&...args)
291 :
Transformer(TransformPairs<std::string>(values), std::forward<Args>(args)...) {}
298 template <
typename T,
typename F>
explicit Transformer(T mapping, F filter_function) {
301 "mapping must produce value pairs");
304 using element_t =
typename detail::element_type<T>::type;
305 using item_t =
typename detail::pair_adaptor<element_t>::first_type;
306 using local_item_t =
typename IsMemberType<item_t>::type;
310 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
313 auto shared_mapping = std::make_shared<T>(std::move(mapping));
316 desc_function_ = [shared_mapping]() {
return detail::generate_map(detail::smart_deref(*shared_mapping)); };
318 func_ = [shared_mapping, filter_fn](std::string &input) {
319 using CLI::detail::lexical_cast;
321 if(!lexical_cast(input, b)) {
322 return std::string();
328 auto res = detail::search(*shared_mapping, b, filter_fn);
332 return std::string{};
337 template <
typename T,
typename... Args>
338 Transformer(T &&mapping, filter_fn_t filter_fn_1, filter_fn_t filter_fn_2, Args &&...other)
340 std::forward<T>(mapping),
341 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
348 using filter_fn_t = std::function<std::string(std::string)>;
351 template <
typename... Args>
352 CheckedTransformer(std::initializer_list<std::pair<std::string, std::string>> values, Args &&...args)
353 :
CheckedTransformer(TransformPairs<std::string>(values), std::forward<Args>(args)...) {}
363 "mapping must produce value pairs");
366 using element_t =
typename detail::element_type<T>::type;
367 using item_t =
typename detail::pair_adaptor<element_t>::first_type;
368 using local_item_t =
typename IsMemberType<item_t>::type;
370 using iteration_type_t =
typename detail::pair_adaptor<element_t>::value_type;
373 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
376 auto shared_mapping = std::make_shared<T>(std::move(mapping));
378 auto tfunc = [shared_mapping]() {
379 std::string out(
"value in ");
380 out += detail::generate_map(detail::smart_deref(*shared_mapping)) +
" OR {";
382 detail::smart_deref(*shared_mapping),
383 [](
const iteration_type_t &v) {
393 func_ = [shared_mapping, tfunc, filter_fn](std::string &input) {
394 using CLI::detail::lexical_cast;
396 bool converted = lexical_cast(input, b);
401 auto res = detail::search(*shared_mapping, b, filter_fn);
404 return std::string{};
407 for(
const auto &v : detail::smart_deref(*shared_mapping)) {
409 if(output_string == input) {
410 return std::string();
414 return "Check " + input +
" " + tfunc() +
" FAILED";
419 template <
typename T,
typename... Args>
422 std::forward<T>(mapping),
423 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
428inline std::string ignore_case(std::string item) {
return detail::to_lower(item); }
431inline std::string ignore_underscore(std::string item) {
return detail::remove_underscore(item); }
434inline std::string ignore_space(std::string item) {
435 item.erase(std::remove(std::begin(item), std::end(item),
' '), std::end(item));
436 item.erase(std::remove(std::begin(item), std::end(item),
'\t'), std::end(item));
451class AsNumberWithUnit :
public Validator {
459 CASE_INSENSITIVE = 1,
462 DEFAULT = CASE_INSENSITIVE | UNIT_OPTIONAL
465 template <
typename Number>
468 const std::string &unit_name =
"UNIT") {
469 description(generate_description<Number>(unit_name, opts));
470 validate_mapping(mapping, opts);
473 func_ = [mapping, opts](std::string &input) -> std::string {
476 detail::rtrim(input);
478 return std::string(
"Input is empty");
482 auto unit_begin = input.end();
483 const std::locale loc{};
484 while(unit_begin > input.begin() && std::isalpha(*(unit_begin - 1), loc)) {
488 std::string unit{unit_begin, input.end()};
489 input.resize(
static_cast<std::size_t
>(std::distance(input.begin(), unit_begin)));
492 if(opts & UNIT_REQUIRED && unit.empty()) {
493 return std::string(
"Missing mandatory unit");
495 if(opts & CASE_INSENSITIVE) {
496 unit = detail::to_lower(unit);
499 using CLI::detail::lexical_cast;
500 if(!lexical_cast(input, num)) {
501 return std::string(
"Value ") + input +
" could not be converted to " + detail::type_name<Number>();
508 auto it = mapping.find(unit);
509 if(it == mapping.end()) {
511 " unit not recognized. "
513 detail::generate_map(mapping,
true);
517 using CLI::detail::lexical_cast;
518 bool converted = lexical_cast(input, num);
520 return std::string(
"Value ") + input +
" could not be converted to " + detail::type_name<Number>();
523 bool ok = detail::checked_multiply(num, it->second);
525 return detail::to_string(num) +
" multiplied by " + unit +
526 " factor would cause number overflow. Use smaller value.";
529 num =
static_cast<Number
>(it->second);
532 input = detail::to_string(num);
541 template <
typename Number>
static void validate_mapping(std::map<std::string, Number> &mapping,
Options opts) {
542 for(
auto &kv : mapping) {
543 if(kv.first.empty()) {
544 throw ValidationError(
"Unit must not be empty.");
546 if(!detail::isalpha(kv.first)) {
547 throw ValidationError(
"Unit must contain only letters.");
552 if(opts & CASE_INSENSITIVE) {
553 std::map<std::string, Number> lower_mapping;
554 for(
auto &kv : mapping) {
555 auto s = detail::to_lower(kv.first);
556 if(lower_mapping.count(s)) {
557 throw ValidationError(std::string(
"Several matching lowercase unit representations are found: ") +
560 lower_mapping[std::move(s)] = kv.second;
562 mapping = std::move(lower_mapping);
567 template <
typename Number>
static std::string generate_description(
const std::string &
name,
Options opts) {
568 std::stringstream out;
569 out << detail::type_name<Number>() <<
' ';
570 if(opts & UNIT_REQUIRED) {
573 out <<
'[' <<
name <<
']';
596 using result_t = std::uint64_t;
609 static std::map<std::string, result_t> init_mapping(
bool kb_is_1000);
612 static const std::map<std::string, result_t> &get_mapping(
bool kb_is_1000);
615#if defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS != 0
617#if CLI11_HAS_FILESYSTEM
619enum class Permission : std::uint8_t { none = 0, read = 1, write = 2, exec = 4 };
620class PermissionValidator :
public Validator {
622 explicit PermissionValidator(Permission permission);
626class FileSizeValidator :
public Validator {
628 explicit FileSizeValidator(std::uint64_t min_size, std::uint64_t max_size = 0);
632CLI11_MODULE_INLINE
const detail::PermissionValidator ReadPermissions(detail::Permission::read);
635CLI11_MODULE_INLINE
const detail::PermissionValidator WritePermissions(detail::Permission::write);
638CLI11_MODULE_INLINE
const detail::PermissionValidator ExecPermissions(detail::Permission::exec);
641CLI11_MODULE_INLINE
const FileSizeValidator NonEmptyFile(1, 0);
649#include "impl/ExtraValidators_inl.hpp"
Definition ExtraValidators.hpp:451
Options
Definition ExtraValidators.hpp:457
AsSizeValue(bool kb_is_1000)
Definition ExtraValidators_inl.hpp:60
Bound(T min_val, T max_val)
Definition ExtraValidators.hpp:74
Bound(T max_val)
Range of one value is 0 to value.
Definition ExtraValidators.hpp:105
IsMember(T &&set)
This checks to see if an item is in a set (empty function).
Definition ExtraValidators.hpp:221
IsMember(T set, F filter_function)
Definition ExtraValidators.hpp:225
IsMember(T &&set, filter_fn_t filter_fn_1, filter_fn_t filter_fn_2, Args &&...other)
You can pass in as many filter functions as you like, they nest (string only currently).
Definition ExtraValidators.hpp:273
IsMember(std::initializer_list< T > values, Args &&...args)
This allows in-place construction using an initializer list.
Definition ExtraValidators.hpp:217
Validate the input as a particular type.
Definition ExtraValidators.hpp:50
Thrown when validation of results fails.
Definition Error.hpp:222
Some validators that are provided.
Definition Validators.hpp:55
Validator & description(std::string validator_desc)
Specify the type string.
Definition Validators.hpp:96
Validator & name(std::string validator_name)
Specify the type string.
Definition Validators.hpp:106
std::function< std::string()> desc_function_
This is the description function, if empty the description_ will be used.
Definition Validators.hpp:58
std::function< std::string(std::string &)> func_
Definition Validators.hpp:62
Definition ExtraValidators.hpp:161
Adaptor for set-like structure: This just wraps a normal container in a few utilities that do almost ...
Definition TypeTools.hpp:137
static auto second(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the second value (really just the underlying value).
Definition TypeTools.hpp:147
static auto first(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the first value (really just the underlying value).
Definition TypeTools.hpp:143