Line data Source code
1 : // Copyright (c) 2006-2024 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 : #pragma once 20 : 21 : /** \file 22 : * \brief Declaration of the option_info class used to record available options. 23 : * 24 : * The library offers a way to verify your command line and other options 25 : * with features such as validators and reading of various types of 26 : * configuration files. 27 : * 28 : * The class defined in this file is used to describe an option. 29 : */ 30 : 31 : // self 32 : // 33 : #include <advgetopt/flags.h> 34 : #include <advgetopt/validator.h> 35 : #include <advgetopt/variables.h> 36 : 37 : 38 : // C++ 39 : // 40 : #include <functional> 41 : #include <map> 42 : 43 : 44 : 45 : namespace advgetopt 46 : { 47 : 48 : 49 : typedef char32_t short_name_t; 50 : 51 : constexpr short_name_t NO_SHORT_NAME = U'\0'; 52 : 53 : 54 : short_name_t string_to_short_name(std::string const & name); 55 : std::string short_name_to_string(short_name_t short_name); 56 : 57 : 58 : 59 : 60 : enum class option_source_t 61 : { 62 : SOURCE_COMMAND_LINE, // set on command line 63 : SOURCE_CONFIGURATION, // read from config file 64 : SOURCE_DIRECT, // set by programmer directly 65 : SOURCE_DYNAMIC, // set dynamically (a.k.a. fluid-settings) 66 : SOURCE_ENVIRONMENT_VARIABLE, // found in environment variable 67 : 68 : SOURCE_UNDEFINED, // the option object exists, but the value is still undefined 69 : }; 70 : 71 : 72 : // the `option_info` can be used instead or on top of the `struct option` 73 : // it is especially used to read an external getopt declaration file 74 : // 75 : class option_info 76 : { 77 : public: 78 : typedef std::shared_ptr<option_info> pointer_t; 79 : typedef std::vector<pointer_t> vector_t; 80 : typedef std::map<std::string, pointer_t> map_by_name_t; 81 : typedef std::map<short_name_t, pointer_t> map_by_short_name_t; 82 : typedef std::function<void (option_info const & opt)> callback_t; 83 : typedef int callback_id_t; 84 : 85 : option_info( 86 : std::string const & name 87 : , short_name_t short_name = NO_SHORT_NAME); 88 : 89 : std::string const & get_name() const; 90 : void set_short_name(short_name_t short_name); 91 : short_name_t get_short_name() const; 92 : std::string get_basename() const; 93 : std::string get_section_name() const; 94 : string_list_t get_section_name_list() const; 95 : bool is_default_option() const; 96 : 97 : void set_environment_variable_name(std::string const & name); 98 : void set_environment_variable_name(char const * name); 99 : std::string get_environment_variable_name() const; 100 : bool get_environment_variable_value( 101 : std::string & value 102 : , char const * intro = nullptr) const; 103 : 104 : void set_flags(flag_t flags); 105 : void add_flag(flag_t flag); 106 : void remove_flag(flag_t flag); 107 : flag_t get_flags() const; 108 : bool has_flag(flag_t flag) const; 109 : 110 : bool has_default() const; 111 : void set_default(std::string const & default_value); 112 : void set_default(char const * default_value); 113 : void remove_default(); 114 : std::string const & get_default() const; 115 : 116 : void set_help(std::string const & help); 117 : void set_help(char const * help); 118 : std::string const & get_help() const; 119 : 120 : bool set_validator(std::string const & name_and_params); 121 : bool set_validator(validator::pointer_t validator); 122 : bool set_validator(std::nullptr_t); 123 : validator::pointer_t get_validator() const; 124 : 125 : void set_alias_destination(pointer_t destination); 126 : pointer_t get_alias_destination() const; 127 : 128 : void set_multiple_separators(string_list_t const & separators); 129 : void set_multiple_separators(char const * const * separators); 130 : string_list_t const & get_multiple_separators() const; 131 : 132 : void set_variables(variables::pointer_t vars); 133 : variables::pointer_t get_variables() const; 134 : bool has_value(std::string const & value) const; 135 : int find_value_index_by_key(std::string key, int idx = 0) const; 136 : bool add_value(std::string const & value, string_list_t const & option_keys = string_list_t(), option_source_t source = option_source_t::SOURCE_DIRECT); 137 : bool set_value(int idx, std::string const & value, string_list_t const & option_keys = string_list_t(), option_source_t source = option_source_t::SOURCE_DIRECT); 138 : bool set_multiple_values(std::string const & value, string_list_t const & option_keys = string_list_t(), option_source_t source = option_source_t::SOURCE_DIRECT); 139 : bool is_defined() const; 140 : option_source_t source() const; 141 : static void set_trace_sources(bool trace); 142 : string_list_t const & trace_sources() const; 143 : static void set_configuration_filename(std::string const & filename); 144 : size_t size() const; 145 : std::string get_value(int idx = 0, bool raw = false) const; 146 : long get_long(int idx = 0) const; 147 : double get_double(int idx = 0) const; 148 : void lock(bool always = true); 149 : void unlock(); 150 : void reset(); 151 : 152 : callback_id_t add_callback(callback_t const & c); 153 : void remove_callback(callback_id_t id); 154 : 155 : private: 156 : struct callback_entry_t 157 : { 158 2 : callback_entry_t(callback_id_t id, callback_t const & c) 159 2 : : f_id(id) 160 2 : , f_callback(c) 161 : { 162 2 : } 163 : 164 : callback_id_t f_id = 0; 165 : callback_t f_callback = callback_t(); 166 : }; 167 : typedef std::vector<callback_entry_t> 168 : callback_vector_t; 169 : 170 : bool validate_all_values(); 171 : bool validates(int idx = 0); 172 : void value_changed(int idx); 173 : void trace_source(int idx); 174 : 175 : // definitions 176 : // 177 : std::string f_name = std::string(); 178 : short_name_t f_short_name = NO_SHORT_NAME; 179 : std::string f_environment_variable_name = std::string(); 180 : flag_t f_flags = GETOPT_FLAG_NONE; 181 : std::string f_default_value = std::string(); 182 : std::string f_help = std::string(); 183 : validator::pointer_t f_validator = validator::pointer_t(); 184 : pointer_t f_alias_destination = pointer_t(); 185 : string_list_t f_multiple_separators = string_list_t(); 186 : callback_vector_t f_callbacks = callback_vector_t(); 187 : id_t f_next_callback_id = 0; 188 : string_list_t f_trace_sources = string_list_t(); 189 : variables::pointer_t f_variables = variables::pointer_t(); 190 : 191 : // value read from command line, environment, .conf file 192 : // 193 : option_source_t f_source = option_source_t::SOURCE_UNDEFINED; 194 : string_list_t f_value = string_list_t(); 195 : mutable std::vector<long> f_integer = std::vector<long>(); 196 : mutable std::vector<double> f_double = std::vector<double>(); 197 : }; 198 : 199 : 200 : class option_info_ref 201 : { 202 : public: 203 : option_info_ref(option_info::pointer_t opt); 204 : 205 : bool empty() const; 206 : size_t length() const; 207 : size_t size() const; 208 : long get_long() const; 209 : double get_double() const; 210 : 211 : operator std::string () const; 212 : 213 : option_info_ref & operator = (char value); 214 : option_info_ref & operator = (char32_t value); 215 : option_info_ref & operator = (char const * value); 216 : option_info_ref & operator = (std::string const & value); 217 : option_info_ref & operator = (option_info_ref const & value); 218 : 219 : option_info_ref & operator += (char value); 220 : option_info_ref & operator += (char32_t value); 221 : option_info_ref & operator += (char const * value); 222 : option_info_ref & operator += (std::string const & value); 223 : option_info_ref & operator += (option_info_ref const & value); 224 : 225 : std::string operator + (char value) const; 226 : std::string operator + (char32_t value) const; 227 : std::string operator + (char const * value) const; 228 : std::string operator + (std::string const & value) const; 229 : std::string operator + (option_info_ref const & value) const; 230 : 231 : friend std::string operator + (char value, option_info_ref const & rhs); 232 : friend std::string operator + (char32_t value, option_info_ref const & rhs); 233 : friend std::string operator + (char const * value, option_info_ref const & rhs); 234 : friend std::string operator + (std::string const & value, option_info_ref const & rhs); 235 : 236 : operator bool () const; 237 : bool operator ! () const; 238 : 239 : bool operator == (char const * value) const; 240 : bool operator == (std::string const & value) const; 241 : bool operator == (option_info_ref const & value) const; 242 : 243 : bool operator != (char const * value) const; 244 : bool operator != (std::string const & value) const; 245 : bool operator != (option_info_ref const & value) const; 246 : 247 : bool operator < (char const * value) const; 248 : bool operator < (std::string const & value) const; 249 : bool operator < (option_info_ref const & value) const; 250 : 251 : bool operator <= (char const * value) const; 252 : bool operator <= (std::string const & value) const; 253 : bool operator <= (option_info_ref const & value) const; 254 : 255 : bool operator > (char const * value) const; 256 : bool operator > (std::string const & value) const; 257 : bool operator > (option_info_ref const & value) const; 258 : 259 : bool operator >= (char const * value) const; 260 : bool operator >= (std::string const & value) const; 261 : bool operator >= (option_info_ref const & value) const; 262 : 263 : friend bool operator == (char const * value, option_info_ref const & rhs); 264 : friend bool operator == (std::string const & value, option_info_ref const & rhs); 265 : 266 : friend bool operator != (char const * value, option_info_ref const & rhs); 267 : friend bool operator != (std::string const & value, option_info_ref const & rhs); 268 : 269 : friend bool operator < (char const * value, option_info_ref const & rhs); 270 : friend bool operator < (std::string const & value, option_info_ref const & rhs); 271 : 272 : friend bool operator <= (char const * value, option_info_ref const & rhs); 273 : friend bool operator <= (std::string const & value, option_info_ref const & rhs); 274 : 275 : friend bool operator > (char const * value, option_info_ref const & rhs); 276 : friend bool operator > (std::string const & value, option_info_ref const & rhs); 277 : 278 : friend bool operator >= (char const * value, option_info_ref const & rhs); 279 : friend bool operator >= (std::string const & value, option_info_ref const & rhs); 280 : 281 : private: 282 : option_info::pointer_t f_opt = option_info::pointer_t(); 283 : }; 284 : 285 : 286 : } // namespace advgetopt 287 : // vim: ts=4 sw=4 et