Line data Source code
1 : /*
2 : * License:
3 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
4 : *
5 : * https://snapwebsites.org/
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 : // the `option_info` can be used instead or on top of the `struct option`
66 : // it is especially used to read an external getopt declaration file
67 : //
68 1074 : class option_info
69 : {
70 : public:
71 : typedef std::shared_ptr<option_info> pointer_t;
72 : typedef std::vector<pointer_t> vector_t;
73 : typedef std::map<std::string, pointer_t> map_by_name_t;
74 : typedef std::map<short_name_t, pointer_t> map_by_short_name_t;
75 :
76 : option_info(std::string const & name, short_name_t short_name = NO_SHORT_NAME);
77 :
78 : std::string const & get_name() const;
79 : void set_short_name(short_name_t short_name);
80 : short_name_t get_short_name() const;
81 : std::string get_basename() const;
82 : std::string get_section_name() const;
83 : string_list_t get_section_name_list() const;
84 : bool is_default_option() const;
85 :
86 : void set_flags(flag_t flags);
87 : void add_flag(flag_t flag);
88 : void remove_flag(flag_t flag);
89 : flag_t get_flags() const;
90 : bool has_flag(flag_t flag) const;
91 :
92 : bool has_default() const;
93 : void set_default(std::string const & default_value);
94 : void set_default(char const * default_value);
95 : void remove_default();
96 : std::string const & get_default() const;
97 :
98 : void set_help(std::string const & help);
99 : void set_help(char const * help);
100 : std::string const & get_help() const;
101 :
102 : bool set_validator(std::string const & name_and_params);
103 : bool set_validator(validator::pointer_t validator);
104 : bool set_validator(std::nullptr_t);
105 : validator::pointer_t get_validator() const;
106 :
107 : void set_alias_destination(option_info::pointer_t destination);
108 : option_info::pointer_t get_alias_destination() const;
109 :
110 : void set_multiple_separators(string_list_t const & separators);
111 : void set_multiple_separators(char const * const * separators);
112 : string_list_t const & get_multiple_separators() const;
113 :
114 : bool add_value(std::string const & value);
115 : bool set_value(int idx, std::string const & value);
116 : bool set_multiple_value(std::string const & value);
117 : bool is_defined() const;
118 : size_t size() const;
119 : std::string const & get_value(int idx = 0) const;
120 : long get_long(int idx = 0) const;
121 : void lock(bool always = true);
122 : void unlock();
123 : void reset();
124 :
125 : private:
126 : bool validate_all_values();
127 : bool validates(int idx = 0);
128 :
129 : // definitions
130 : //
131 : std::string f_name = std::string();
132 : short_name_t f_short_name = NO_SHORT_NAME;
133 : flag_t f_flags = GETOPT_FLAG_NONE;
134 : std::string f_default_value = std::string();
135 : std::string f_help = std::string();
136 : validator::pointer_t f_validator = validator::pointer_t();
137 : pointer_t f_alias_destination = pointer_t();
138 : string_list_t f_multiple_separators = string_list_t();
139 :
140 : // value read from command line, environment, .conf file
141 : //
142 : string_list_t f_value = string_list_t();
143 : mutable std::vector<long> f_integer = std::vector<long>();
144 : };
145 :
146 :
147 159 : class option_info_ref
148 : {
149 : public:
150 : option_info_ref(option_info::pointer_t opt);
151 :
152 : bool empty() const;
153 : size_t length() const;
154 : size_t size() const;
155 : long get_long() const;
156 :
157 : operator std::string () const;
158 :
159 : option_info_ref & operator = (char value);
160 : option_info_ref & operator = (char32_t value);
161 : option_info_ref & operator = (char const * value);
162 : option_info_ref & operator = (std::string const & value);
163 : option_info_ref & operator = (option_info_ref const & value);
164 :
165 : option_info_ref & operator += (char value);
166 : option_info_ref & operator += (char32_t value);
167 : option_info_ref & operator += (char const * value);
168 : option_info_ref & operator += (std::string const & value);
169 : option_info_ref & operator += (option_info_ref const & value);
170 :
171 : std::string operator + (char value) const;
172 : std::string operator + (char32_t value) const;
173 : std::string operator + (char const * value) const;
174 : std::string operator + (std::string const & value) const;
175 : std::string operator + (option_info_ref const & value) const;
176 :
177 : friend std::string operator + (char value, option_info_ref const & rhs);
178 : friend std::string operator + (char32_t value, option_info_ref const & rhs);
179 : friend std::string operator + (char const * value, option_info_ref const & rhs);
180 : friend std::string operator + (std::string const & value, option_info_ref const & rhs);
181 :
182 : operator bool () const;
183 : bool operator ! () const;
184 :
185 : bool operator == (char const * value) const;
186 : bool operator == (std::string const & value) const;
187 : bool operator == (option_info_ref const & value) const;
188 :
189 : bool operator != (char const * value) const;
190 : bool operator != (std::string const & value) const;
191 : bool operator != (option_info_ref const & value) const;
192 :
193 : bool operator < (char const * value) const;
194 : bool operator < (std::string const & value) const;
195 : bool operator < (option_info_ref const & value) const;
196 :
197 : bool operator <= (char const * value) const;
198 : bool operator <= (std::string const & value) const;
199 : bool operator <= (option_info_ref const & value) const;
200 :
201 : bool operator > (char const * value) const;
202 : bool operator > (std::string const & value) const;
203 : bool operator > (option_info_ref const & value) const;
204 :
205 : bool operator >= (char const * value) const;
206 : bool operator >= (std::string const & value) const;
207 : bool operator >= (option_info_ref const & value) const;
208 :
209 : friend bool operator == (char const * value, option_info_ref const & rhs);
210 : friend bool operator == (std::string const & value, option_info_ref const & rhs);
211 :
212 : friend bool operator != (char const * value, option_info_ref const & rhs);
213 : friend bool operator != (std::string const & value, option_info_ref const & rhs);
214 :
215 : friend bool operator < (char const * value, option_info_ref const & rhs);
216 : friend bool operator < (std::string const & value, option_info_ref const & rhs);
217 :
218 : friend bool operator <= (char const * value, option_info_ref const & rhs);
219 : friend bool operator <= (std::string const & value, option_info_ref const & rhs);
220 :
221 : friend bool operator > (char const * value, option_info_ref const & rhs);
222 : friend bool operator > (std::string const & value, option_info_ref const & rhs);
223 :
224 : friend bool operator >= (char const * value, option_info_ref const & rhs);
225 : friend bool operator >= (std::string const & value, option_info_ref const & rhs);
226 :
227 : private:
228 : option_info::pointer_t f_opt = option_info::pointer_t();
229 : };
230 :
231 :
232 : } // namespace advgetopt
233 :
234 :
235 : std::string operator + (char32_t value, std::string const & rhs);
236 : std::string operator + (std::string const & lhs, char32_t value);
237 :
238 :
239 : // vim: ts=4 sw=4 et
|