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