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) {
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";
84 else if(val > max_val)
92 template <
typename T>
explicit Bound(T max_val) :
Bound(static_cast<T>(0), max_val) {}
118 std::string out(1,
'{');
128template <
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) {
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>;
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};
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)
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)...) {}
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) {
223 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
230 func_ = [set, filter_fn](std::string &input) {
231 using CLI::detail::lexical_cast;
233 if(!lexical_cast(input, b)) {
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)); },
265template <
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) {
300 using CLI::detail::lexical_cast;
302 if(!lexical_cast(input, b)) {
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) {
371 func_ = [mapping, tfunc, filter_fn](std::string &input) {
372 using CLI::detail::lexical_cast;
374 bool converted = lexical_cast(input, b);
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));
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)));
476 using CLI::detail::lexical_cast;
477 if(!lexical_cast(input, num)) {
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. "
495 using CLI::detail::lexical_cast;
496 bool converted = lexical_cast(input, num);
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()) {
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>() <<
' ';
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
598enum class Permission : std::uint8_t { none = 0, read = 1, write = 2, exec = 4 };
599class PermissionValidator :
public Validator {
601 explicit PermissionValidator(Permission permission);
606const detail::PermissionValidator ReadPermissions(detail::Permission::read);
609const detail::PermissionValidator WritePermissions(detail::Permission::write);
612const detail::PermissionValidator ExecPermissions(detail::Permission::exec);
620#include "impl/ExtraValidators_inl.hpp"
#define CLI11_MODULE_INLINE
Definition Macros.hpp:183
Options
Definition ExtraValidators.hpp:435
@ UNIT_OPTIONAL
Definition ExtraValidators.hpp:438
@ CASE_INSENSITIVE
Definition ExtraValidators.hpp:437
@ DEFAULT
Definition ExtraValidators.hpp:440
@ UNIT_REQUIRED
Definition ExtraValidators.hpp:439
@ CASE_SENSITIVE
Definition ExtraValidators.hpp:436
AsNumberWithUnit(std::map< std::string, Number > mapping, Options opts=DEFAULT, const std::string &unit_name="UNIT")
Definition ExtraValidators.hpp:444
AsSizeValue(bool kb_is_1000)
std::uint64_t result_t
Definition ExtraValidators.hpp:575
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
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
std::function< std::string(std::string)> filter_fn_t
Definition ExtraValidators.hpp:200
Validate the input as a particular type.
Definition ExtraValidators.hpp:46
TypeValidator()
Definition ExtraValidators.hpp:57
TypeValidator(const std::string &validator_name)
Definition ExtraValidators.hpp:48
Validator & description(std::string validator_desc)
Specify the type string.
Definition Validators.hpp:99
Validator(std::string validator_desc, std::function< std::string(std::string &)> func)
Definition Validators.hpp:71
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
auto smart_deref(T value) -> decltype(*value)
Definition ExtraValidators.hpp:103
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::string generate_map(const T &map, bool key_only=false)
Generate a string representation of a map.
Definition ExtraValidators.hpp:128
std::string remove_underscore(std::string str)
remove underscores from a string
Definition StringTools.hpp:190
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
std::string & trim(std::string &str)
Trim whitespace from string.
Definition StringTools.hpp:117
std::string generate_set(const T &set)
Generate a string representation of a set.
Definition ExtraValidators.hpp:115
CLI11_MODULE_INLINE constexpr enabler dummy
An instance to use in EnableIf.
Definition TypeTools.hpp:39
std::string value_string(const T &value)
get a string as a convertible value for arithmetic types
Definition TypeTools.hpp:470
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
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition StringTools.hpp:54
enabler
Simple empty scoped class.
Definition TypeTools.hpp:36
bool isalpha(const std::string &str)
Verify that str consists of letters only.
Definition StringTools.hpp:177
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition StringTools.hpp:182
CLI11_INLINE std::string & rtrim(std::string &str)
Trim whitespace from right of string.
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::string ignore_underscore(std::string item)
Helper function to allow ignore_underscore to be passed to IsMember or Transform.
Definition ExtraValidators.hpp:409
typename std::enable_if< B, T >::type enable_if_t
Definition TypeTools.hpp:47
AsNumberWithUnit::Options operator|(const AsNumberWithUnit::Options &a, const AsNumberWithUnit::Options &b)
Definition ExtraValidators.hpp:558
CLI11_MODULE_INLINE const detail::IPV4Validator ValidIPV4
Check for an IP4 address.
Definition ExtraValidators.hpp:98
@ ValidationError
Definition Error.hpp:51
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
const TypeValidator< double > Number("NUMBER")
Check for a number.
std::vector< std::pair< std::string, T > > TransformPairs
definition of the default transformation object
Definition ExtraValidators.hpp:265
T type
Definition TypeTools.hpp:82
T type
Definition TypeTools.hpp:116
Definition ExtraValidators.hpp:148
static const auto value
Definition ExtraValidators.hpp:153
std::integral_constant< bool, value > type
Definition ExtraValidators.hpp:154
static auto test(int) -> decltype(std::declval< CC >().find(std::declval< VV >()), std::true_type())
static auto test(...) -> decltype(std::false_type())
Adaptor for set-like structure: This just wraps a normal container in a few utilities that do almost ...
Definition TypeTools.hpp:130
typename T::value_type value_type
Definition TypeTools.hpp:131
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
typename std::remove_const< value_type >::type first_type
Definition TypeTools.hpp:132
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