CLI11 2.7.1
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
ExtraValidators_inl.hpp
1// Copyright (c) 2017-2026, University of Cincinnati, developed by Henry Schreiner
2// under NSF AWARD 1414736 and by the respective contributors.
3// All rights reserved.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6
7#pragma once
8
9// IWYU pragma: private, include "CLI/CLI.hpp"
10#include "../ExtraValidators.hpp"
11
12#if (defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS == 1) || \
13 (!defined(CLI11_DISABLE_EXTRA_VALIDATORS) || CLI11_DISABLE_EXTRA_VALIDATORS == 0)
14
15#include "../Encoding.hpp"
16#include "../Macros.hpp"
17#include "../StringTools.hpp"
18#include "../TypeTools.hpp"
19
20// [CLI11:public_includes:set]
21#include <algorithm>
22#include <fstream>
23#include <map>
24#include <string>
25#include <utility>
26// [CLI11:public_includes:end]
27
28namespace CLI {
29// [CLI11:extra_validators_inl_hpp:verbatim]
30
31namespace detail {
32
33CLI11_INLINE IPV4Validator::IPV4Validator() : Validator("IPV4") {
34 func_ = [](std::string &ip_addr) {
35 auto cdot = std::count(ip_addr.begin(), ip_addr.end(), '.');
36 if(cdot != 3u) {
37 return std::string("Invalid IPV4 address: must have 3 separators");
38 }
39 auto result = CLI::detail::split(ip_addr, '.');
40 int num = 0;
41 for(const auto &var : result) {
42 using CLI::detail::lexical_cast;
43 bool retval = lexical_cast(var, num);
44 if(!retval) {
45 return std::string("Failed parsing number (") + var + ')';
46 }
47 if(num < 0 || num > 255) {
48 return std::string("Each IP number must be between 0 and 255 ") + var;
49 }
50 }
51 return std::string{};
52 };
53}
54
55} // namespace detail
56
57CLI11_INLINE AsSizeValue::AsSizeValue(bool kb_is_1000) : AsNumberWithUnit(get_mapping(kb_is_1000)) {
58 if(kb_is_1000) {
59 description("SIZE [b, kb(=1000b), kib(=1024b), ...]");
60 } else {
61 description("SIZE [b, kb(=1024b), ...]");
62 }
63}
64
65CLI11_INLINE std::map<std::string, AsSizeValue::result_t> AsSizeValue::init_mapping(bool kb_is_1000) {
66 std::map<std::string, result_t> m;
67 result_t k_factor = kb_is_1000 ? 1000 : 1024;
68 result_t ki_factor = 1024;
69 result_t k = 1;
70 result_t ki = 1;
71 m["b"] = 1;
72 for(std::string p : {"k", "m", "g", "t", "p", "e"}) {
73 k *= k_factor;
74 ki *= ki_factor;
75 m[p] = k;
76 m[p + "b"] = k;
77 m[p + "i"] = ki;
78 m[p + "ib"] = ki;
79 }
80 return m;
81}
82
83CLI11_INLINE const std::map<std::string, AsSizeValue::result_t> &AsSizeValue::get_mapping(bool kb_is_1000) {
84 if(kb_is_1000) {
85 static auto m = init_mapping(true);
86 return m;
87 }
88 static auto m = init_mapping(false);
89 return m;
90}
91
92#if defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS != 0
93// new extra validators
94
95#if defined CLI11_HAS_FILESYSTEM && CLI11_HAS_FILESYSTEM > 0
96namespace detail {
97CLI11_INLINE PermissionValidator::PermissionValidator(Permission permission) {
98 std::filesystem::perms permission_code = std::filesystem::perms::none;
99 std::string permission_name;
100 switch(permission) {
101 case Permission::read:
102 permission_code = std::filesystem::perms::owner_read | std::filesystem::perms::group_read |
103 std::filesystem::perms::others_read;
104 permission_name = "read";
105 break;
106 case Permission::write:
107 permission_code = std::filesystem::perms::owner_write | std::filesystem::perms::group_write |
108 std::filesystem::perms::others_write;
109 permission_name = "write";
110 break;
111 case Permission::exec:
112 permission_code = std::filesystem::perms::owner_exec | std::filesystem::perms::group_exec |
113 std::filesystem::perms::others_exec;
114 permission_name = "exec";
115 break;
116 case Permission::none:
117 default:
118 permission_code = std::filesystem::perms::none;
119 break;
120 }
121 func_ = [permission_code](std::string &path) {
122 std::error_code ec;
123 auto p = std::filesystem::path(path);
124 if(!std::filesystem::exists(p, ec)) {
125 return std::string("Path does not exist: ") + path;
126 }
127 if(ec) {
128 return std::string("Error checking path: ") + ec.message(); // LCOV_EXCL_LINE
129 }
130 if(permission_code == std::filesystem::perms::none) {
131 return std::string{};
132 }
133 auto perms = std::filesystem::status(p, ec).permissions();
134 if(ec) {
135 return std::string("Error checking path status: ") + ec.message(); // LCOV_EXCL_LINE
136 }
137 if((perms & permission_code) == std::filesystem::perms::none) {
138 return std::string("Path does not have required permissions: ") + path;
139 }
140 return std::string{};
141 };
142 description("Path with " + permission_name + " permission");
143}
144} // namespace detail
145
146CLI11_INLINE FileSizeValidator::FileSizeValidator(std::uint64_t min_size, std::uint64_t max_size) {
147 std::string desc;
148 if(max_size == 0) {
149 desc = "File size at least " + std::to_string(min_size) + " bytes";
150 } else {
151 desc = "File size between " + std::to_string(min_size) + " and " + std::to_string(max_size) + " bytes";
152 }
153 description(desc);
154 func_ = [min_size, max_size](std::string &path) {
155 std::error_code ec;
156 auto p = std::filesystem::path(path);
157 if(!std::filesystem::exists(p, ec)) {
158 return std::string("File does not exist: ") + path;
159 }
160 if(ec) {
161 return std::string("Error checking file: ") + ec.message(); // LCOV_EXCL_LINE
162 }
163 auto size = std::filesystem::file_size(p, ec);
164 if(ec) {
165 return std::string("Error getting file size: ") + ec.message(); // LCOV_EXCL_LINE
166 }
167 if(size < min_size) {
168 return std::string("File size ") + std::to_string(size) + " bytes is less than minimum " +
169 std::to_string(min_size) + " bytes";
170 }
171 if(max_size > 0 && size > max_size) {
172 return std::string("File size ") + std::to_string(size) + " bytes exceeds maximum " +
173 std::to_string(max_size) + " bytes";
174 }
175 return std::string{};
176 };
177}
178#endif
179
180#endif
181// [CLI11:extra_validators_inl_hpp:end]
182} // namespace CLI
183
184#endif
AsSizeValue(bool kb_is_1000)
Definition ExtraValidators_inl.hpp:57
Validator & description(std::string validator_desc)
Specify the type string.
Definition Validators.hpp:99