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