Line data Source code
1 : /*
2 : * License:
3 : * Copyright (c) 2006-2021 Made to Order Software Corp. All Rights Reserved
4 : *
5 : * https://snapwebsites.org/project/advgetopt
6 : * contact@m2osw.com
7 : *
8 : * This program is free software; you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation; either version 2 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License along
19 : * with this program; if not, write to the Free Software Foundation, Inc.,
20 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 : *
22 : * Authors:
23 : * Alexis Wilke alexis@m2osw.com
24 : * Doug Barbieri doug@m2osw.com
25 : */
26 : #pragma once
27 :
28 : /** \file
29 : * \brief Declaration of the option_info class used to record available options.
30 : *
31 : * The library offers a way to verify your command line and other options
32 : * with features such as validators and reading of various types of
33 : * configuration files.
34 : *
35 : * The class defined in this file is used to describe an option.
36 : */
37 :
38 : // advgetopt lib
39 : //
40 : #include "advgetopt/flags.h"
41 : #include "advgetopt/validator.h"
42 :
43 :
44 : // C++ lib
45 : //
46 : #include <map>
47 : #include <memory>
48 :
49 :
50 :
51 : namespace advgetopt
52 : {
53 :
54 :
55 : typedef char32_t short_name_t;
56 :
57 : constexpr short_name_t NO_SHORT_NAME = U'\0';
58 :
59 :
60 : short_name_t string_to_short_name(std::string const & name);
61 : std::string short_name_to_string(short_name_t short_name);
62 :
63 :
64 :
65 :
66 : enum class option_source_t
67 : {
68 : SOURCE_COMMAND_LINE, // set on command line
69 : SOURCE_CONFIGURATION, // read from config file
70 : SOURCE_DIRECT, // set by programmer directly
71 : SOURCE_DYNAMIC, // set dynamically
72 : SOURCE_ENVIRONMENT_VARIABLE, // found in environment variable
73 :
74 : SOURCE_UNDEFINED, // the option object exists, but the value is still undefined
75 : };
76 :
77 :
78 : // the `option_info` can be used instead or on top of the `struct option`
79 : // it is especially used to read an external getopt declaration file
80 : //
81 1298 : class option_info
82 : {
83 : public:
84 : typedef std::shared_ptr<option_info> pointer_t;
85 : typedef std::vector<pointer_t> vector_t;
86 : typedef std::map<std::string, pointer_t> map_by_name_t;
87 : typedef std::map<short_name_t, pointer_t> map_by_short_name_t;
88 : typedef std::function<void (option_info const & opt)> callback_t;
89 : typedef int callback_id_t;
90 :
91 : option_info(std::string const & name, short_name_t short_name = NO_SHORT_NAME);
92 :
93 : std::string const & get_name() const;
94 : void set_short_name(short_name_t short_name);
95 : short_name_t get_short_name() const;
96 : std::string get_basename() const;
97 : std::string get_section_name() const;
98 : string_list_t get_section_name_list() const;
99 : bool is_default_option() const;
100 :
101 : void set_flags(flag_t flags);
102 : void add_flag(flag_t flag);
103 : void remove_flag(flag_t flag);
104 : flag_t get_flags() const;
105 : bool has_flag(flag_t flag) const;
106 :
107 : bool has_default() const;
108 : void set_default(std::string const & default_value);
109 : void set_default(char const * default_value);
110 : void remove_default();
111 : std::string const & get_default() const;
112 :
113 : void set_help(std::string const & help);
114 : void set_help(char const * help);
115 : std::string const & get_help() const;
116 :
117 : bool set_validator(std::string const & name_and_params);
118 : bool set_validator(validator::pointer_t validator);
119 : bool set_validator(std::nullptr_t);
120 : validator::pointer_t get_validator() const;
121 :
122 : void set_alias_destination(option_info::pointer_t destination);
123 : option_info::pointer_t get_alias_destination() const;
124 :
125 : void set_multiple_separators(string_list_t const & separators);
126 : void set_multiple_separators(char const * const * separators);
127 : string_list_t const & get_multiple_separators() const;
128 :
129 : bool has_value(std::string const & value) const;
130 : bool add_value(std::string const & value, option_source_t source = option_source_t::SOURCE_DIRECT);
131 : bool set_value(int idx, std::string const & value, option_source_t source = option_source_t::SOURCE_DIRECT);
132 : bool set_multiple_values(std::string const & value, option_source_t source = option_source_t::SOURCE_DIRECT);
133 : bool is_defined() const;
134 : option_source_t source() const;
135 : static void set_trace_sources(bool trace);
136 : string_list_t const & trace_sources() const;
137 : static void set_configuration_filename(std::string const & filename);
138 : size_t size() const;
139 : std::string const & get_value(int idx = 0) const;
140 : long get_long(int idx = 0) const;
141 : void lock(bool always = true);
142 : void unlock();
143 : void reset();
144 :
145 : callback_id_t add_callback(callback_t const & c);
146 : void remove_callback(callback_id_t id);
147 :
148 : private:
149 37 : struct callback_entry_t
150 : {
151 2 : callback_entry_t(callback_id_t id, callback_t const & c)
152 2 : : f_id(id)
153 2 : , f_callback(c)
154 : {
155 2 : }
156 :
157 : callback_id_t f_id = 0;
158 : callback_t f_callback = callback_t();
159 : };
160 : typedef std::vector<callback_entry_t>
161 : callback_vector_t;
162 :
163 : bool validate_all_values();
164 : bool validates(int idx = 0);
165 : void value_changed(int idx);
166 : void trace_source(int idx);
167 :
168 : // definitions
169 : //
170 : std::string f_name = std::string();
171 : short_name_t f_short_name = NO_SHORT_NAME;
172 : flag_t f_flags = GETOPT_FLAG_NONE;
173 : std::string f_default_value = std::string();
174 : std::string f_help = std::string();
175 : validator::pointer_t f_validator = validator::pointer_t();
176 : pointer_t f_alias_destination = pointer_t();
177 : string_list_t f_multiple_separators = string_list_t();
178 : callback_vector_t f_callbacks = callback_vector_t();
179 : id_t f_next_callback_id = 0;
180 : string_list_t f_trace_sources = string_list_t();
181 :
182 : // value read from command line, environment, .conf file
183 : //
184 : option_source_t f_source = option_source_t::SOURCE_UNDEFINED;
185 : string_list_t f_value = string_list_t();
186 : mutable std::vector<long> f_integer = std::vector<long>();
187 : };
188 :
189 :
190 159 : class option_info_ref
191 : {
192 : public:
193 : option_info_ref(option_info::pointer_t opt);
194 :
195 : bool empty() const;
196 : size_t length() const;
197 : size_t size() const;
198 : long get_long() const;
199 :
200 : operator std::string () const;
201 :
202 : option_info_ref & operator = (char value);
203 : option_info_ref & operator = (char32_t value);
204 : option_info_ref & operator = (char const * value);
205 : option_info_ref & operator = (std::string const & value);
206 : option_info_ref & operator = (option_info_ref const & value);
207 :
208 : option_info_ref & operator += (char value);
209 : option_info_ref & operator += (char32_t value);
210 : option_info_ref & operator += (char const * value);
211 : option_info_ref & operator += (std::string const & value);
212 : option_info_ref & operator += (option_info_ref const & value);
213 :
214 : std::string operator + (char value) const;
215 : std::string operator + (char32_t value) const;
216 : std::string operator + (char const * value) const;
217 : std::string operator + (std::string const & value) const;
218 : std::string operator + (option_info_ref const & value) const;
219 :
220 : friend std::string operator + (char value, option_info_ref const & rhs);
221 : friend std::string operator + (char32_t value, option_info_ref const & rhs);
222 : friend std::string operator + (char const * value, option_info_ref const & rhs);
223 : friend std::string operator + (std::string const & value, option_info_ref const & rhs);
224 :
225 : operator bool () const;
226 : bool operator ! () const;
227 :
228 : bool operator == (char const * value) const;
229 : bool operator == (std::string const & value) const;
230 : bool operator == (option_info_ref const & value) const;
231 :
232 : bool operator != (char const * value) const;
233 : bool operator != (std::string const & value) const;
234 : bool operator != (option_info_ref const & value) const;
235 :
236 : bool operator < (char const * value) const;
237 : bool operator < (std::string const & value) const;
238 : bool operator < (option_info_ref const & value) const;
239 :
240 : bool operator <= (char const * value) const;
241 : bool operator <= (std::string const & value) const;
242 : bool operator <= (option_info_ref const & value) const;
243 :
244 : bool operator > (char const * value) const;
245 : bool operator > (std::string const & value) const;
246 : bool operator > (option_info_ref const & value) const;
247 :
248 : bool operator >= (char const * value) const;
249 : bool operator >= (std::string const & value) const;
250 : bool operator >= (option_info_ref const & value) const;
251 :
252 : friend bool operator == (char const * value, option_info_ref const & rhs);
253 : friend bool operator == (std::string const & value, option_info_ref const & rhs);
254 :
255 : friend bool operator != (char const * value, option_info_ref const & rhs);
256 : friend bool operator != (std::string const & value, option_info_ref const & rhs);
257 :
258 : friend bool operator < (char const * value, option_info_ref const & rhs);
259 : friend bool operator < (std::string const & value, option_info_ref const & rhs);
260 :
261 : friend bool operator <= (char const * value, option_info_ref const & rhs);
262 : friend bool operator <= (std::string const & value, option_info_ref const & rhs);
263 :
264 : friend bool operator > (char const * value, option_info_ref const & rhs);
265 : friend bool operator > (std::string const & value, option_info_ref const & rhs);
266 :
267 : friend bool operator >= (char const * value, option_info_ref const & rhs);
268 : friend bool operator >= (std::string const & value, option_info_ref const & rhs);
269 :
270 : private:
271 : option_info::pointer_t f_opt = option_info::pointer_t();
272 : };
273 :
274 :
275 : } // namespace advgetopt
276 :
277 :
278 : std::string operator + (char32_t value, std::string const & rhs);
279 : std::string operator + (std::string const & lhs, char32_t value);
280 :
281 :
282 : // vim: ts=4 sw=4 et
|