advgetopt 2.0.49
Parse complex command line arguments and configuration files in C++.
validator_regex.cpp
Go to the documentation of this file.
1// Copyright (c) 2006-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/advgetopt
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
30// self
31//
33
34
35// cppthread
36//
37#include <cppthread/log.h>
38
39
40// last include
41//
42#include <snapdev/poison.h>
43
44
45
46
47namespace advgetopt
48{
49
50
51
52namespace
53{
54
55
56
58 : public validator_factory
59{
60public:
62 {
63 validator::register_validator(*this);
64 }
65
66 virtual std::string get_name() const override
67 {
68 return std::string("regex");
69 }
70
71 virtual std::shared_ptr<validator> create(string_list_t const & data) const override
72 {
73 return std::make_shared<validator_regex>(data);
74 }
75};
76
78
79
80
81} // no name namespace
82
83
84
85
86
88{
89 if(regex_list.size() > 1)
90 {
91 cppthread::log << cppthread::log_level_t::error
92 << "validator_regex() only supports one parameter; "
93 << regex_list.size()
94 << " were supplied; single or double quotation may be required?"
95 << cppthread::end;
96 return;
97 }
98
99 std::string regex;
100 if(!regex_list.empty())
101 {
102 regex = regex_list[0];
103 }
104 std::regex::flag_type flags = std::regex_constants::extended;
105 if(regex.length() >= 2
106 && regex[0] == '/')
107 {
108 auto it(regex.end());
109 for(--it; it != regex.begin(); --it)
110 {
111 if(*it == '/')
112 {
113 break;
114 }
115 switch(*it)
116 {
117 case 'i':
118 flags |= std::regex_constants::icase;
119 break;
120
121 default:
122 cppthread::log << cppthread::log_level_t::error
123 << "unsupported regex flag "
124 << *it
125 << " in regular expression \""
126 << regex
127 << "\"."
128 << cppthread::end;
129 break;
130
131 }
132 }
133 if(it == regex.begin())
134 {
135 cppthread::log << cppthread::log_level_t::error
136 << "invalid regex definition, ending / is missing in \""
137 << regex
138 << "\"."
139 << cppthread::end;
140
141 f_regex = std::regex(std::string(regex.begin() + 1, regex.end()), flags);
142 }
143 else
144 {
145 f_regex = std::regex(std::string(regex.begin() + 1, it), flags);
146 }
147 }
148 else
149 {
150 f_regex = std::regex(regex, flags);
151 }
152}
153
154
161std::string validator_regex::name() const
162{
163 return std::string("regex");
164}
165
166
176bool validator_regex::validate(std::string const & value) const
177{
178 std::smatch info;
179 return std::regex_match(value, info, f_regex);
180}
181
182
183
184} // namespace advgetopt
185// vim: ts=4 sw=4 et
virtual std::shared_ptr< validator > create(string_list_t const &data) const override
validator_regex(string_list_t const &data)
virtual std::string name() const override
Return the name of this validator.
virtual bool validate(std::string const &value) const override
Check the value against a regular expression.
The advgetopt environment to parse command line options.
Definition version.h:37
constexpr flag_t option_flags_merge()
Definition flags.h:87
std::vector< std::string > string_list_t
Definition utils.h:41
Declaration of validators which can be used to verify the parameters.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.