8 #if (defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS == 1) || \
9 (!defined(CLI11_DISABLE_EXTRA_VALIDATORS) || CLI11_DISABLE_EXTRA_VALIDATORS == 0)
49 :
Validator(validator_name, [](
std::string &input_string) {
51 auto val = DesiredType();
53 return std::string(
"Failed parsing ") + input_string +
" as a " + detail::type_name<DesiredType>();
61 const TypeValidator<double>
Number(
"NUMBER");
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) {
80 return std::string(
"Value ") + input +
" could not be converted";
84 else if(val > max_val)
92 template <
typename T>
explicit Bound(T max_val) :
Bound(static_cast<T>(0), max_val) {}
101 template <
typename T,
118 std::string out(1,
'{');
128 template <
typename T> std::string
generate_map(
const T &map,
bool key_only =
false) {
131 std::string out(1,
'{');
134 [key_only](
const iteration_type_t &v) {
148 template <
typename C,
typename V>
struct has_find {
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());
154 using type = std::integral_constant<bool, value>;
158 template <typename T, typename V, enable_if_t<!has_find<T, V>::value, detail::enabler> =
detail::dummy>
159 auto search(
const T &set,
const V &val) -> std::pair<bool, decltype(std::begin(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};
169 template <typename T, typename V, enable_if_t<has_find<T, V>::value, detail::enabler> =
detail::dummy>
170 auto search(
const T &set,
const V &val) -> std::pair<bool, decltype(std::begin(detail::smart_deref(set)))> {
172 auto it = setref.find(val);
173 return {(it != std::end(setref)), it};
177 template <
typename T,
typename V>
178 auto 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)))> {
182 auto res =
search(set, val);
183 if((res.first) || (!(filter_function))) {
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};
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)...) {}
212 template <
typename T,
typename F>
explicit IsMember(T set, F filter_function) {
223 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
230 func_ = [set, filter_fn](std::string &input) {
247 return std::string{};
256 template <
typename T,
typename... Args>
259 std::forward<T>(set),
260 [filter_fn_1, filter_fn_2](
std::string a) {
return filter_fn_2(filter_fn_1(a)); },
265 template <
typename T>
using TransformPairs = std::vector<std::pair<std::string, T>>;
273 template <
typename... Args>
274 Transformer(std::initializer_list<std::pair<std::string, std::string>> values, Args &&...args)
282 template <
typename T,
typename F>
explicit Transformer(T mapping, F filter_function) {
285 "mapping must produce value pairs");
294 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
299 func_ = [mapping, filter_fn](std::string &input) {
303 return std::string();
313 return std::string{};
318 template <
typename T,
typename... Args>
321 std::forward<T>(mapping),
322 [filter_fn_1, filter_fn_2](
std::string a) {
return filter_fn_2(filter_fn_1(a)); },
332 template <
typename... Args>
333 CheckedTransformer(std::initializer_list<std::pair<std::string, std::string>> values, Args &&...args)
344 "mapping must produce value pairs");
354 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
356 auto tfunc = [mapping]() {
357 std::string out(
"value in ");
361 [](
const iteration_type_t &v) {
369 desc_function_ = tfunc;
371 func_ = [mapping, tfunc, filter_fn](std::string &input) {
382 return std::string{};
387 if(output_string == input) {
388 return std::string();
392 return "Check " + input +
" " + tfunc() +
" FAILED";
397 template <
typename T,
typename... Args>
400 std::forward<T>(mapping),
401 [filter_fn_1, filter_fn_2](
std::string a) {
return filter_fn_2(filter_fn_1(a)); },
413 item.erase(std::remove(std::begin(item), std::end(item),
' '), std::end(item));
414 item.erase(std::remove(std::begin(item), std::end(item),
'\t'), std::end(item));
437 CASE_INSENSITIVE = 1,
440 DEFAULT = CASE_INSENSITIVE | UNIT_OPTIONAL
443 template <
typename Number>
446 const std::string &unit_name =
"UNIT") {
447 description(generate_description<Number>(unit_name, opts));
448 validate_mapping(mapping, opts);
451 func_ = [mapping, opts](std::string &input) -> std::string {
460 auto unit_begin = input.end();
461 while(unit_begin > input.begin() &&
std::isalpha(*(unit_begin - 1), std::locale())) {
465 std::string unit{unit_begin, input.end()};
466 input.resize(static_cast<std::size_t>(std::distance(input.begin(), unit_begin)));
469 if(opts & UNIT_REQUIRED && unit.empty()) {
472 if(opts & CASE_INSENSITIVE) {
478 throw ValidationError(std::string(
"Value ") + input +
" could not be converted to " +
479 detail::type_name<Number>());
486 auto it = mapping.find(unit);
487 if(it == mapping.end()) {
489 " unit not recognized. "
498 throw ValidationError(std::string(
"Value ") + input +
" could not be converted to " +
499 detail::type_name<Number>());
505 " factor would cause number overflow. Use smaller value.");
508 num =
static_cast<Number>(it->second);
520 template <
typename Number>
static void validate_mapping(std::map<std::string, Number> &mapping, Options opts) {
521 for(
auto &kv : mapping) {
522 if(kv.first.empty()) {
526 throw ValidationError(
"Unit must contain only letters.");
531 if(opts & CASE_INSENSITIVE) {
532 std::map<std::string, Number> lower_mapping;
533 for(
auto &kv : mapping) {
535 if(lower_mapping.count(s)) {
536 throw ValidationError(std::string(
"Several matching lowercase unit representations are found: ") +
541 mapping = std::move(lower_mapping);
546 template <
typename Number>
static std::string generate_description(
const std::string &name, Options opts) {
547 std::stringstream out;
548 out << detail::type_name<Number>() <<
' ';
549 if(opts & UNIT_REQUIRED) {
552 out <<
'[' << name <<
']';
588 static std::map<std::string, result_t> init_mapping(
bool kb_is_1000);
591 static std::map<std::string, result_t> get_mapping(
bool kb_is_1000);
594 #if defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS != 0
596 #if CLI11_HAS_FILESYSTEM
598 enum class Permission : std::uint8_t { none = 0, read = 1, write = 2, exec = 4 };
599 class PermissionValidator :
public Validator {
601 explicit PermissionValidator(Permission permission);
606 const detail::PermissionValidator ReadPermissions(detail::Permission::read);
609 const detail::PermissionValidator WritePermissions(detail::Permission::write);
612 const detail::PermissionValidator ExecPermissions(detail::Permission::exec);
619 #ifndef CLI11_COMPILE
620 #include "impl/ExtraValidators_inl.hpp"
std::uint64_t result_t
Definition: ExtraValidators.hpp:575
Definition: ExtraValidators.hpp:429
TypeValidator(const std::string &validator_name)
Definition: ExtraValidators.hpp:48
Validate the input as a particular type.
Definition: ExtraValidators.hpp:46
Adaptor for set-like structure: This just wraps a normal container in a few utilities that do almost ...
Definition: TypeTools.hpp:130
const TypeValidator< double > Number("NUMBER")
Check for a number.
std::string ignore_space(std::string item)
Helper function to allow checks to ignore spaces to be passed to IsMember or Transform.
Definition: ExtraValidators.hpp:412
Produce a bounded range (factory). Min and max are inclusive.
Definition: ExtraValidators.hpp:64
std::string generate_set(const T &set)
Generate a string representation of a set.
Definition: ExtraValidators.hpp:115
TypeValidator()
Definition: ExtraValidators.hpp:57
enabler
Simple empty scoped class.
Definition: TypeTools.hpp:36
std::function< std::string(std::string)> filter_fn_t
Definition: ExtraValidators.hpp:200
AsNumberWithUnit(std::map< std::string, Number > mapping, Options opts=DEFAULT, const std::string &unit_name="UNIT")
Definition: ExtraValidators.hpp:444
std::string ignore_case(std::string item)
Helper function to allow ignore_case to be passed to IsMember or Transform.
Definition: ExtraValidators.hpp:406
std::function< std::string(std::string &)> func_
Definition: Validators.hpp:61
auto smart_deref(T value) -> decltype(*value)
Definition: ExtraValidators.hpp:103
typename std::remove_const< value_type >::type first_type
Definition: TypeTools.hpp:132
Validate the given string is a legal ipv4 address.
Definition: ExtraValidators.hpp:38
bool lexical_cast(const std::string &input, T &output)
Integer conversion.
Definition: TypeTools.hpp:1123
Definition: ExtraValidators.hpp:573
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition: TypeTools.hpp:337
std::integral_constant< bool, value > type
Definition: ExtraValidators.hpp:154
static const auto value
Definition: ExtraValidators.hpp:153
Definition: ExtraValidators.hpp:148
IsMember(T set, F filter_function)
Definition: ExtraValidators.hpp:212
Bound(T max_val)
Range of one value is 0 to value.
Definition: ExtraValidators.hpp:92
std::string ignore_underscore(std::string item)
Helper function to allow ignore_underscore to be passed to IsMember or Transform. ...
Definition: ExtraValidators.hpp:409
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:54
CLI11_MODULE_INLINE const detail::IPV4Validator ValidIPV4
Check for an IP4 address.
Definition: ExtraValidators.hpp:98
static auto test(int) -> decltype(std::declval< CC >().find(std::declval< VV >()), std::true_type())
Bound(T min_val, T max_val)
Definition: ExtraValidators.hpp:70
auto search(const T &set, const V &val) -> std::pair< bool, decltype(std::begin(detail::smart_deref(set)))>
A search function.
Definition: ExtraValidators.hpp:159
Thrown when validation of results fails.
Definition: Error.hpp:221
std::string & trim(std::string &str)
Trim whitespace from string.
Definition: StringTools.hpp:117
CLI11_INLINE std::string & rtrim(std::string &str)
Trim whitespace from right of string.
std::string remove_underscore(std::string str)
remove underscores from a string
Definition: StringTools.hpp:190
#define CLI11_MODULE_INLINE
Definition: Macros.hpp:183
typename std::enable_if< B, T >::type enable_if_t
Definition: TypeTools.hpp:47
IsMember(T &&set)
This checks to see if an item is in a set (empty function)
Definition: ExtraValidators.hpp:208
IsMember(std::initializer_list< T > values, Args &&...args)
This allows in-place construction using an initializer list.
Definition: ExtraValidators.hpp:204
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
Options
Definition: ExtraValidators.hpp:435
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition: StringTools.hpp:182
Verify items are in a set.
Definition: ExtraValidators.hpp:198
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
constexpr std::enable_if< I< type_count_base< T >::value, int >::type tuple_type_size(){return subtype_count< typename std::tuple_element< I, T >::type >::value+tuple_type_size< T, I+1 >);}template< typename T >struct type_count< T, typename std::enable_if< is_tuple_like< T >::value &&!is_complex< T >::value >::type >{static constexpr int value{tuple_type_size< T, 0 >)};};template< typename T > struct subtype_count{static constexpr int value{is_mutable_container< T >::value?expected_max_vector_size:type_count< T >::value};};template< typename T, typename Enable=void > struct type_count_min{static const int value{0};};template< typename T >struct type_count_min< T, typename std::enable_if<!is_mutable_container< T >::value &&!is_tuple_like< T >::value &&!is_wrapper< T >::value &&!is_complex< T >::value &&!std::is_void< T >::value >::type >{static constexpr int value{type_count< T >::value};};template< typename T > struct type_count_min< T, typename std::enable_if< is_complex< T >::value >::type >{static constexpr int value{1};};template< typename T >struct type_count_min< T, typename std::enable_if< is_wrapper< T >::value &&!is_complex< T >::value &&!is_tuple_like< T >::value >::type >{static constexpr int value{subtype_count_min< typename T::value_type >::value};};template< typename T, std::size_t I >constexpr typename std::enable_if< I==type_count_base< T >::value, int >::type tuple_type_size_min(){return 0;}template< typename T, std::size_t I > constexpr typename std::enable_if< I< type_count_base< T >::value, int >::type tuple_type_size_min(){return subtype_count_min< typename std::tuple_element< I, T >::type >::value+tuple_type_size_min< T, I+1 >);}template< typename T >struct type_count_min< T, typename std::enable_if< is_tuple_like< T >::value &&!is_complex< T >::value >::type >{static constexpr int value{tuple_type_size_min< T, 0 >)};};template< typename T > struct subtype_count_min{static constexpr int value{is_mutable_container< T >::value?((type_count< T >::value< expected_max_vector_size)?type_count< T >::value:0):type_count_min< T >::value};};template< typename T, typename Enable=void > struct expected_count{static const int value{0};};template< typename T >struct expected_count< T, typename std::enable_if<!is_mutable_container< T >::value &&!is_wrapper< T >::value &&!std::is_void< T >::value >::type >{static constexpr int value{1};};template< typename T > struct expected_count< T, typename std::enable_if< is_mutable_container< T >::value >::type >{static constexpr int value{expected_max_vector_size};};template< typename T >struct expected_count< T, typename std::enable_if<!is_mutable_container< T >::value &&is_wrapper< T >::value >::type >{static constexpr int value{expected_count< typename T::value_type >::value};};enum class object_category:std::uint8_t{char_value=1, integral_value=2, unsigned_integral=4, enumeration=6, boolean_value=8, floating_point=10, number_constructible=12, double_constructible=14, integer_constructible=16, string_assignable=23, string_constructible=24, wstring_assignable=25, wstring_constructible=26, other=45, wrapper_value=50, complex_number=60, tuple_value=70, container_value=80,};template< typename T, typename Enable=void > struct classify_object{static constexpr object_category value{object_category::other};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, char >::value &&std::is_signed< T >::value &&!is_bool< T >::value &&!std::is_enum< T >::value >::type >{static constexpr object_category value{object_category::integral_value};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&!std::is_same< T, char >::value &&!is_bool< T >::value >::type >{static constexpr object_category value{object_category::unsigned_integral};};template< typename T >struct classify_object< T, typename std::enable_if< std::is_same< T, char >::value &&!std::is_enum< T >::value >::type >{static constexpr object_category value{object_category::char_value};};template< typename T > struct classify_object< T, typename std::enable_if< is_bool< T >::value >::type >{static constexpr object_category value{object_category::boolean_value};};template< typename T > struct classify_object< T, typename std::enable_if< std::is_floating_point< T >::value >::type >{static constexpr object_category value{object_category::floating_point};};#define WIDE_STRING_CHECK#define STRING_CHECKtemplate< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&WIDE_STRING_CHECK &&std::is_assignable< T &, std::string >::value >::type >{static constexpr object_category value{object_category::string_assignable};};template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&!std::is_assignable< T &, std::string >::value &&(type_count< T >::value==1)&&WIDE_STRING_CHECK &&std::is_constructible< T, std::string >::value >::type >{static constexpr object_category value{object_category::string_constructible};};template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&STRING_CHECK &&std::is_assignable< T &, std::wstring >::value >::type >{static constexpr object_category value{object_category::wstring_assignable};};template< typename T >struct classify_object< T, typename std::enable_if<!std::is_floating_point< T >::value &&!std::is_integral< T >::value &&!std::is_assignable< T &, std::wstring >::value &&(type_count< T >::value==1)&&STRING_CHECK &&std::is_constructible< T, std::wstring >::value >::type >{static constexpr object_category value{object_category::wstring_constructible};};template< typename T > struct classify_object< T, typename std::enable_if< std::is_enum< T >::value >::type >{static constexpr object_category value{object_category::enumeration};};template< typename T > struct classify_object< T, typename std::enable_if< is_complex< T >::value >::type >{static constexpr object_category value{object_category::complex_number};};template< typename T > struct uncommon_type{using type=typename std::conditional< !std::is_floating_point< T >::value &&!std::is_integral< T >::value &&!std::is_assignable< T &, std::string >::value &&!std::is_constructible< T, std::string >::value &&!std::is_assignable< T &, std::wstring >::value &&!std::is_constructible< T, std::wstring >::value &&!is_complex< T >::value &&!is_mutable_container< T >::value &&!std::is_enum< T >::value, std::true_type, std::false_type >::type;static constexpr bool value=type::value;};template< typename T >struct classify_object< T, typename std::enable_if<(!is_mutable_container< T >::value &&is_wrapper< T >::value &&!is_tuple_like< T >::value &&uncommon_type< T >::value)>::type >{static constexpr object_category value{object_category::wrapper_value};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&is_direct_constructible< T, double >::value &&is_direct_constructible< T, int >::value >::type >{static constexpr object_category value{object_category::number_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&!is_direct_constructible< T, double >::value &&is_direct_constructible< T, int >::value >::type >{static constexpr object_category value{object_category::integer_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< uncommon_type< T >::value &&type_count< T >::value==1 &&!is_wrapper< T >::value &&is_direct_constructible< T, double >::value &&!is_direct_constructible< T, int >::value >::type >{static constexpr object_category value{object_category::double_constructible};};template< typename T >struct classify_object< T, typename std::enable_if< is_tuple_like< T >::value &&((type_count< T >::value >=2 &&!is_wrapper< T >::value)||(uncommon_type< T >::value &&!is_direct_constructible< T, double >::value &&!is_direct_constructible< T, int >::value)||(uncommon_type< T >::value &&type_count< T >::value >=2))>::type >{static constexpr object_category value{object_category::tuple_value};};template< typename T > struct classify_object< T, typename std::enable_if< is_mutable_container< T >::value >::type >{static constexpr object_category value{object_category::container_value};};template< typename T, enable_if_t< classify_object< T >::value==object_category::char_value, detail::enabler >=detail::dummy >constexpr const char *type_name(){return"CHAR";}template< typename T, enable_if_t< classify_object< T >::value==object_category::integral_value||classify_object< T >::value==object_category::integer_constructible, detail::enabler >=detail::dummy >constexpr const char *type_name(){return"INT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::unsigned_integral, detail::enabler >=detail::dummy >constexpr const char *type_name(){return"UINT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::floating_point||classify_object< T >::value==object_category::number_constructible||classify_object< T >::value==object_category::double_constructible, detail::enabler >=detail::dummy >constexpr const char *type_name(){return"FLOAT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::enumeration, detail::enabler >=detail::dummy >constexpr const char *type_name(){return"ENUM";}template< typename T, enable_if_t< classify_object< T >::value==object_category::boolean_value, detail::enabler >=detail::dummy >constexpr const char *type_name(){return"BOOLEAN";}template< typename T, enable_if_t< classify_object< T >::value==object_category::complex_number, detail::enabler >=detail::dummy >constexpr const char *type_name(){return"COMPLEX";}template< typename T, enable_if_t< classify_object< T >::value >=object_category::string_assignable &&classify_object< T >::value<=object_category::other, detail::enabler >=detail::dummy >constexpr const char *type_name(){return"TEXT";}template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value >=2, detail::enabler >=detail::dummy >std::string type_name();template< typename T, enable_if_t< classify_object< T >::value==object_category::container_value||classify_object< T >::value==object_category::wrapper_value, detail::enabler >=detail::dummy >std::string type_name();template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value==1, detail::enabler >=detail::dummy >inline std::string type_name(){return type_name< typename std::decay< typename std::tuple_element< 0, T >::type >::type >);}template< typename T, std::size_t I >inline typename std::enable_if< I==type_count_base< T >::value, std::string >::type tuple_name(){return std::string{};}template< typename T, std::size_t I >inline typename std::enable_if<(I< type_count_base< T >::value), std::string >::type tuple_name(){auto str=std::string{type_name< typename std::decay< typename std::tuple_element< I, T >::type >::type >)}+ ','+tuple_name< T, I+1 >);if(str.back()== ',') str.pop_back();return str;}template< typename T, enable_if_t< classify_object< T >::value==object_category::tuple_value &&type_count_base< T >::value >=2, detail::enabler > > std::string type_name()
Recursively generate the tuple type name.
Definition: TypeTools.hpp:927
Some validators that are provided.
Definition: Validators.hpp:54
std::vector< std::pair< std::string, T >> TransformPairs
definition of the default transformation object
Definition: ExtraValidators.hpp:265
std::string value_string(const T &value)
get a string as a convertible value for arithmetic types
Definition: TypeTools.hpp:470
T type
Definition: TypeTools.hpp:82
CLI11_MODULE_INLINE constexpr enabler dummy
An instance to use in EnableIf.
Definition: TypeTools.hpp:39
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
typename T::value_type value_type
Definition: TypeTools.hpp:131
std::string generate_map(const T &map, bool key_only=false)
Generate a string representation of a map.
Definition: ExtraValidators.hpp:128
AsNumberWithUnit::Options operator|(const AsNumberWithUnit::Options &a, const AsNumberWithUnit::Options &b)
Definition: ExtraValidators.hpp:558
bool isalpha(const std::string &str)
Verify that str consists of letters only.
Definition: StringTools.hpp:177
T type
Definition: TypeTools.hpp:116
Validator & description(std::string validator_desc)
Specify the type string.
Definition: Validators.hpp:99