10#include "../ExtraValidators.hpp"
12#if (defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS == 1) || \
13 (!defined(CLI11_DISABLE_EXTRA_VALIDATORS) || CLI11_DISABLE_EXTRA_VALIDATORS == 0)
15#include "../Encoding.hpp"
16#include "../Macros.hpp"
17#include "../StringTools.hpp"
18#include "../TypeTools.hpp"
33CLI11_INLINE IPV4Validator::IPV4Validator() : Validator(
"IPV4") {
34 func_ = [](std::string &ip_addr) {
35 auto cdot = std::count(ip_addr.begin(), ip_addr.end(),
'.');
37 return std::string(
"Invalid IPV4 address: must have 3 separators");
39 auto result = CLI::detail::split(ip_addr,
'.');
40 if(result.size() != 4) {
41 return std::string(
"Invalid IPV4 address: must have four parts (") + ip_addr +
')';
44 for(
const auto &var : result) {
45 using CLI::detail::lexical_cast;
46 bool retval = lexical_cast(var, num);
48 return std::string(
"Failed parsing number (") + var +
')';
50 if(num < 0 || num > 255) {
51 return std::string(
"Each IP number must be between 0 and 255 ") + var;
60CLI11_INLINE AsSizeValue::AsSizeValue(
bool kb_is_1000) :
AsNumberWithUnit(get_mapping(kb_is_1000)) {
62 description(
"SIZE [b, kb(=1000b), kib(=1024b), ...]");
68CLI11_INLINE std::map<std::string, AsSizeValue::result_t> AsSizeValue::init_mapping(
bool kb_is_1000) {
69 std::map<std::string, result_t> m;
70 result_t k_factor = kb_is_1000 ? 1000 : 1024;
71 result_t ki_factor = 1024;
75 for(std::string p : {
"k",
"m",
"g",
"t",
"p",
"e"}) {
86CLI11_INLINE std::map<std::string, AsSizeValue::result_t> AsSizeValue::get_mapping(
bool kb_is_1000) {
88 static auto m = init_mapping(
true);
91 static auto m = init_mapping(
false);
98#if defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS != 0
102#if defined CLI11_HAS_FILESYSTEM && CLI11_HAS_FILESYSTEM > 0
103CLI11_INLINE PermissionValidator::PermissionValidator(Permission permission) {
104 std::filesystem::perms permission_code = std::filesystem::perms::none;
105 std::string permission_name;
107 case Permission::read:
108 permission_code = std::filesystem::perms::owner_read | std::filesystem::perms::group_read |
109 std::filesystem::perms::others_read;
110 permission_name =
"read";
112 case Permission::write:
113 permission_code = std::filesystem::perms::owner_write | std::filesystem::perms::group_write |
114 std::filesystem::perms::others_write;
115 permission_name =
"write";
117 case Permission::exec:
118 permission_code = std::filesystem::perms::owner_exec | std::filesystem::perms::group_exec |
119 std::filesystem::perms::others_exec;
120 permission_name =
"exec";
122 case Permission::none:
124 permission_code = std::filesystem::perms::none;
127 func_ = [permission_code](std::string &path) {
129 auto p = std::filesystem::path(path);
130 if(!std::filesystem::exists(p, ec)) {
131 return std::string(
"Path does not exist: ") + path;
134 return std::string(
"Error checking path: ") + ec.message();
136 if(permission_code == std::filesystem::perms::none) {
137 return std::string{};
139 auto perms = std::filesystem::status(p, ec).permissions();
141 return std::string(
"Error checking path status: ") + ec.message();
143 if((perms & permission_code) == std::filesystem::perms::none) {
144 return std::string(
"Path does not have required permissions: ") + path;
146 return std::string{};
148 description(
"Path with " + permission_name +
" permission");
Definition ExtraValidators.hpp:429
Validator & description(std::string validator_desc)
Specify the type string.
Definition Validators.hpp:99