advgetopt 2.0.47
Parse complex command line arguments and configuration files in C++.
advgetopt.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
29// advgetopt
30//
32#include <advgetopt/options.h>
33#include <advgetopt/validator.h>
34#include <advgetopt/variables.h>
35
36
37// C++
38//
39#include <limits>
40#include <map>
41#include <memory>
42#include <ostream>
43#include <vector>
44
45
46
47namespace advgetopt
48{
49
50
51constexpr char const CONFIGURATION_SECTIONS[] = "configuration_sections";
52
53
54constexpr flag_t SYSTEM_OPTION_NONE = 0x0000;
55
56// system commands
57constexpr flag_t SYSTEM_OPTION_HELP = 0x0001;
58constexpr flag_t SYSTEM_OPTION_VERSION = 0x0002;
60constexpr flag_t SYSTEM_OPTION_LICENSE = 0x0008;
67
68// system options
70
73
74
75
76class getopt
77{
78public:
79 typedef std::shared_ptr<getopt> pointer_t;
80
81 getopt(options_environment const & opts);
82 getopt(options_environment const & opts
83 , int argc
84 , char * argv[]);
85
88 bool has_flag(flag_t flag) const;
89 void reset();
90
92 option const * opts
93 , bool ignore_duplicates = false);
95 std::string const & filename
96 , int min_sections
97 , int max_sections
98 , bool ignore_duplicates = false);
99 void add_option(
101 , bool ignore_duplicates = false);
102 void link_aliases();
103 void set_short_name(
104 std::string const & name
105 , short_name_t short_name);
106
107 void finish_parsing(int argc, char * argv[]);
108
109 void parse_program_name(char * argv[]);
110
112 int argc = 0
113 , char * argv[] = nullptr);
114 void process_configuration_file(std::string const & filename);
115
116 std::string get_environment_variable() const;
117 static string_list_t split_environment(std::string const & environment);
119
120 void parse_string(
121 std::string const & str
123 , bool only_environment_variable = false);
124 void parse_arguments(
125 int argc
126 , char * argv[]
128 , bool only_environment_variable = false);
131 , std::string const & value
132 , std::string const & filename
133 , string_list_t const & option_keys
135
136 flag_t process_system_options(std::basic_ostream<char> & out);
137
139 get_options() const;
141 std::string const & name
142 , bool exact_option = false) const;
144 short_name_t name
145 , bool exact_option = false) const;
146 std::string options_to_string(
147 bool include_progname = false
148 , bool keep_defaults = false) const;
149 bool is_defined(std::string const & name) const;
150 std::size_t size(std::string const & name) const;
151 bool has_default(std::string const & name) const;
152 std::string get_default(std::string const & name) const;
153 long get_long(
154 std::string const & name
155 , int idx = 0
156 , long min = std::numeric_limits<long>::min()
157 , long max = std::numeric_limits<long>::max()) const;
158 double get_double(
159 std::string const & name
160 , int idx = 0
161 , double min = std::numeric_limits<double>::min()
162 , double max = std::numeric_limits<double>::max()) const;
163 std::string get_string(
164 std::string const & name
165 , int idx = 0
166 , bool raw = false) const;
167 std::string operator [] (std::string const & name) const;
168 option_info_ref operator [] (std::string const & name);
169
170 std::string get_program_name() const;
171 std::string get_program_fullname() const;
172 std::string get_project_name() const;
173 std::string get_group_name() const;
174 std::string get_group_or_project_name() const;
176 std::string get_environment_variable_name() const;
177 std::string get_options_filename() const;
179 std::string get_configuration_filename(int idx) const;
181 bool exists
182 , bool writable
183 , int argc = 0
184 , char * argv[] = nullptr) const;
185 std::string get_output_filename() const;
186
187 group_description const *
188 find_group(flag_t group) const;
189 std::string usage(flag_t show = GETOPT_FLAG_SHOW_MOST) const;
190 std::string process_help_string(char const * help) const;
191
193
194private:
195 void initialize_parser(options_environment const & opt_env);
198 void show_option_sources(std::basic_ostream<char> & out);
200 void is_parsed() const;
201 static string_list_t find_config_dir(int argc, char * argv[]);
202 static void add_configuration_filename(string_list_t & names, std::string const & add);
204 string_list_t & names
205 , bool writable
206 , int argc
207 , char * argv[]) const;
209 string_list_t & names
210 , bool writable) const;
211
212 void add_options(
214 , int & i
215 , int argc
216 , char ** argv
217 , string_list_t const & option_keys
219 static string_list_t parse_option_map(std::string const & raw_key);
220
221 std::string f_program_fullname = std::string();
222 std::string f_program_name = std::string();
223
228 std::string f_environment_variable = std::string();
230 bool f_parsed = false;
231};
232
233
234
235
236} // namespace advgetopt
237// vim: ts=4 sw=4 et
Class used to parse command line options.
Definition advgetopt.h:77
std::string get_group_name() const
Retrieve the group name if one is defined.
std::string get_string(std::string const &name, int idx=0, bool raw=false) const
Get the content of an option as a string.
std::string options_to_string(bool include_progname=false, bool keep_defaults=false) const
Transform all the defined options back in a string.
string_list_t get_configuration_filenames(bool exists, bool writable, int argc=0, char *argv[]=nullptr) const
Generate a list of configuration filenames.
std::string get_group_or_project_name() const
Retrieve the group or project name.
std::size_t size(std::string const &name) const
Retrieve the number of arguments.
options_environment f_options_environment
Definition advgetopt.h:224
option_info::pointer_t get_alias_destination(option_info::pointer_t opt) const
Return the alias if there is one.
void initialize_parser(options_environment const &opt_env)
Initialize the parser.
void add_option_from_string(option_info::pointer_t opt, std::string const &value, std::string const &filename, string_list_t const &option_keys, option_source_t source=option_source_t::SOURCE_DIRECT)
Add an option with a value defined in a string.
void get_managed_configuration_filenames(string_list_t &names, bool writable, int argc, char *argv[]) const
Generate the list of managed configuration filenames.
void process_configuration_file(std::string const &filename)
Parse one specific configuration file and process the results.
void parse_configuration_files(int argc=0, char *argv[]=nullptr)
This function checks for arguments in configuration files.
void add_options(option_info::pointer_t opt, int &i, int argc, char **argv, string_list_t const &option_keys, option_source_t source=option_source_t::SOURCE_DIRECT)
Read parameters of the current option.
void get_direct_configuration_filenames(string_list_t &names, bool writable) const
Define the list of direct configuration filenames.
option_info::map_by_name_t f_options_by_name
Definition advgetopt.h:225
void parse_string(std::string const &str, option_source_t source=option_source_t::SOURCE_DIRECT, bool only_environment_variable=false)
Parse a string similar to a command line argument.
std::string f_program_name
Definition advgetopt.h:222
bool is_defined(std::string const &name) const
Check whether a parameter is defined.
option_info::pointer_t get_option(std::string const &name, bool exact_option=false) const
Retrieve an option by name.
std::string usage(flag_t show=GETOPT_FLAG_SHOW_MOST) const
Create a string of the command line arguments.
std::string get_environment_variable() const
void add_option(option_info::pointer_t opt, bool ignore_duplicates=false)
Add one option to the advgetopt object.
void parse_environment_variable()
Check for an environment variable.
option_info::map_by_short_name_t f_options_by_short_name
Definition advgetopt.h:226
std::shared_ptr< getopt > pointer_t
Definition advgetopt.h:79
static string_list_t find_config_dir(int argc, char *argv[])
Search for the "--config-dir" option in a set of arguments.
double get_double(std::string const &name, int idx=0, double min=std::numeric_limits< double >::min(), double max=std::numeric_limits< double >::max()) const
This function retrieves an argument as a double value.
std::string get_program_fullname() const
Get the full name of the program.
std::string operator[](std::string const &name) const
Retrieve the value of an argument.
void define_environment_variable_data()
Retrieve the environment variable string.
std::string get_default(std::string const &name) const
Get the default value for this option.
std::string f_program_fullname
Definition advgetopt.h:221
static void add_configuration_filename(string_list_t &names, std::string const &add)
Add one configuration filename to our list.
variables::pointer_t get_variables() const
Retrieve a pointer to the variables defined in the getopt object.
std::string get_environment_variable_name() const
bool has_default(std::string const &name) const
Check whether an option has a default value.
std::string get_options_filename() const
Get the path and filename to options.
size_t get_configuration_filename_size() const
void parse_arguments(int argc, char *argv[], option_source_t source=option_source_t::SOURCE_DIRECT, bool only_environment_variable=false)
Parse an array of arguments.
void parse_options_info(option const *opts, bool ignore_duplicates=false)
Parse the options to option_info objects.
void parse_options_from_file()
Check for a file with option definitions.
void parse_program_name(char *argv[])
Transform the argv[0] parameter in the program name.
std::string f_environment_variable
Definition advgetopt.h:228
std::string get_program_name() const
Get the basename of the program.
void is_parsed() const
Verify that the parser is done.
options_environment const & get_options_environment() const
Return a reference to the options environment.
void reset()
Reset all the options.
std::string get_project_name() const
Retrieve the project name if one is defined.
flag_t process_system_options(std::basic_ostream< char > &out)
Process the system options.
bool has_flag(flag_t flag) const
Check whether an environment flag is set or not.
void link_aliases()
Link options marked as a GETOPT_FLAG_ALIAS.
static string_list_t split_environment(std::string const &environment)
Transform a string in an array of arguments.
group_description const * find_group(flag_t group) const
Search for group in the list of group names.
long get_long(std::string const &name, int idx=0, long min=std::numeric_limits< long >::min(), long max=std::numeric_limits< long >::max()) const
This function retrieves an argument as a long value.
std::string get_output_filename() const
Determine the best suited file for updates.
std::string get_configuration_filename(int idx) const
option_info::map_by_name_t const & get_options() const
Retrieve the complete list of options.
void show_option_sources(std::basic_ostream< char > &out)
Output the source of each option.
void finish_parsing(int argc, char *argv[])
Actually parse everything.
void parse_options_from_group_names()
Transform group names in –<name>-help commands.
variables::pointer_t f_variables
Definition advgetopt.h:229
option_info::pointer_t f_default_option
Definition advgetopt.h:227
void set_short_name(std::string const &name, short_name_t short_name)
Assign a short name to an option.
std::string process_help_string(char const *help) const
Change the % flags in help strings.
static string_list_t parse_option_map(std::string const &raw_key)
Parse a map following an option name.
std::map< std::string, pointer_t > map_by_name_t
Definition option_info.h:80
std::shared_ptr< option_info > pointer_t
Definition option_info.h:78
std::map< short_name_t, pointer_t > map_by_short_name_t
Definition option_info.h:81
std::shared_ptr< variables > pointer_t
Definition variables.h:61
The advgetopt environment to parse command line options.
constexpr flag_t SYSTEM_OPTION_HELP
Definition advgetopt.h:57
constexpr flag_t SYSTEM_OPTION_BUILD_DATE
Definition advgetopt.h:61
std::uint32_t flag_t
Definition flags.h:41
char32_t short_name_t
Definition option_info.h:49
constexpr flag_t SYSTEM_OPTION_OPTIONS_MASK
Definition advgetopt.h:72
constexpr flag_t SYSTEM_OPTION_SHOW_OPTION_VALUE
Definition advgetopt.h:66
constexpr flag_t SYSTEM_OPTION_NONE
Definition advgetopt.h:54
constexpr flag_t SYSTEM_OPTION_VERSION
Definition advgetopt.h:58
constexpr flag_t SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS
Definition advgetopt.h:64
constexpr flag_t SYSTEM_OPTION_COMMANDS_MASK
Definition advgetopt.h:71
constexpr flag_t SYSTEM_OPTION_LICENSE
Definition advgetopt.h:60
constexpr flag_t SYSTEM_OPTION_CONFIGURATION_FILENAMES
Definition advgetopt.h:63
constexpr flag_t SYSTEM_OPTION_CONFIG_DIR
Definition advgetopt.h:69
static constexpr flag_t GETOPT_FLAG_SHOW_MOST
Definition flags.h:59
constexpr flag_t SYSTEM_OPTION_COPYRIGHT
Definition advgetopt.h:59
constexpr flag_t SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME
Definition advgetopt.h:62
std::vector< std::string > string_list_t
Definition utils.h:41
constexpr char const CONFIGURATION_SECTIONS[]
Definition advgetopt.h:51
constexpr flag_t SYSTEM_OPTION_SHOW_OPTION_SOURCES
Definition advgetopt.h:65
Declaration of the option_info class used to record available options.
Definitions of the options class a initialization functions.
Structure representing an option.
Definition options.h:70
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.