CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
ExtraValidators_inl.hpp
1// Copyright (c) 2017-2025, 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 if(result.size() != 4) {
41 return std::string("Invalid IPV4 address: must have four parts (") + ip_addr + ')';
42 }
43 int num = 0;
44 for(const auto &var : result) {
45 using CLI::detail::lexical_cast;
46 bool retval = lexical_cast(var, num);
47 if(!retval) {
48 return std::string("Failed parsing number (") + var + ')';
49 }
50 if(num < 0 || num > 255) {
51 return std::string("Each IP number must be between 0 and 255 ") + var;
52 }
53 }
54 return std::string{};
55 };
56}
57
58} // namespace detail
59
60CLI11_INLINE AsSizeValue::AsSizeValue(bool kb_is_1000) : AsNumberWithUnit(get_mapping(kb_is_1000)) {
61 if(kb_is_1000) {
62 description("SIZE [b, kb(=1000b), kib(=1024b), ...]");
63 } else {
64 description("SIZE [b, kb(=1024b), ...]");
65 }
66}
67
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;
72 result_t k = 1;
73 result_t ki = 1;
74 m["b"] = 1;
75 for(std::string p : {"k", "m", "g", "t", "p", "e"}) {
76 k *= k_factor;
77 ki *= ki_factor;
78 m[p] = k;
79 m[p + "b"] = k;
80 m[p + "i"] = ki;
81 m[p + "ib"] = ki;
82 }
83 return m;
84}
85
86CLI11_INLINE std::map<std::string, AsSizeValue::result_t> AsSizeValue::get_mapping(bool kb_is_1000) {
87 if(kb_is_1000) {
88 static auto m = init_mapping(true);
89 return m;
90 }
91 static auto m = init_mapping(false);
92 return m;
93}
94
95namespace detail {} // namespace detail
97
98#if defined(CLI11_ENABLE_EXTRA_VALIDATORS) && CLI11_ENABLE_EXTRA_VALIDATORS != 0
99// new extra validators
100namespace detail {
101
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;
106 switch(permission) {
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";
111 break;
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";
116 break;
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";
121 break;
122 case Permission::none:
123 default:
124 permission_code = std::filesystem::perms::none;
125 break;
126 }
127 func_ = [permission_code](std::string &path) {
128 std::error_code ec;
129 auto p = std::filesystem::path(path);
130 if(!std::filesystem::exists(p, ec)) {
131 return std::string("Path does not exist: ") + path;
132 }
133 if(ec) {
134 return std::string("Error checking path: ") + ec.message(); // LCOV_EXCL_LINE
135 }
136 if(permission_code == std::filesystem::perms::none) {
137 return std::string{};
138 }
139 auto perms = std::filesystem::status(p, ec).permissions();
140 if(ec) {
141 return std::string("Error checking path status: ") + ec.message(); // LCOV_EXCL_LINE
142 }
143 if((perms & permission_code) == std::filesystem::perms::none) {
144 return std::string("Path does not have required permissions: ") + path;
145 }
146 return std::string{};
147 };
148 description("Path with " + permission_name + " permission");
149}
150#endif
151
152} // namespace detail
153#endif
154 // [CLI11:extra_validators_inl_hpp:end]
155} // namespace CLI
156
157#endif
Definition ExtraValidators.hpp:429
Validator & description(std::string validator_desc)
Specify the type string.
Definition Validators.hpp:99