advgetopt 2.0.47
Parse complex command line arguments and configuration files in C++.
option_info.h
Go to the documentation of this file.
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
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
45namespace advgetopt
46{
47
48
49typedef char32_t short_name_t;
50
52
53
54short_name_t string_to_short_name(std::string const & name);
56
57
58
59
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//
76{
77public:
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
86 std::string const & name
88
89 std::string const & get_name() const;
92 std::string get_basename() const;
93 std::string get_section_name() 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;
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);
122 bool set_validator(std::nullptr_t);
124
127
129 void set_multiple_separators(char const * const * separators);
131
134 bool has_value(std::string const & value) const;
135 int find_value_index_by_key(std::string key, int idx = 0) const;
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
154
155private:
167 typedef std::vector<callback_entry_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();
179 std::string f_environment_variable_name = std::string();
181 std::string f_default_value = std::string();
182 std::string f_help = std::string();
190
191 // value read from command line, environment, .conf file
192 //
195 mutable std::vector<long> f_integer = std::vector<long>();
196 mutable std::vector<double> f_double = std::vector<double>();
197};
198
199
201{
202public:
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);
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);
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
281private:
283};
284
285
286} // namespace advgetopt
287// vim: ts=4 sw=4 et
size_t length() const
Return the length of the option's value.
size_t size() const
Return the length of the option's value.
friend bool operator>(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
bool operator!() const
Check whether the value is an empty string or not.
friend bool operator<=(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
long get_long() const
Retrieve the referenced option as a long.
double get_double() const
Retrieve the referenced option as a double.
bool empty() const
Retrieve the length of the option's value.
option_info::pointer_t f_opt
option_info_ref & operator+=(char value)
Append the character value to this option's value.
friend bool operator<(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
friend bool operator!=(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
friend std::string operator+(char value, option_info_ref const &rhs)
Concatenate a character and an option reference value.
friend bool operator==(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
option_info_ref & operator=(char value)
Set the option value to value.
friend bool operator>=(char const *value, option_info_ref const &rhs)
Compare value with the value of the right hand-side option.
string_list_t get_section_name_list() const
Retrieve a list of section names.
void set_flags(flag_t flags)
Get the flags.
bool validates(int idx=0)
Check a value validity.
void unlock()
Unlock this value.
void add_flag(flag_t flag)
Make sure a given flag is set.
std::string get_environment_variable_name() const
Retrieve the environment variable name of this option.
bool is_default_option() const
Check whether this is the default option.
std::vector< pointer_t > vector_t
Definition option_info.h:79
variables::pointer_t get_variables() const
Retrieve the list of variables held by this option info.
option_source_t f_source
void remove_default()
Remove the default value.
std::map< std::string, pointer_t > map_by_name_t
Definition option_info.h:80
std::vector< long > f_integer
flag_t get_flags() const
Retrieve the flags.
std::string get_value(int idx=0, bool raw=false) const
Retrieve the value.
long get_long(int idx=0) const
Get the value as a long.
string_list_t f_value
bool has_value(std::string const &value) const
Check whether one of the values matches the input.
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)
Set a multi-value at once.
validator::pointer_t f_validator
std::string f_default_value
callback_vector_t f_callbacks
bool is_defined() const
Check whether a value is defined.
string_list_t const & trace_sources() const
Get the trace of this option.
callback_id_t add_callback(callback_t const &c)
Add a callback to call on a change to this value.
std::vector< callback_entry_t > callback_vector_t
void lock(bool always=true)
Lock this value.
size_t size() const
Retrieve the number of values defined for this option.
pointer_t get_alias_destination() const
Get a link to the destination alias.
void set_environment_variable_name(std::string const &name)
Set the option specific environment variable name.
std::string const & get_default() const
Retrieve the default value.
void value_changed(int idx)
Call whenever the value changed so we can handle callbacks.
std::shared_ptr< option_info > pointer_t
Definition option_info.h:78
string_list_t f_trace_sources
void set_short_name(short_name_t short_name)
Assign a short name to an option.
void reset()
Reset this value.
void trace_source(int idx)
Remember the source information at of this last change.
pointer_t f_alias_destination
void set_help(std::string const &help)
Set the help string for this option.
bool has_default() const
Check whether this option has a default value.
bool get_environment_variable_value(std::string &value, char const *intro=nullptr) const
Retrieve the environment variable value of this option.
std::string get_section_name() const
Retrieve the name of the sections.
bool validate_all_values()
Validate all the values of this option_info object.
static void set_trace_sources(bool trace)
Whether the sources should be traced.
variables::pointer_t f_variables
void set_variables(variables::pointer_t vars)
Assign variables to this option info.
std::function< void(option_info const &opt)> callback_t
Definition option_info.h:82
static void set_configuration_filename(std::string const &filename)
Save the filename of the current configuration file.
string_list_t const & get_multiple_separators() const
Retrieve the list of separators for this argument.
std::string get_basename() const
Retrieve the name of the option without any section names.
bool set_validator(std::string const &name_and_params)
Set the validator for this option.
option_source_t source() const
Return the source of this option info.
std::string const & get_name() const
Get the long name of the option.
std::map< short_name_t, pointer_t > map_by_short_name_t
Definition option_info.h:81
int find_value_index_by_key(std::string key, int idx=0) const
Get the index at which a value with the given key is defined.
void set_default(std::string const &default_value)
Set the default value.
short_name_t get_short_name() const
Get the short name of the option.
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)
Replace a value.
double get_double(int idx=0) const
Get the value as a double.
std::string f_environment_variable_name
bool has_flag(flag_t flag) const
Check whether a flag is set.
void remove_callback(callback_id_t id)
Remove a callback.
std::string const & get_help() const
Get the help string.
string_list_t f_multiple_separators
short_name_t f_short_name
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)
Add a value to this option.
void set_alias_destination(pointer_t destination)
Set the alias option.
validator::pointer_t get_validator() const
Retrieve a pointer to the validator.
void remove_flag(flag_t flag)
Make sure a given flag is not set.
void set_multiple_separators(string_list_t const &separators)
Set the list of separators.
std::vector< double > f_double
std::shared_ptr< validator > pointer_t
Definition validator.h:64
std::shared_ptr< variables > pointer_t
Definition variables.h:61
Definitions of the options class a initialization functions.
The advgetopt environment to parse command line options.
static constexpr flag_t GETOPT_FLAG_NONE
Definition flags.h:43
constexpr flag_t option_flags_merge()
Definition flags.h:87
short_name_t string_to_short_name(std::string const &name)
Transform a string to a short name.
constexpr short_name_t NO_SHORT_NAME
Definition option_info.h:51
std::uint32_t flag_t
Definition flags.h:41
char32_t short_name_t
Definition option_info.h:49
std::string short_name_to_string(short_name_t short_name)
Convert a short name to a UTF-8 string.
std::vector< std::string > string_list_t
Definition utils.h:41
callback_id_t f_id
callback_t f_callback
callback_entry_t(callback_id_t id, callback_t const &c)
Declaration of validators which can be used to verify the parameters.
Declaration of the variable class.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.