$mermaidjs
CLI11 2.7.2
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
Validators_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 "../Validators.hpp"
11
12#include "../Encoding.hpp"
13#include "../Macros.hpp"
14#include "../StringTools.hpp"
15#include "../TypeTools.hpp"
16
17// [CLI11:public_includes:set]
18#include <functional>
19#include <stdexcept>
20#include <string>
21#include <system_error>
22#include <utility>
23// [CLI11:public_includes:end]
24
25namespace CLI {
26// [CLI11:validators_inl_hpp:verbatim]
27
28CLI11_INLINE Validator::Validator(std::string validator_desc, std::function<std::string(std::string &)> func)
29 : desc_function_([validator_desc]() { return validator_desc; }), func_(std::move(func)) {}
30
31CLI11_INLINE Validator::Validator(std::function<std::string(std::string &)> op,
32 std::string validator_desc,
33 std::string validator_name)
34 : desc_function_([validator_desc]() { return validator_desc; }), func_(std::move(op)),
35 name_(std::move(validator_name)) {}
36
37CLI11_INLINE std::string Validator::operator()(std::string &str) const {
38 std::string retstring;
39 if(active_) {
40 if(non_modifying_) {
41 std::string value = str;
42 retstring = func_(value);
43 } else {
44 retstring = func_(str);
45 }
46 }
47 return retstring;
48}
49
50CLI11_INLINE std::string Validator::operator()(const std::string &str) const {
51 std::string value = str;
52 return (active_) ? func_(value) : std::string{};
53}
54
55CLI11_NODISCARD CLI11_INLINE Validator Validator::description(std::string validator_desc) const {
56 Validator newval(*this);
57 newval.desc_function_ = [validator_desc]() { return validator_desc; };
58 return newval;
59}
60
61CLI11_NODISCARD CLI11_INLINE std::string Validator::get_description() const {
62 if(active_) {
63 return desc_function_();
64 }
65 return std::string{};
66}
67
68CLI11_NODISCARD CLI11_INLINE Validator Validator::name(std::string validator_name) const {
69 Validator newval(*this);
70 newval.name_ = std::move(validator_name);
71 return newval;
72}
73
74CLI11_NODISCARD CLI11_INLINE Validator Validator::active(bool active_val) const {
75 Validator newval(*this);
76 newval.active_ = active_val;
77 return newval;
78}
79
80CLI11_NODISCARD CLI11_INLINE Validator Validator::application_index(int app_index) const {
81 Validator newval(*this);
82 newval.application_index_ = app_index;
83 return newval;
84}
85
86CLI11_INLINE Validator Validator::operator&(const Validator &other) const {
87 Validator newval;
88
89 newval._merge_description(*this, other, " AND ");
90
91 // Give references (will make a copy in lambda function)
92 const std::function<std::string(std::string & filename)> &f1 = func_;
93 const std::function<std::string(std::string & filename)> &f2 = other.func_;
94
95 newval.func_ = [f1, f2](std::string &input) {
96 std::string s1 = f1(input);
97 std::string s2 = f2(input);
98 if(!s1.empty() && !s2.empty())
99 return std::string("(") + s1 + ") AND (" + s2 + ")";
100 return s1 + s2;
101 };
102
103 newval.active_ = active_ && other.active_;
105 return newval;
106}
107
108CLI11_INLINE Validator Validator::operator|(const Validator &other) const {
109 Validator newval;
110
111 newval._merge_description(*this, other, " OR ");
112
113 // Give references (will make a copy in lambda function)
114 const std::function<std::string(std::string &)> &f1 = func_;
115 const std::function<std::string(std::string &)> &f2 = other.func_;
116
117 newval.func_ = [f1, f2](std::string &input) {
118 std::string s1 = f1(input);
119 std::string s2 = f2(input);
120 if(s1.empty() || s2.empty())
121 return std::string();
122
123 return std::string("(") + s1 + ") OR (" + s2 + ")";
124 };
125 newval.active_ = active_ && other.active_;
127 return newval;
128}
129
130CLI11_INLINE Validator Validator::operator!() const {
131 Validator newval;
132 const std::function<std::string()> &dfunc1 = desc_function_;
133 newval.desc_function_ = [dfunc1]() {
134 auto str = dfunc1();
135 return (!str.empty()) ? std::string("NOT ") + str : std::string{};
136 };
137 // Give references (will make a copy in lambda function)
138 const std::function<std::string(std::string & res)> &f1 = func_;
139
140 newval.func_ = [f1, dfunc1](std::string &test) -> std::string {
141 std::string s1 = f1(test);
142 if(s1.empty()) {
143 return std::string("check ") + dfunc1() + " succeeded improperly";
144 }
145 return std::string{};
146 };
147 newval.active_ = active_;
149 return newval;
150}
151
152CLI11_INLINE void
153Validator::_merge_description(const Validator &val1, const Validator &val2, const std::string &merger) {
154
155 const std::function<std::string()> &dfunc1 = val1.desc_function_;
156 const std::function<std::string()> &dfunc2 = val2.desc_function_;
157
158 desc_function_ = [=]() {
159 std::string f1 = dfunc1();
160 std::string f2 = dfunc2();
161 if((f1.empty()) || (f2.empty())) {
162 return f1 + f2;
163 }
164 return std::string(1, '(') + f1 + ')' + merger + '(' + f2 + ')';
165 };
166}
167
168namespace detail {
169
170#if defined CLI11_HAS_FILESYSTEM && CLI11_HAS_FILESYSTEM > 0
171CLI11_INLINE path_type check_path(const char *file) noexcept {
172 std::error_code ec;
173 auto stat = std::filesystem::status(to_path(file), ec);
174 if(ec) {
175 return path_type::nonexistent;
176 }
177 switch(stat.type()) {
178 case std::filesystem::file_type::none: // LCOV_EXCL_LINE
179 case std::filesystem::file_type::not_found:
180 return path_type::nonexistent; // LCOV_EXCL_LINE
181 case std::filesystem::file_type::directory:
182 return path_type::directory;
183 case std::filesystem::file_type::symlink:
184 case std::filesystem::file_type::block:
185 case std::filesystem::file_type::character:
186 case std::filesystem::file_type::fifo:
187 case std::filesystem::file_type::socket:
188 case std::filesystem::file_type::regular:
189 case std::filesystem::file_type::unknown:
190 default:
191 return path_type::file;
192 }
193}
194#else
195CLI11_INLINE path_type check_path(const char *file) noexcept {
196#if defined(_MSC_VER)
197 struct __stat64 buffer;
198 if(_stat64(file, &buffer) == 0) {
199 return ((buffer.st_mode & S_IFDIR) != 0) ? path_type::directory : path_type::file;
200 }
201#else
202 struct stat buffer;
203 if(stat(file, &buffer) == 0) {
204 return ((buffer.st_mode & S_IFDIR) != 0) ? path_type::directory : path_type::file;
205 }
206#endif
207 return path_type::nonexistent;
208}
209#endif
210
211CLI11_INLINE ExistingFileValidator::ExistingFileValidator() : Validator("FILE") {
212 func_ = [](std::string &filename) -> std::string {
213 if(filename.empty())
214 return std::string("File name is missing.");
215
216 auto path_result = check_path(filename.c_str());
217 if(path_result == path_type::nonexistent) {
218 return "File does not exist: " + filename;
219 }
220 if(path_result == path_type::directory) {
221 return "File is actually a directory: " + filename;
222 }
223 return std::string();
224 };
225}
226
227CLI11_INLINE ExistingDirectoryValidator::ExistingDirectoryValidator() : Validator("DIR") {
228 func_ = [](std::string &filename) {
229 auto path_result = check_path(filename.c_str());
230 if(path_result == path_type::nonexistent) {
231 return "Directory does not exist: " + filename;
232 }
233 if(path_result == path_type::file) {
234 return "Directory is actually a file: " + filename;
235 }
236 return std::string();
237 };
238}
239
240CLI11_INLINE ExistingPathValidator::ExistingPathValidator() : Validator("PATH(existing)") {
241 func_ = [](std::string &filename) {
242 auto path_result = check_path(filename.c_str());
243 if(path_result == path_type::nonexistent) {
244 return "Path does not exist: " + filename;
245 }
246 return std::string();
247 };
248}
249
250CLI11_INLINE NonexistentPathValidator::NonexistentPathValidator() : Validator("PATH(non-existing)") {
251 func_ = [](std::string &filename) {
252 auto path_result = check_path(filename.c_str());
253 if(path_result != path_type::nonexistent) {
254 return "Path already exists: " + filename;
255 }
256 return std::string();
257 };
258}
259
260CLI11_INLINE EscapedStringTransformer::EscapedStringTransformer() {
261 func_ = [](std::string &str) {
262 try {
263 if(str.size() > 1 && (str.front() == '\"' || str.front() == '\'' || str.front() == '`') &&
264 str.front() == str.back()) {
265 process_quoted_string(str);
266 } else if(str.find_first_of('\\') != std::string::npos) {
267 if(detail::is_binary_escaped_string(str)) {
268 str = detail::extract_binary_string(str);
269 } else {
270 str = remove_escaped_characters(str);
271 }
272 }
273 return std::string{};
274 } catch(const std::invalid_argument &ia) {
275 return std::string(ia.what());
276 }
277 };
278}
279} // namespace detail
280
281CLI11_INLINE FileOnDefaultPath::FileOnDefaultPath(std::string default_path, bool enableErrorReturn)
282 : Validator("FILE") {
283 func_ = [default_path, enableErrorReturn](std::string &filename) {
284 auto path_result = detail::check_path(filename.c_str());
285 if(path_result == detail::path_type::nonexistent) {
286 std::string test_file_path = default_path;
287 if(!default_path.empty() && default_path.back() != '/' && default_path.back() != '\\') {
288 // Add folder separator
289 test_file_path += '/';
290 }
291 test_file_path.append(filename);
292 path_result = detail::check_path(test_file_path.c_str());
293 if(path_result == detail::path_type::file) {
294 filename = test_file_path;
295 } else {
296 if(enableErrorReturn) {
297 return "File does not exist: " + filename;
298 }
299 }
300 }
301 return std::string{};
302 };
303}
304
305namespace detail {
306
307CLI11_INLINE std::pair<std::string, std::string> split_program_name(std::string commandline) {
308 // try to determine the programName
309 std::pair<std::string, std::string> vals;
310 trim(commandline);
311 auto esp = commandline.find_first_of(' ', 1);
312 while(detail::check_path(commandline.substr(0, esp).c_str()) != path_type::file) {
313 if(esp == std::string::npos) {
314 // if we have reached the end and haven't found a valid file just assume the first argument is the
315 // program name
316 if(commandline[0] == '"' || commandline[0] == '\'' || commandline[0] == '`') {
317 bool embeddedQuote = false;
318 auto keyChar = commandline[0];
319 auto end = commandline.find_first_of(keyChar, 1);
320 while((end != std::string::npos) && (commandline[end - 1] == '\\')) { // deal with escaped quotes
321 end = commandline.find_first_of(keyChar, end + 1);
322 embeddedQuote = true;
323 }
324 if(end != std::string::npos) {
325 vals.first = commandline.substr(1, end - 1);
326 esp = end + 1;
327 if(embeddedQuote) {
328 vals.first = find_and_replace(vals.first, std::string("\\") + keyChar, std::string(1, keyChar));
329 }
330 } else {
331 esp = commandline.find_first_of(' ', 1);
332 }
333 } else {
334 esp = commandline.find_first_of(' ', 1);
335 }
336
337 break;
338 }
339 esp = commandline.find_first_of(' ', esp + 1);
340 }
341 if(vals.first.empty()) {
342 vals.first = commandline.substr(0, esp);
343 rtrim(vals.first);
344 }
345
346 // strip the program name
347 vals.second = (esp < commandline.length() - 1) ? commandline.substr(esp + 1) : std::string{};
348 ltrim(vals.second);
349 return vals;
350}
351
352} // namespace detail
353
354// [CLI11:validators_inl_hpp:end]
355} // namespace CLI
Some validators that are provided.
Definition Validators.hpp:55
Validator operator&(const Validator &other) const
Definition Validators_inl.hpp:86
int application_index_
A Validator will only apply to an indexed value (-1 is all elements).
Definition Validators.hpp:66
Validator & description(std::string validator_desc)
Specify the type string.
Definition Validators.hpp:96
Validator operator|(const Validator &other) const
Definition Validators_inl.hpp:108
bool active_
Enable for Validator to allow it to be disabled if need be.
Definition Validators.hpp:68
bool non_modifying_
specify that a validator should not modify the input
Definition Validators.hpp:70
Validator & name(std::string validator_name)
Specify the type string.
Definition Validators.hpp:106
std::function< std::string()> desc_function_
This is the description function, if empty the description_ will be used.
Definition Validators.hpp:58
std::function< std::string(std::string &)> func_
Definition Validators.hpp:62
CLI11_NODISCARD std::string get_description() const
Generate type description information for the Validator.
Definition Validators_inl.hpp:61
Validator & active(bool active_val=true)
Specify whether the Validator is active or not.
Definition Validators.hpp:115
std::string name_
The name for search purposes of the Validator.
Definition Validators.hpp:64
Validator operator!() const
Create a validator that fails when a given validator succeeds.
Definition Validators_inl.hpp:130
std::string operator()(std::string &str) const
Definition Validators_inl.hpp:37
Validator & application_index(int app_index)
Specify the application index of a validator.
Definition Validators.hpp:128