31#if defined CLI11_CPP17 && defined __has_include && !defined CLI11_HAS_FILESYSTEM
32#if __has_include(<filesystem>)
34#if defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
35#define CLI11_HAS_FILESYSTEM 0
36#elif defined(__wasi__)
38#define CLI11_HAS_FILESYSTEM 0
41#if defined __cpp_lib_filesystem && __cpp_lib_filesystem >= 201703
42#if defined _GLIBCXX_RELEASE && _GLIBCXX_RELEASE >= 9
43#define CLI11_HAS_FILESYSTEM 1
44#elif defined(__GLIBCXX__)
46#define CLI11_HAS_FILESYSTEM 0
48#define CLI11_HAS_FILESYSTEM 1
51#define CLI11_HAS_FILESYSTEM 0
57#if defined CLI11_HAS_FILESYSTEM && CLI11_HAS_FILESYSTEM > 0
88 std::function<std::string(std::string &)>
func_{[](std::string &) {
return std::string{}; }};
98 Validator(std::string validator_desc, std::function<std::string(std::string &)> func)
106 Validator(std::function<std::string(std::string &)> op, std::string validator_desc, std::string validator_name =
"")
108 name_(std::move(validator_name)) {}
111 func_ = std::move(op);
121 std::string value = str;
138 return std::string{};
142 name_ = std::move(validator_name);
148 newval.
name_ = std::move(validator_name);
201 void _merge_description(
const Validator &val1,
const Validator &val2,
const std::string &merger);
272 :
Validator(validator_name, [](std::string &input_string) {
273 auto val = DesiredType();
275 return std::string(
"Failed parsing ") + input_string +
" as a " + detail::type_name<DesiredType>();
277 return std::string();
299 template <
typename T>
300 Range(T min_val, T max_val,
const std::string &validator_name = std::string{}) :
Validator(validator_name) {
301 if(validator_name.empty()) {
302 std::stringstream out;
303 out << detail::type_name<T>() <<
" in [" << min_val <<
" - " << max_val <<
"]";
307 func_ = [min_val, max_val](std::string &input) {
310 if((!converted) || (val < min_val || val > max_val)) {
311 std::stringstream out;
312 out <<
"Value " << input <<
" not in range [";
313 out << min_val <<
" - " << max_val <<
"]";
316 return std::string{};
321 template <
typename T>
322 explicit Range(T max_val,
const std::string &validator_name = std::string{})
323 :
Range(
static_cast<T
>(0), max_val, validator_name) {}
330const Range PositiveNumber((std::numeric_limits<double>::min)(), (std::numeric_limits<double>::max)(),
"POSITIVE");
339 template <
typename T>
Bound(T min_val, T max_val) {
340 std::stringstream out;
341 out << detail::type_name<T>() <<
" bounded to [" << min_val <<
" - " << max_val <<
"]";
344 func_ = [min_val, max_val](std::string &input) {
348 return std::string(
"Value ") + input +
" could not be converted";
352 else if(val > max_val)
355 return std::string{};
360 template <
typename T>
explicit Bound(T max_val) :
Bound(static_cast<T>(0), max_val) {}
380 std::string out(1,
'{');
390template <
typename T> std::string
generate_map(
const T &map,
bool key_only =
false) {
393 std::string out(1,
'{');
396 [key_only](
const iteration_type_t &v) {
411 template <
typename CC,
typename VV>
412 static auto test(
int) ->
decltype(std::declval<CC>().find(std::declval<VV>()), std::true_type());
413 template <
typename,
typename>
static auto test(...) ->
decltype(std::false_type());
416 using type = std::integral_constant<bool, value>;
424 auto it = std::find_if(std::begin(setref), std::end(setref), [&val](
decltype(*std::begin(setref)) v) {
427 return {(it != std::end(setref)), it};
434 auto it = setref.find(val);
435 return {(it != std::end(setref)), it};
439template <
typename T,
typename V>
440auto search(
const T &set,
const V &val,
const std::function<V(V)> &filter_function)
444 auto res =
search(set, val);
445 if((res.first) || (!(filter_function))) {
450 auto it = std::find_if(std::begin(setref), std::end(setref), [&](
decltype(*std::begin(setref)) v) {
452 a = filter_function(a);
455 return {(it != std::end(setref)), it};
463inline typename std::enable_if<std::is_signed<T>::value, T>::type
overflowCheck(
const T &a,
const T &b) {
464 if((a > 0) == (b > 0)) {
465 return ((std::numeric_limits<T>::max)() / (std::abs)(a) < (std::abs)(b));
467 return ((std::numeric_limits<T>::min)() / (std::abs)(a) > -(std::abs)(b));
471inline typename std::enable_if<!std::is_signed<T>::value, T>::type
overflowCheck(
const T &a,
const T &b) {
472 return ((std::numeric_limits<T>::max)() / a < b);
476template <
typename T>
typename std::enable_if<std::is_integral<T>::value,
bool>::type
checked_multiply(T &a, T b) {
477 if(a == 0 || b == 0 || a == 1 || b == 1) {
481 if(a == (std::numeric_limits<T>::min)() || b == (std::numeric_limits<T>::min)()) {
493typename std::enable_if<std::is_floating_point<T>::value,
bool>::type
checked_multiply(T &a, T b) {
495 if(std::isinf(c) && !std::isinf(a) && !std::isinf(b)) {
509 template <
typename T,
typename... Args>
510 IsMember(std::initializer_list<T> values, Args &&...args)
511 :
IsMember(std::vector<T>(values), std::forward<Args>(args)...) {}
514 template <
typename T>
explicit IsMember(T &&set) :
IsMember(std::forward<T>(set), nullptr) {}
518 template <
typename T,
typename F>
explicit IsMember(T set, F filter_function) {
529 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
536 func_ = [set, filter_fn](std::string &input) {
552 return std::string{};
561 template <
typename T,
typename... Args>
564 std::forward<T>(set),
565 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
570template <
typename T>
using TransformPairs = std::vector<std::pair<std::string, T>>;
578 template <
typename... Args>
579 Transformer(std::initializer_list<std::pair<std::string, std::string>> values, Args &&...args)
587 template <
typename T,
typename F>
explicit Transformer(T mapping, F filter_function) {
590 "mapping must produce value pairs");
599 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
604 func_ = [mapping, filter_fn](std::string &input) {
607 return std::string();
617 return std::string{};
622 template <
typename T,
typename... Args>
625 std::forward<T>(mapping),
626 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
636 template <
typename... Args>
637 CheckedTransformer(std::initializer_list<std::pair<std::string, std::string>> values, Args &&...args)
648 "mapping must produce value pairs");
658 std::function<local_item_t(local_item_t)> filter_fn = filter_function;
660 auto tfunc = [mapping]() {
661 std::string out(
"value in ");
673 func_ = [mapping, tfunc, filter_fn](std::string &input) {
683 return std::string{};
688 if(output_string == input) {
689 return std::string();
693 return "Check " + input +
" " + tfunc() +
" FAILED";
698 template <
typename T,
typename... Args>
701 std::forward<T>(mapping),
702 [filter_fn_1, filter_fn_2](std::string a) {
return filter_fn_2(filter_fn_1(a)); },
714 item.erase(std::remove(std::begin(item), std::end(item),
' '), std::end(item));
715 item.erase(std::remove(std::begin(item), std::end(item),
'\t'), std::end(item));
744 template <
typename Number>
747 const std::string &unit_name =
"UNIT") {
748 description(generate_description<Number>(unit_name, opts));
749 validate_mapping(mapping, opts);
752 func_ = [mapping, opts](std::string &input) -> std::string {
761 auto unit_begin = input.end();
762 while(unit_begin > input.begin() && std::isalpha(*(unit_begin - 1), std::locale())) {
766 std::string unit{unit_begin, input.end()};
767 input.resize(
static_cast<std::size_t
>(std::distance(input.begin(), unit_begin)));
778 throw ValidationError(std::string(
"Value ") + input +
" could not be converted to " +
779 detail::type_name<Number>());
786 auto it = mapping.find(unit);
787 if(it == mapping.end()) {
789 " unit not recognized. "
797 throw ValidationError(std::string(
"Value ") + input +
" could not be converted to " +
798 detail::type_name<Number>());
804 " factor would cause number overflow. Use smaller value.");
807 num =
static_cast<Number>(it->second);
819 template <
typename Number>
static void validate_mapping(std::map<std::string, Number> &mapping,
Options opts) {
820 for(
auto &kv : mapping) {
821 if(kv.first.empty()) {
825 throw ValidationError(
"Unit must contain only letters.");
831 std::map<std::string, Number> lower_mapping;
832 for(
auto &kv : mapping) {
834 if(lower_mapping.count(s)) {
835 throw ValidationError(std::string(
"Several matching lowercase unit representations are found: ") +
840 mapping = std::move(lower_mapping);
845 template <
typename Number>
static std::string generate_description(
const std::string &
name,
Options opts) {
846 std::stringstream out;
847 out << detail::type_name<Number>() <<
' ';
851 out <<
'[' <<
name <<
']';
887 static std::map<std::string, result_t> init_mapping(
bool kb_is_1000);
890 static std::map<std::string, result_t> get_mapping(
bool kb_is_1000);
907#include "impl/Validators_inl.hpp"
#define CLI11_INLINE
Definition: Macros.hpp:73
#define CLI11_NODISCARD
Definition: Macros.hpp:47
Definition: Validators.hpp:730
Options
Definition: Validators.hpp:736
@ UNIT_OPTIONAL
Definition: Validators.hpp:739
@ CASE_INSENSITIVE
Definition: Validators.hpp:738
@ DEFAULT
Definition: Validators.hpp:741
@ UNIT_REQUIRED
Definition: Validators.hpp:740
@ CASE_SENSITIVE
Definition: Validators.hpp:737
AsNumberWithUnit(std::map< std::string, Number > mapping, Options opts=DEFAULT, const std::string &unit_name="UNIT")
Definition: Validators.hpp:745
Definition: Validators.hpp:872
AsSizeValue(bool kb_is_1000)
std::uint64_t result_t
Definition: Validators.hpp:874
Produce a bounded range (factory). Min and max are inclusive.
Definition: Validators.hpp:333
Bound(T min_val, T max_val)
Definition: Validators.hpp:339
Bound(T max_val)
Range of one value is 0 to value.
Definition: Validators.hpp:360
Class wrapping some of the accessors of Validator.
Definition: Validators.hpp:205
Definition: Validators.hpp:287
FileOnDefaultPath(std::string default_path, bool enableErrorReturn=true)
Verify items are in a set.
Definition: Validators.hpp:504
IsMember(T &&set)
This checks to see if an item is in a set (empty function)
Definition: Validators.hpp:514
IsMember(T set, F filter_function)
Definition: Validators.hpp:518
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:562
IsMember(std::initializer_list< T > values, Args &&...args)
This allows in-place construction using an initializer list.
Definition: Validators.hpp:510
std::function< std::string(std::string)> filter_fn_t
Definition: Validators.hpp:506
Produce a range (factory). Min and max are inclusive.
Definition: Validators.hpp:293
Range(T min_val, T max_val, const std::string &validator_name=std::string{})
Definition: Validators.hpp:300
Range(T max_val, const std::string &validator_name=std::string{})
Range of one value is 0 to value.
Definition: Validators.hpp:322
Validate the input as a particular type.
Definition: Validators.hpp:269
TypeValidator()
Definition: Validators.hpp:279
TypeValidator(const std::string &validator_name)
Definition: Validators.hpp:271
Thrown when validation of results fails.
Definition: Error.hpp:213
Some validators that are provided.
Definition: Validators.hpp:81
CLI11_NODISCARD int get_application_index() const
Get the current value of the application index.
Definition: Validators.hpp:182
int application_index_
A Validator will only apply to an indexed value (-1 is all elements)
Definition: Validators.hpp:92
Validator & non_modifying(bool no_modify=true)
Specify whether the Validator can be modifying or not.
Definition: Validators.hpp:166
Validator & description(std::string validator_desc)
Specify the type string.
Definition: Validators.hpp:126
std::string operator()(const std::string &str) const
Definition: Validators.hpp:120
CLI11_NODISCARD Validator application_index(int app_index) const
Specify the application index of a validator.
Definition: Validators.hpp:176
CLI11_NODISCARD bool get_active() const
Get a boolean if the validator is active.
Definition: Validators.hpp:184
Validator operator&(const Validator &other) const
bool active_
Enable for Validator to allow it to be disabled if need be.
Definition: Validators.hpp:94
bool non_modifying_
specify that a validator should not modify the input
Definition: Validators.hpp:96
Validator(std::string validator_desc)
Construct a Validator with just the description string.
Definition: Validators.hpp:104
Validator(std::string validator_desc, std::function< std::string(std::string &)> func)
Definition: Validators.hpp:98
CLI11_NODISCARD Validator description(std::string validator_desc) const
Specify the type string.
Validator & name(std::string validator_name)
Specify the type string.
Definition: Validators.hpp:141
std::string operator()(std::string &str) const
std::function< std::string()> desc_function_
This is the description function, if empty the description_ will be used.
Definition: Validators.hpp:84
std::function< std::string(std::string &)> func_
Definition: Validators.hpp:88
CLI11_NODISCARD std::string get_description() const
Generate type description information for the Validator.
Definition: Validators.hpp:134
Validator operator!() const
Create a validator that fails when a given validator succeeds.
CLI11_NODISCARD Validator active(bool active_val=true) const
Specify whether the Validator is active or not.
Definition: Validators.hpp:159
CLI11_NODISCARD const std::string & get_name() const
Get the name of the Validator.
Definition: Validators.hpp:152
Validator operator|(const Validator &other) const
Validator & active(bool active_val=true)
Specify whether the Validator is active or not.
Definition: Validators.hpp:154
std::string name_
The name for search purposes of the Validator.
Definition: Validators.hpp:90
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:187
CLI11_NODISCARD Validator name(std::string validator_name) const
Specify the type string.
Definition: Validators.hpp:146
Validator & application_index(int app_index)
Specify the application index of a validator.
Definition: Validators.hpp:171
Validator(std::function< std::string(std::string &)> op, std::string validator_desc, std::string validator_name="")
Construct Validator from basic information.
Definition: Validators.hpp:106
Validator & operation(std::function< std::string(std::string &)> op)
Set the Validator operation function.
Definition: Validators.hpp:110
Check for an existing directory (returns error message if check fails)
Definition: Validators.hpp:226
ExistingDirectoryValidator()
Check for an existing file (returns error message if check fails)
Definition: Validators.hpp:220
Check for an existing path.
Definition: Validators.hpp:232
Validate the given string is a legal ipv4 address.
Definition: Validators.hpp:244
Check for an non-existing path.
Definition: Validators.hpp:238
NonexistentPathValidator()
constexpr enabler dummy
An instance to use in EnableIf.
Definition: TypeTools.hpp:34
auto smart_deref(T value) -> decltype(*value)
Definition: Validators.hpp:366
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:270
path_type
CLI enumeration of different file types.
Definition: Validators.hpp:214
std::string generate_map(const T &map, bool key_only=false)
Generate a string representation of a map.
Definition: Validators.hpp:390
std::string remove_underscore(std::string str)
remove underscores from a string
Definition: StringTools.hpp:182
std::enable_if< std::is_signed< T >::value, T >::type overflowCheck(const T &a, const T &b)
Do a check for overflow on signed numbers.
Definition: Validators.hpp:463
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:476
std::string & trim(std::string &str)
Trim whitespace from string.
Definition: StringTools.hpp:109
std::string generate_set(const T &set)
Generate a string representation of a set.
Definition: Validators.hpp:377
std::string value_string(const T &value)
get a string as a convertible value for arithmetic types
Definition: TypeTools.hpp:340
CLI11_INLINE path_type check_path(const char *file) noexcept
get the type of the path from a file name
auto search(const T &set, const V &val) -> std::pair< bool, decltype(std::begin(detail::smart_deref(set)))>
A search function.
Definition: Validators.hpp:421
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:51
bool isalpha(const std::string &str)
Verify that str consists of letters only.
Definition: StringTools.hpp:169
CLI11_INLINE std::pair< std::string, std::string > split_program_name(std::string commandline)
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition: StringTools.hpp:174
CLI11_INLINE std::string & rtrim(std::string &str)
Trim whitespace from right of string.
enabler
Simple empty scoped class.
Definition: TypeTools.hpp:31
bool lexical_cast(const std::string &input, T &output)
Integer conversion.
Definition: TypeTools.hpp:884
std::string ignore_case(std::string item)
Helper function to allow ignore_case to be passed to IsMember or Transform.
Definition: Validators.hpp:707
std::string ignore_underscore(std::string item)
Helper function to allow ignore_underscore to be passed to IsMember or Transform.
Definition: Validators.hpp:710
typename std::enable_if< B, T >::type enable_if_t
Definition: TypeTools.hpp:42
const detail::ExistingDirectoryValidator ExistingDirectory
Check for an existing directory (returns error message if check fails)
Definition: Validators.hpp:257
AsNumberWithUnit::Options operator|(const AsNumberWithUnit::Options &a, const AsNumberWithUnit::Options &b)
Definition: Validators.hpp:857
const detail::NonexistentPathValidator NonexistentPath
Check for an non-existing path.
Definition: Validators.hpp:263
const detail::IPV4Validator ValidIPV4
Check for an IP4 address.
Definition: Validators.hpp:266
const detail::ExistingPathValidator ExistingPath
Check for an existing path.
Definition: Validators.hpp:260
std::vector< std::pair< std::string, T > > TransformPairs
definition of the default transformation object
Definition: Validators.hpp:570
const detail::ExistingFileValidator ExistingFile
Check for existing file (returns error message if check fails)
Definition: Validators.hpp:254
const Range NonNegativeNumber((std::numeric_limits< double >::max)(), "NONNEGATIVE")
Check for a non negative number.
std::string ignore_space(std::string item)
Helper function to allow checks to ignore spaces to be passed to IsMember or Transform.
Definition: Validators.hpp:713
const Range PositiveNumber((std::numeric_limits< double >::min)(),(std::numeric_limits< double >::max)(), "POSITIVE")
Check for a positive valued number (val>0.0), <double>::min here is the smallest positive number.
const TypeValidator< double > Number("NUMBER")
Check for a number.
T type
Definition: TypeTools.hpp:74
T type
Definition: TypeTools.hpp:87
Definition: Validators.hpp:410
static const auto value
Definition: Validators.hpp:415
std::integral_constant< bool, value > type
Definition: Validators.hpp:416
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:98
typename T::value_type value_type
Definition: TypeTools.hpp:99
typename std::remove_const< value_type >::type first_type
Definition: TypeTools.hpp:100
static auto first(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the first value (really just the underlying value)
Definition: TypeTools.hpp:104