13#include "StringTools.hpp"
14#include "TypeTools.hpp"
31#if defined CLI11_HAS_FILESYSTEM && CLI11_HAS_FILESYSTEM > 0
62 std::function<std::string(std::string &)>
func_{[](std::string &) {
return std::string{}; }};
72 Validator(std::string validator_desc, std::function<std::string(std::string &)> func)
76 Validator() =
default;
80 Validator(std::function<std::string(std::string &)> op, std::string validator_desc, std::string validator_name =
"")
82 name_(std::move(validator_name)) {}
85 func_ = std::move(op);
90 std::string
operator()(std::string &str)
const;
95 std::string value = str;
112 return std::string{};
116 name_ = std::move(validator_name);
122 newval.name_ = std::move(validator_name);
135 newval.active_ = active_val;
152 newval.application_index_ = app_index;
175 void _merge_description(
const Validator &val1,
const Validator &val2,
const std::string &merger);
188enum class path_type { nonexistent, file, directory };
191CLI11_INLINE path_type check_path(
const char *file)
noexcept;
254 :
Validator(validator_name, [](std::string &input_string) {
255 using CLI::detail::lexical_cast;
256 auto val = DesiredType();
257 if(!lexical_cast(input_string, val)) {
258 return std::string(
"Failed parsing ") + input_string +
" as a " + detail::type_name<DesiredType>();
260 return std::string();
266const TypeValidator<double> Number(
"NUMBER");
272 explicit FileOnDefaultPath(std::string default_path,
bool enableErrorReturn =
true);
282 template <
typename T>
283 Range(T min_val, T max_val,
const std::string &validator_name = std::string{}) :
Validator(validator_name) {
284 if(validator_name.empty()) {
285 std::stringstream out;
286 out << detail::type_name<T>() <<
" in [" << min_val <<
" - " << max_val <<
"]";
290 func_ = [min_val, max_val](std::string &input) {
291 using CLI::detail::lexical_cast;
293 bool converted = lexical_cast(input, val);
294 if((!converted) || (val < min_val || val > max_val)) {
295 std::stringstream out;
296 out <<
"Value " << input <<
" not in range [";
297 out << min_val <<
" - " << max_val <<
"]";
300 return std::string{};
305 template <
typename T>
306 explicit Range(T max_val,
const std::string &validator_name = std::string{})
307 :
Range(static_cast<T>(0), max_val, validator_name) {}
311const Range NonNegativeNumber((std::numeric_limits<double>::max)(),
"NONNEGATIVE");
314const Range PositiveNumber((std::numeric_limits<double>::min)(), (std::numeric_limits<double>::max)(),
"POSITIVE");
323 template <
typename T>
Bound(T min_val, T max_val) {
324 std::stringstream out;
325 out << detail::type_name<T>() <<
" bounded to [" << min_val <<
" - " << max_val <<
"]";
328 func_ = [min_val, max_val](std::string &input) {
329 using CLI::detail::lexical_cast;
331 bool converted = lexical_cast(input, val);
333 return std::string(
"Value ") + input +
" could not be converted";
336 input = detail::to_string(min_val);
337 else if(val > max_val)
338 input = detail::to_string(max_val);
340 return std::string{};
345 template <
typename T>
explicit Bound(T max_val) :
Bound(static_cast<T>(0), max_val) {}
350 enable_if_t<is_copyable_ptr<typename std::remove_reference<T>::type>::value, detail::enabler> = detail::dummy>
351auto smart_deref(T value) ->
decltype(*value) {
357 enable_if_t<!is_copyable_ptr<typename std::remove_reference<T>::type>::value, detail::enabler> = detail::dummy>
358typename std::remove_reference<T>::type &smart_deref(T &value) {
362template <
typename T> std::string generate_set(
const T &set) {
363 using element_t =
typename detail::element_type<T>::type;
364 using iteration_type_t =
typename detail::pair_adaptor<element_t>::value_type;
365 std::string out(1,
'{');
366 out.append(detail::join(
367 detail::smart_deref(set),
375template <
typename T> std::string generate_map(
const T &map,
bool key_only =
false) {
376 using element_t =
typename detail::element_type<T>::type;
377 using iteration_type_t =
typename detail::pair_adaptor<element_t>::value_type;
378 std::string out(1,
'{');
379 out.append(detail::join(
380 detail::smart_deref(map),
381 [key_only](
const iteration_type_t &v) {
396 template <
typename CC,
typename VV>
397 static auto test(
int) ->
decltype(std::declval<CC>().find(std::declval<VV>()), std::true_type());
398 template <
typename,
typename>
static auto test(...) ->
decltype(std::false_type());
400 static const auto value =
decltype(test<C, V>(0))::value;
401 using type = std::integral_constant<bool, value>;
405template <typename T, typename V, enable_if_t<!has_find<T, V>::value, detail::enabler> = detail::dummy>
406auto search(
const T &set,
const V &val) -> std::pair<bool,
decltype(std::begin(detail::smart_deref(set)))> {
407 using element_t =
typename detail::element_type<T>::type;
408 auto &setref = detail::smart_deref(set);
409 auto it = std::find_if(std::begin(setref), std::end(setref), [&val](
decltype(*std::begin(setref)) v) {
412 return {(it != std::end(setref)), it};
416template <typename T, typename V, enable_if_t<has_find<T, V>::value, detail::enabler> = detail::dummy>
417auto search(
const T &set,
const V &val) -> std::pair<bool,
decltype(std::begin(detail::smart_deref(set)))> {
418 auto &setref = detail::smart_deref(set);
419 auto it = setref.find(val);
420 return {(it != std::end(setref)), it};
424template <
typename T,
typename V>
425auto search(
const T &set,
const V &val,
const std::function<V(V)> &filter_function)
426 -> std::pair<bool,
decltype(std::begin(detail::smart_deref(set)))> {
427 using element_t =
typename detail::element_type<T>::type;
429 auto res = search(set, val);
430 if((res.first) || (!(filter_function))) {
434 auto &setref = detail::smart_deref(set);
435 auto it = std::find_if(std::begin(setref), std::end(setref), [&](
decltype(*std::begin(setref)) v) {
437 a = filter_function(a);
440 return {(it != std::end(setref)), it};
448inline typename std::enable_if<std::is_signed<T>::value, T>::type overflowCheck(
const T &a,
const T &b) {
449 if((a > 0) == (b > 0)) {
450 return ((std::numeric_limits<T>::max)() / (std::abs)(a) < (std::abs)(b));
452 return ((std::numeric_limits<T>::min)() / (std::abs)(a) > -(std::abs)(b));
456inline typename std::enable_if<!std::is_signed<T>::value, T>::type overflowCheck(
const T &a,
const T &b) {
457 return ((std::numeric_limits<T>::max)() / a < b);
461template <
typename T>
typename std::enable_if<std::is_integral<T>::value,
bool>::type checked_multiply(T &a, T b) {
462 if(a == 0 || b == 0 || a == 1 || b == 1) {
466 if(a == (std::numeric_limits<T>::min)() || b == (std::numeric_limits<T>::min)()) {
469 if(overflowCheck(a, b)) {
478typename std::enable_if<std::is_floating_point<T>::value,
bool>::type checked_multiply(T &a, T b) {
480 if(std::isinf(c) && !std::isinf(a) && !std::isinf(b)) {
491 using filter_fn_t = std::function<std::string(std::string)>;
494 template <
typename T,
typename... Args>
495 IsMember(std::initializer_list<T> values, Args &&...args)
496 :
IsMember(std::vector<T>(values), std::forward<Args>(args)...) {}
499 template <
typename T>
explicit IsMember(T &&set) :
IsMember(std::forward<T>(set), nullptr) {}
503 template <
typename T,
typename F>
explicit IsMember(T set, F filter_function) {
507 using element_t =
typename detail::element_type<T>::type;
508 using item_t =
typename detail::pair_adaptor<element_t>::first_type;
510 using local_item_t =
typename IsMemberType<item_t>::type;
514 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
517 desc_function_ = [set]() {
return detail::generate_set(detail::smart_deref(set)); };
521 func_ = [set, filter_fn](std::string &input) {
522 using CLI::detail::lexical_cast;
524 if(!lexical_cast(input, b)) {
530 auto res = detail::search(set, b, filter_fn);
538 return std::string{};
542 return input +
" not in " + detail::generate_set(detail::smart_deref(set));
547 template <
typename T,
typename... Args>
548 IsMember(T &&set, filter_fn_t filter_fn_1, filter_fn_t filter_fn_2, Args &&...other)
550 std::forward<T>(set),
551 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
556template <
typename T>
using TransformPairs = std::vector<std::pair<std::string, T>>;
561 using filter_fn_t = std::function<std::string(std::string)>;
564 template <
typename... Args>
565 Transformer(std::initializer_list<std::pair<std::string, std::string>> values, Args &&...args)
566 :
Transformer(TransformPairs<std::string>(values), std::forward<Args>(args)...) {}
573 template <
typename T,
typename F>
explicit Transformer(T mapping, F filter_function) {
576 "mapping must produce value pairs");
579 using element_t =
typename detail::element_type<T>::type;
580 using item_t =
typename detail::pair_adaptor<element_t>::first_type;
581 using local_item_t =
typename IsMemberType<item_t>::type;
585 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
588 desc_function_ = [mapping]() {
return detail::generate_map(detail::smart_deref(mapping)); };
590 func_ = [mapping, filter_fn](std::string &input) {
591 using CLI::detail::lexical_cast;
593 if(!lexical_cast(input, b)) {
594 return std::string();
600 auto res = detail::search(mapping, b, filter_fn);
604 return std::string{};
609 template <
typename T,
typename... Args>
610 Transformer(T &&mapping, filter_fn_t filter_fn_1, filter_fn_t filter_fn_2, Args &&...other)
612 std::forward<T>(mapping),
613 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
620 using filter_fn_t = std::function<std::string(std::string)>;
623 template <
typename... Args>
624 CheckedTransformer(std::initializer_list<std::pair<std::string, std::string>> values, Args &&...args)
625 :
CheckedTransformer(TransformPairs<std::string>(values), std::forward<Args>(args)...) {}
635 "mapping must produce value pairs");
638 using element_t =
typename detail::element_type<T>::type;
639 using item_t =
typename detail::pair_adaptor<element_t>::first_type;
640 using local_item_t =
typename IsMemberType<item_t>::type;
642 using iteration_type_t =
typename detail::pair_adaptor<element_t>::value_type;
645 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
647 auto tfunc = [mapping]() {
648 std::string out(
"value in ");
649 out += detail::generate_map(detail::smart_deref(mapping)) +
" OR {";
651 detail::smart_deref(mapping),
660 func_ = [mapping, tfunc, filter_fn](std::string &input) {
661 using CLI::detail::lexical_cast;
663 bool converted = lexical_cast(input, b);
668 auto res = detail::search(mapping, b, filter_fn);
671 return std::string{};
674 for(
const auto &v : detail::smart_deref(mapping)) {
676 if(output_string == input) {
677 return std::string();
681 return "Check " + input +
" " + tfunc() +
" FAILED";
686 template <
typename T,
typename... Args>
689 std::forward<T>(mapping),
690 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
695inline std::string ignore_case(std::string item) {
return detail::to_lower(item); }
698inline std::string ignore_underscore(std::string item) {
return detail::remove_underscore(item); }
701inline std::string ignore_space(std::string item) {
702 item.erase(std::remove(std::begin(item), std::end(item),
' '), std::end(item));
703 item.erase(std::remove(std::begin(item), std::end(item),
'\t'), std::end(item));
726 CASE_INSENSITIVE = 1,
729 DEFAULT = CASE_INSENSITIVE | UNIT_OPTIONAL
732 template <
typename Number>
735 const std::string &unit_name =
"UNIT") {
736 description(generate_description<Number>(unit_name, opts));
737 validate_mapping(mapping, opts);
740 func_ = [mapping, opts](std::string &input) -> std::string {
743 detail::rtrim(input);
745 throw ValidationError(
"Input is empty");
749 auto unit_begin = input.end();
750 while(unit_begin > input.begin() && std::isalpha(*(unit_begin - 1), std::locale())) {
754 std::string unit{unit_begin, input.end()};
755 input.resize(
static_cast<std::size_t
>(std::distance(input.begin(), unit_begin)));
758 if(opts & UNIT_REQUIRED && unit.empty()) {
759 throw ValidationError(
"Missing mandatory unit");
761 if(opts & CASE_INSENSITIVE) {
762 unit = detail::to_lower(unit);
765 using CLI::detail::lexical_cast;
766 if(!lexical_cast(input, num)) {
767 throw ValidationError(std::string(
"Value ") + input +
" could not be converted to " +
768 detail::type_name<Number>());
775 auto it = mapping.find(unit);
776 if(it == mapping.end()) {
777 throw ValidationError(unit +
778 " unit not recognized. "
780 detail::generate_map(mapping,
true));
784 using CLI::detail::lexical_cast;
785 bool converted = lexical_cast(input, num);
787 throw ValidationError(std::string(
"Value ") + input +
" could not be converted to " +
788 detail::type_name<Number>());
791 bool ok = detail::checked_multiply(num, it->second);
793 throw ValidationError(detail::to_string(num) +
" multiplied by " + unit +
794 " factor would cause number overflow. Use smaller value.");
797 num =
static_cast<Number
>(it->second);
800 input = detail::to_string(num);
809 template <
typename Number>
static void validate_mapping(std::map<std::string, Number> &mapping,
Options opts) {
810 for(
auto &kv : mapping) {
811 if(kv.first.empty()) {
812 throw ValidationError(
"Unit must not be empty.");
814 if(!detail::isalpha(kv.first)) {
815 throw ValidationError(
"Unit must contain only letters.");
820 if(opts & CASE_INSENSITIVE) {
821 std::map<std::string, Number> lower_mapping;
822 for(
auto &kv : mapping) {
823 auto s = detail::to_lower(kv.first);
824 if(lower_mapping.count(s)) {
825 throw ValidationError(std::string(
"Several matching lowercase unit representations are found: ") +
828 lower_mapping[detail::to_lower(kv.first)] = kv.second;
830 mapping = std::move(lower_mapping);
835 template <
typename Number>
static std::string generate_description(
const std::string &
name,
Options opts) {
836 std::stringstream out;
837 out << detail::type_name<Number>() <<
' ';
838 if(opts & UNIT_REQUIRED) {
841 out <<
'[' <<
name <<
']';
864 using result_t = std::uint64_t;
877 static std::map<std::string, result_t> init_mapping(
bool kb_is_1000);
880 static std::map<std::string, result_t> get_mapping(
bool kb_is_1000);
888CLI11_INLINE std::pair<std::string, std::string> split_program_name(std::string commandline);
897#include "impl/Validators_inl.hpp"
Definition Validators.hpp:718
Options
Definition Validators.hpp:724
Definition Validators.hpp:862
Produce a bounded range (factory). Min and max are inclusive.
Definition Validators.hpp:317
Bound(T min_val, T max_val)
Definition Validators.hpp:323
Bound(T max_val)
Range of one value is 0 to value.
Definition Validators.hpp:345
Class wrapping some of the accessors of Validator.
Definition Validators.hpp:179
Definition Validators.hpp:270
Verify items are in a set.
Definition Validators.hpp:489
IsMember(T &&set)
This checks to see if an item is in a set (empty function)
Definition Validators.hpp:499
IsMember(T set, F filter_function)
Definition Validators.hpp:503
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 Validators.hpp:548
IsMember(std::initializer_list< T > values, Args &&...args)
This allows in-place construction using an initializer list.
Definition Validators.hpp:495
Produce a range (factory). Min and max are inclusive.
Definition Validators.hpp:276
Range(T min_val, T max_val, const std::string &validator_name=std::string{})
Definition Validators.hpp:283
Range(T max_val, const std::string &validator_name=std::string{})
Range of one value is 0 to value.
Definition Validators.hpp:306
Validate the input as a particular type.
Definition Validators.hpp:251
Thrown when validation of results fails.
Definition Error.hpp:221
Some validators that are provided.
Definition Validators.hpp:55
Validator operator&(const Validator &other) const
Definition Validators_inl.hpp:45
CLI11_NODISCARD int get_application_index() const
Get the current value of the application index.
Definition Validators.hpp:156
int application_index_
A Validator will only apply to an indexed value (-1 is all elements)
Definition Validators.hpp:66
Validator & non_modifying(bool no_modify=true)
Specify whether the Validator can be modifying or not.
Definition Validators.hpp:140
Validator & description(std::string validator_desc)
Specify the type string.
Definition Validators.hpp:100
std::string operator()(const std::string &str) const
Definition Validators.hpp:94
CLI11_NODISCARD Validator application_index(int app_index) const
Specify the application index of a validator.
Definition Validators.hpp:150
Validator operator|(const Validator &other) const
Definition Validators_inl.hpp:67
CLI11_NODISCARD bool get_active() const
Get a boolean if the validator is active.
Definition Validators.hpp:158
bool active_
Enable for Validator to allow it to be disabled if need be.
Definition Validators.hpp:68
bool non_modifying_
specify that a validator should not modify the input
Definition Validators.hpp:70
Validator(std::string validator_desc)
Construct a Validator with just the description string.
Definition Validators.hpp:78
Validator & name(std::string validator_name)
Specify the type string.
Definition Validators.hpp:115
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
CLI11_NODISCARD std::string get_description() const
Generate type description information for the Validator.
Definition Validators.hpp:108
CLI11_NODISCARD Validator active(bool active_val=true) const
Specify whether the Validator is active or not.
Definition Validators.hpp:133
CLI11_NODISCARD const std::string & get_name() const
Get the name of the Validator.
Definition Validators.hpp:126
Validator & active(bool active_val=true)
Specify whether the Validator is active or not.
Definition Validators.hpp:128
std::string name_
The name for search purposes of the Validator.
Definition Validators.hpp:64
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:161
CLI11_NODISCARD Validator name(std::string validator_name) const
Specify the type string.
Definition Validators.hpp:120
Validator operator!() const
Create a validator that fails when a given validator succeeds.
Definition Validators_inl.hpp:89
std::string operator()(std::string &str) const
Definition Validators_inl.hpp:26
Validator & application_index(int app_index)
Specify the application index of a validator.
Definition Validators.hpp:145
Validator(std::function< std::string(std::string &)> op, std::string validator_desc, std::string validator_name="")
Construct Validator from basic information.
Definition Validators.hpp:80
Validator & operation(std::function< std::string(std::string &)> op)
Set the Validator operation function.
Definition Validators.hpp:84
Check for an existing directory (returns error message if check fails)
Definition Validators.hpp:200
Check for an existing file (returns error message if check fails)
Definition Validators.hpp:194
Check for an existing path.
Definition Validators.hpp:206
Validate the given string is a legal ipv4 address.
Definition Validators.hpp:218
Check for an non-existing path.
Definition Validators.hpp:212
Definition Validators.hpp:395
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