advgetopt 2.0.49
Parse complex command line arguments and configuration files in C++.
validator_email.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
26// self
27//
29
30
31// cppthread
32//
33#include <cppthread/log.h>
34
35
36// libtld
37//
38#include <libtld/tld.h>
39
40
41// last include
42//
43#include <snapdev/poison.h>
44
45
46
47
48namespace advgetopt
49{
50
51
52
53namespace
54{
55
56
57
59 : public validator_factory
60{
61public:
63 {
64 validator::register_validator(*this);
65 }
66
67 virtual std::string get_name() const override
68 {
69 return std::string("email");
70 }
71
72 virtual std::shared_ptr<validator> create(string_list_t const & data) const override
73 {
74 return std::make_shared<validator_email>(data);
75 }
76};
77
79
80
81
82} // no name namespace
83
84
85
86
87
89{
90 // at this time the tld library does not offer support for
91 // flags or anything
92 //
93 if(param_list.size() > 1)
94 {
95 cppthread::log << cppthread::log_level_t::error
96 << "validator_email() supports zero or one parameter."
97 << cppthread::end;
98 return;
99 }
100
101 if(param_list.size() == 1)
102 {
103 if(param_list[0] == "single")
104 {
105 f_multiple = false;
106 }
107 else if(param_list[0] == "multiple")
108 {
109 f_multiple = true;
110 }
111 else
112 {
113 cppthread::log << cppthread::log_level_t::error
114 << "validator_email(): unknown parameter \""
115 << param_list[0]
116 << "\"."
117 << cppthread::end;
118 }
119 }
120}
121
122
129std::string validator_email::name() const
130{
131 return std::string("email");
132}
133
134
143bool validator_email::validate(std::string const & value) const
144{
146 if(list.parse(value, 0) != TLD_RESULT_SUCCESS)
147 {
148 return false;
149 }
150
151 if(f_multiple)
152 {
153 return list.count() > 0;
154 }
155 return list.count() == 1;
156}
157
158
159
160} // namespace advgetopt
161// vim: ts=4 sw=4 et
virtual std::shared_ptr< validator > create(string_list_t const &data) const override
virtual std::string name() const override
Return the name of this validator.
validator_email(string_list_t const &data)
virtual bool validate(std::string const &value) const override
Check the value to make sure emails are considered valid.
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.