10#include "../Validators.hpp" 
   12#include "../Encoding.hpp" 
   13#include "../Macros.hpp" 
   14#include "../StringTools.hpp" 
   15#include "../TypeTools.hpp" 
   27    std::string retstring;
 
   30            std::string value = str;
 
   31            retstring = 
func_(value);
 
   33            retstring = 
func_(str);
 
 
   41    newval.
desc_function_ = [validator_desc]() { 
return validator_desc; };
 
 
   48    newval._merge_description(*
this, other, 
" AND ");
 
   51    const std::function<std::string(std::string & filename)> &f1 = 
func_;
 
   52    const std::function<std::string(std::string & filename)> &f2 = other.
func_;
 
   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 + 
")";
 
 
   70    newval._merge_description(*
this, other, 
" OR ");
 
   73    const std::function<std::string(std::string &)> &f1 = 
func_;
 
   74    const std::function<std::string(std::string &)> &f2 = other.
func_;
 
   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())
 
   82        return std::string(
"(") + s1 + 
") OR (" + s2 + 
")";
 
 
   94        return (!str.empty()) ? std::string(
"NOT ") + str : std::string{};
 
   97    const std::function<std::string(std::string & res)> &f1 = 
func_;
 
   99    newval.
func_ = [f1, dfunc1](std::string &test) -> std::string {
 
  100        std::string s1 = f1(test);
 
  102            return std::string(
"check ") + dfunc1() + 
" succeeded improperly";
 
  104        return std::string{};
 
 
  112Validator::_merge_description(
const Validator &val1, 
const Validator &val2, 
const std::string &merger) {
 
  118        std::string f1 = dfunc1();
 
  119        std::string f2 = dfunc2();
 
  120        if((f1.empty()) || (f2.empty())) {
 
  123        return std::string(1, 
'(') + f1 + 
')' + merger + 
'(' + f2 + 
')';
 
  129#if defined CLI11_HAS_FILESYSTEM && CLI11_HAS_FILESYSTEM > 0 
  130CLI11_INLINE path_type check_path(
const char *file) 
noexcept {
 
  132    auto stat = std::filesystem::status(to_path(file), ec);
 
  134        return path_type::nonexistent;
 
  136    switch(stat.type()) {
 
  137    case std::filesystem::file_type::none:  
 
  138    case std::filesystem::file_type::not_found:
 
  139        return path_type::nonexistent;  
 
  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:
 
  150        return path_type::file;
 
  154CLI11_INLINE path_type check_path(
const char *file) 
noexcept {
 
  156    struct __stat64 buffer;
 
  157    if(_stat64(file, &buffer) == 0) {
 
  158        return ((buffer.st_mode & S_IFDIR) != 0) ? path_type::directory : path_type::file;
 
  162    if(stat(file, &buffer) == 0) {
 
  163        return ((buffer.st_mode & S_IFDIR) != 0) ? path_type::directory : path_type::file;
 
  166    return path_type::nonexistent;
 
  170CLI11_INLINE ExistingFileValidator::ExistingFileValidator() : Validator(
"FILE") {
 
  171    func_ = [](std::string &filename) {
 
  172        auto path_result = check_path(filename.c_str());
 
  173        if(path_result == path_type::nonexistent) {
 
  174            return "File does not exist: " + filename;
 
  176        if(path_result == path_type::directory) {
 
  177            return "File is actually a directory: " + filename;
 
  179        return std::string();
 
  183CLI11_INLINE ExistingDirectoryValidator::ExistingDirectoryValidator() : Validator(
"DIR") {
 
  184    func_ = [](std::string &filename) {
 
  185        auto path_result = check_path(filename.c_str());
 
  186        if(path_result == path_type::nonexistent) {
 
  187            return "Directory does not exist: " + filename;
 
  189        if(path_result == path_type::file) {
 
  190            return "Directory is actually a file: " + filename;
 
  192        return std::string();
 
  196CLI11_INLINE ExistingPathValidator::ExistingPathValidator() : Validator(
"PATH(existing)") {
 
  197    func_ = [](std::string &filename) {
 
  198        auto path_result = check_path(filename.c_str());
 
  199        if(path_result == path_type::nonexistent) {
 
  200            return "Path does not exist: " + filename;
 
  202        return std::string();
 
  206CLI11_INLINE NonexistentPathValidator::NonexistentPathValidator() : Validator(
"PATH(non-existing)") {
 
  207    func_ = [](std::string &filename) {
 
  208        auto path_result = check_path(filename.c_str());
 
  209        if(path_result != path_type::nonexistent) {
 
  210            return "Path already exists: " + filename;
 
  212        return std::string();
 
  216CLI11_INLINE EscapedStringTransformer::EscapedStringTransformer() {
 
  217    func_ = [](std::string &str) {
 
  219            if(str.size() > 1 && (str.front() == 
'\"' || str.front() == 
'\'' || str.front() == 
'`') &&
 
  220               str.front() == str.back()) {
 
  221                process_quoted_string(str);
 
  222            } 
else if(str.find_first_of(
'\\') != std::string::npos) {
 
  223                if(detail::is_binary_escaped_string(str)) {
 
  224                    str = detail::extract_binary_string(str);
 
  226                    str = remove_escaped_characters(str);
 
  229            return std::string{};
 
  230        } 
catch(
const std::invalid_argument &ia) {
 
  231            return std::string(ia.what());
 
  237CLI11_INLINE FileOnDefaultPath::FileOnDefaultPath(std::string default_path, 
bool enableErrorReturn)
 
  238    : Validator(
"FILE") {
 
  239    func_ = [default_path, enableErrorReturn](std::string &filename) {
 
  240        auto path_result = detail::check_path(filename.c_str());
 
  241        if(path_result == detail::path_type::nonexistent) {
 
  242            std::string test_file_path = default_path;
 
  243            if(default_path.back() != 
'/' && default_path.back() != 
'\\') {
 
  245                test_file_path += 
'/';
 
  247            test_file_path.append(filename);
 
  248            path_result = detail::check_path(test_file_path.c_str());
 
  249            if(path_result == detail::path_type::file) {
 
  250                filename = test_file_path;
 
  252                if(enableErrorReturn) {
 
  253                    return "File does not exist: " + filename;
 
  257        return std::string{};
 
  263CLI11_INLINE std::pair<std::string, std::string> split_program_name(std::string commandline) {
 
  265    std::pair<std::string, std::string> vals;
 
  267    auto esp = commandline.find_first_of(
' ', 1);
 
  268    while(detail::check_path(commandline.substr(0, esp).c_str()) != path_type::file) {
 
  269        esp = commandline.find_first_of(
' ', esp + 1);
 
  270        if(esp == std::string::npos) {
 
  273            if(commandline[0] == 
'"' || commandline[0] == 
'\'' || commandline[0] == 
'`') {
 
  274                bool embeddedQuote = 
false;
 
  275                auto keyChar = commandline[0];
 
  276                auto end = commandline.find_first_of(keyChar, 1);
 
  277                while((end != std::string::npos) && (commandline[end - 1] == 
'\\')) {  
 
  278                    end = commandline.find_first_of(keyChar, end + 1);
 
  279                    embeddedQuote = 
true;
 
  281                if(end != std::string::npos) {
 
  282                    vals.first = commandline.substr(1, end - 1);
 
  285                        vals.first = find_and_replace(vals.first, std::string(
"\\") + keyChar, std::string(1, keyChar));
 
  288                    esp = commandline.find_first_of(
' ', 1);
 
  291                esp = commandline.find_first_of(
' ', 1);
 
  297    if(vals.first.empty()) {
 
  298        vals.first = commandline.substr(0, esp);
 
  303    vals.second = (esp < commandline.length() - 1) ? commandline.substr(esp + 1) : std::string{};
 
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