8#if (defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS == 1) || \
9 (!defined(CLI11_DISABLE_EXTRA_VALIDATORS) || CLI11_DISABLE_EXTRA_VALIDATORS == 0)
14#include "StringTools.hpp"
15#include "Validators.hpp"
49 :
Validator(validator_name, [](std::string &input_string) {
50 using CLI::detail::lexical_cast;
51 auto val = DesiredType();
52 if(!lexical_cast(input_string, val)) {
53 return std::string(
"Failed parsing ") + input_string +
" as a " + detail::type_name<DesiredType>();
70 template <
typename T>
Bound(T min_val, T max_val) {
71 std::stringstream out;
72 out << detail::type_name<T>() <<
" bounded to [" << min_val <<
" - " << max_val <<
"]";
75 func_ = [min_val, max_val](std::string &input) {
76 using CLI::detail::lexical_cast;
78 bool converted = lexical_cast(input, val);
80 return std::string(
"Value ") + input +
" could not be converted";
83 input = detail::to_string(min_val);
84 else if(val > max_val)
85 input = detail::to_string(max_val);
92 template <
typename T>
explicit Bound(T max_val) :
Bound(static_cast<T>(0), max_val) {}
98const detail::IPV4Validator ValidIPV4;
102 enable_if_t<is_copyable_ptr<typename std::remove_reference<T>::type>::value, detail::enabler> = detail::dummy>
103auto smart_deref(T value) ->
decltype(*value) {
109 enable_if_t<!is_copyable_ptr<typename std::remove_reference<T>::type>::value, detail::enabler> = detail::dummy>
110typename std::remove_reference<T>::type &smart_deref(T &value) {
115template <
typename T> std::string generate_set(
const T &set) {
116 using element_t =
typename detail::element_type<T>::type;
117 using iteration_type_t =
typename detail::pair_adaptor<element_t>::value_type;
118 std::string out(1,
'{');
119 out.append(detail::join(
120 detail::smart_deref(set),
128template <
typename T> std::string generate_map(
const T &map,
bool key_only =
false) {
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(map),
134 [key_only](
const iteration_type_t &v) {
149 template <
typename CC,
typename VV>
150 static auto test(
int) ->
decltype(std::declval<CC>().find(std::declval<VV>()), std::true_type());
151 template <
typename,
typename>
static auto test(...) ->
decltype(std::false_type());
153 static const auto value =
decltype(test<C, V>(0))::value;
154 using type = std::integral_constant<bool, value>;
158template <typename T, typename V, enable_if_t<!has_find<T, V>::value, detail::enabler> = detail::dummy>
159auto search(
const T &set,
const V &val) -> std::pair<bool,
decltype(std::begin(detail::smart_deref(set)))> {
160 using element_t =
typename detail::element_type<T>::type;
161 auto &setref = detail::smart_deref(set);
162 auto it = std::find_if(std::begin(setref), std::end(setref), [&val](
decltype(*std::begin(setref)) v) {
165 return {(it != std::end(setref)), it};
169template <typename T, typename V, enable_if_t<has_find<T, V>::value, detail::enabler> = detail::dummy>
170auto search(
const T &set,
const V &val) -> std::pair<bool,
decltype(std::begin(detail::smart_deref(set)))> {
171 auto &setref = detail::smart_deref(set);
172 auto it = setref.find(val);
173 return {(it != std::end(setref)), it};
177template <
typename T,
typename V>
178auto search(
const T &set,
const V &val,
const std::function<V(V)> &filter_function)
179 -> std::pair<bool,
decltype(std::begin(detail::smart_deref(set)))> {
180 using element_t =
typename detail::element_type<T>::type;
182 auto res = search(set, val);
183 if((res.first) || (!(filter_function))) {
187 auto &setref = detail::smart_deref(set);
188 auto it = std::find_if(std::begin(setref), std::end(setref), [&](
decltype(*std::begin(setref)) v) {
190 a = filter_function(a);
193 return {(it != std::end(setref)), it};
200 using filter_fn_t = std::function<std::string(std::string)>;
203 template <
typename T,
typename... Args>
204 IsMember(std::initializer_list<T> values, Args &&...args)
205 :
IsMember(std::vector<T>(values), std::forward<Args>(args)...) {}
208 template <
typename T>
explicit IsMember(T &&set) :
IsMember(std::forward<T>(set), nullptr) {}
212 template <
typename T,
typename F>
explicit IsMember(T set, F filter_function) {
216 using element_t =
typename detail::element_type<T>::type;
217 using item_t =
typename detail::pair_adaptor<element_t>::first_type;
219 using local_item_t =
typename IsMemberType<item_t>::type;
223 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
226 desc_function_ = [set]() {
return detail::generate_set(detail::smart_deref(set)); };
230 func_ = [set, filter_fn](std::string &input) {
231 using CLI::detail::lexical_cast;
233 if(!lexical_cast(input, b)) {
239 auto res = detail::search(set, b, filter_fn);
247 return std::string{};
251 return input +
" not in " + detail::generate_set(detail::smart_deref(set));
256 template <
typename T,
typename... Args>
257 IsMember(T &&set, filter_fn_t filter_fn_1, filter_fn_t filter_fn_2, Args &&...other)
259 std::forward<T>(set),
260 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
265template <
typename T>
using TransformPairs = std::vector<std::pair<std::string, T>>;
270 using filter_fn_t = std::function<std::string(std::string)>;
273 template <
typename... Args>
274 Transformer(std::initializer_list<std::pair<std::string, std::string>> values, Args &&...args)
275 :
Transformer(TransformPairs<std::string>(values), std::forward<Args>(args)...) {}
282 template <
typename T,
typename F>
explicit Transformer(T mapping, F filter_function) {
285 "mapping must produce value pairs");
288 using element_t =
typename detail::element_type<T>::type;
289 using item_t =
typename detail::pair_adaptor<element_t>::first_type;
290 using local_item_t =
typename IsMemberType<item_t>::type;
294 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
297 desc_function_ = [mapping]() {
return detail::generate_map(detail::smart_deref(mapping)); };
299 func_ = [mapping, filter_fn](std::string &input) {
300 using CLI::detail::lexical_cast;
302 if(!lexical_cast(input, b)) {
303 return std::string();
309 auto res = detail::search(mapping, b, filter_fn);
313 return std::string{};
318 template <
typename T,
typename... Args>
319 Transformer(T &&mapping, filter_fn_t filter_fn_1, filter_fn_t filter_fn_2, Args &&...other)
321 std::forward<T>(mapping),
322 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
329 using filter_fn_t = std::function<std::string(std::string)>;
332 template <
typename... Args>
333 CheckedTransformer(std::initializer_list<std::pair<std::string, std::string>> values, Args &&...args)
334 :
CheckedTransformer(TransformPairs<std::string>(values), std::forward<Args>(args)...) {}
344 "mapping must produce value pairs");
347 using element_t =
typename detail::element_type<T>::type;
348 using item_t =
typename detail::pair_adaptor<element_t>::first_type;
349 using local_item_t =
typename IsMemberType<item_t>::type;
351 using iteration_type_t =
typename detail::pair_adaptor<element_t>::value_type;
354 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
356 auto tfunc = [mapping]() {
357 std::string out(
"value in ");
358 out += detail::generate_map(detail::smart_deref(mapping)) +
" OR {";
360 detail::smart_deref(mapping),
369 func_ = [mapping, tfunc, filter_fn](std::string &input) {
370 using CLI::detail::lexical_cast;
372 bool converted = lexical_cast(input, b);
377 auto res = detail::search(mapping, b, filter_fn);
380 return std::string{};
383 for(
const auto &v : detail::smart_deref(mapping)) {
385 if(output_string == input) {
386 return std::string();
390 return "Check " + input +
" " + tfunc() +
" FAILED";
395 template <
typename T,
typename... Args>
398 std::forward<T>(mapping),
399 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
404inline std::string ignore_case(std::string item) {
return detail::to_lower(item); }
407inline std::string ignore_underscore(std::string item) {
return detail::remove_underscore(item); }
410inline std::string ignore_space(std::string item) {
411 item.erase(std::remove(std::begin(item), std::end(item),
' '), std::end(item));
412 item.erase(std::remove(std::begin(item), std::end(item),
'\t'), std::end(item));
435 CASE_INSENSITIVE = 1,
438 DEFAULT = CASE_INSENSITIVE | UNIT_OPTIONAL
441 template <
typename Number>
444 const std::string &unit_name =
"UNIT") {
445 description(generate_description<Number>(unit_name, opts));
446 validate_mapping(mapping, opts);
449 func_ = [mapping, opts](std::string &input) -> std::string {
452 detail::rtrim(input);
454 throw ValidationError(
"Input is empty");
458 auto unit_begin = input.end();
459 while(unit_begin > input.begin() && std::isalpha(*(unit_begin - 1), std::locale())) {
463 std::string unit{unit_begin, input.end()};
464 input.resize(
static_cast<std::size_t
>(std::distance(input.begin(), unit_begin)));
467 if(opts & UNIT_REQUIRED && unit.empty()) {
468 throw ValidationError(
"Missing mandatory unit");
470 if(opts & CASE_INSENSITIVE) {
471 unit = detail::to_lower(unit);
474 using CLI::detail::lexical_cast;
475 if(!lexical_cast(input, num)) {
476 throw ValidationError(std::string(
"Value ") + input +
" could not be converted to " +
477 detail::type_name<Number>());
484 auto it = mapping.find(unit);
485 if(it == mapping.end()) {
486 throw ValidationError(unit +
487 " unit not recognized. "
489 detail::generate_map(mapping,
true));
493 using CLI::detail::lexical_cast;
494 bool converted = lexical_cast(input, num);
496 throw ValidationError(std::string(
"Value ") + input +
" could not be converted to " +
497 detail::type_name<Number>());
500 bool ok = detail::checked_multiply(num, it->second);
502 throw ValidationError(detail::to_string(num) +
" multiplied by " + unit +
503 " factor would cause number overflow. Use smaller value.");
506 num =
static_cast<Number
>(it->second);
509 input = detail::to_string(num);
518 template <
typename Number>
static void validate_mapping(std::map<std::string, Number> &mapping,
Options opts) {
519 for(
auto &kv : mapping) {
520 if(kv.first.empty()) {
521 throw ValidationError(
"Unit must not be empty.");
523 if(!detail::isalpha(kv.first)) {
524 throw ValidationError(
"Unit must contain only letters.");
529 if(opts & CASE_INSENSITIVE) {
530 std::map<std::string, Number> lower_mapping;
531 for(
auto &kv : mapping) {
532 auto s = detail::to_lower(kv.first);
533 if(lower_mapping.count(s)) {
534 throw ValidationError(std::string(
"Several matching lowercase unit representations are found: ") +
537 lower_mapping[detail::to_lower(kv.first)] = kv.second;
539 mapping = std::move(lower_mapping);
544 template <
typename Number>
static std::string generate_description(
const std::string &
name,
Options opts) {
545 std::stringstream out;
546 out << detail::type_name<Number>() <<
' ';
547 if(opts & UNIT_REQUIRED) {
550 out <<
'[' <<
name <<
']';
573 using result_t = std::uint64_t;
586 static std::map<std::string, result_t> init_mapping(
bool kb_is_1000);
589 static std::map<std::string, result_t> get_mapping(
bool kb_is_1000);
592#if defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS != 0
600#include "impl/ExtraValidators_inl.hpp"
Definition ExtraValidators.hpp:427
Options
Definition ExtraValidators.hpp:433
Definition ExtraValidators.hpp:571
AsSizeValue(bool kb_is_1000)
Definition ExtraValidators_inl.hpp:59
Produce a bounded range (factory). Min and max are inclusive.
Definition ExtraValidators.hpp:64
Bound(T min_val, T max_val)
Definition ExtraValidators.hpp:70
Bound(T max_val)
Range of one value is 0 to value.
Definition ExtraValidators.hpp:92
Verify items are in a set.
Definition ExtraValidators.hpp:198
IsMember(T &&set)
This checks to see if an item is in a set (empty function)
Definition ExtraValidators.hpp:208
IsMember(T set, F filter_function)
Definition ExtraValidators.hpp:212
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:257
IsMember(std::initializer_list< T > values, Args &&...args)
This allows in-place construction using an initializer list.
Definition ExtraValidators.hpp:204
Validate the input as a particular type.
Definition ExtraValidators.hpp:46
Thrown when validation of results fails.
Definition Error.hpp:221
Some validators that are provided.
Definition Validators.hpp:54
Validator & description(std::string validator_desc)
Specify the type string.
Definition Validators.hpp:99
Validator & name(std::string validator_name)
Specify the type string.
Definition Validators.hpp:114
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
Validate the given string is a legal ipv4 address.
Definition ExtraValidators.hpp:38
Definition ExtraValidators.hpp:148
Adaptor for set-like structure: This just wraps a normal container in a few utilities that do almost ...
Definition TypeTools.hpp:130
static auto second(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the second value (really just the underlying value)
Definition TypeTools.hpp:140
static auto first(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the first value (really just the underlying value)
Definition TypeTools.hpp:136