Line data Source code
1 : // Copyright (c) 2006-2021 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 Definitions of the advanced getopt class.
23 : *
24 : * The advgetopt library offers an advanced way to manage your command
25 : * line tools options on the command line, in environment variables, and
26 : * in configuration files.
27 : */
28 :
29 : // advgetopt lib
30 : //
31 : #include "advgetopt/option_info.h"
32 : #include "advgetopt/options.h"
33 : #include "advgetopt/validator.h"
34 :
35 :
36 : // C++ lib
37 : //
38 : #include <limits>
39 : #include <map>
40 : #include <memory>
41 : #include <ostream>
42 : #include <vector>
43 :
44 :
45 :
46 : namespace advgetopt
47 : {
48 :
49 :
50 : constexpr char const CONFIGURATION_SECTIONS[] = "configuration_sections";
51 :
52 :
53 : constexpr flag_t SYSTEM_OPTION_NONE = 0x0000;
54 :
55 : constexpr flag_t SYSTEM_OPTION_HELP = 0x0001;
56 : constexpr flag_t SYSTEM_OPTION_VERSION = 0x0002;
57 : constexpr flag_t SYSTEM_OPTION_COPYRIGHT = 0x0004;
58 : constexpr flag_t SYSTEM_OPTION_LICENSE = 0x0008;
59 : constexpr flag_t SYSTEM_OPTION_BUILD_DATE = 0x0010;
60 : constexpr flag_t SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME = 0x0020;
61 : constexpr flag_t SYSTEM_OPTION_CONFIGURATION_FILENAMES = 0x0040;
62 : constexpr flag_t SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS = 0x0080;
63 : constexpr flag_t SYSTEM_OPTION_SHOW_OPTION_SOURCES = 0x0100;
64 :
65 : constexpr flag_t SYSTEM_OPTION_CONFIG_DIR = 0x1000; // option
66 :
67 : constexpr flag_t SYSTEM_OPTION_COMMANDS_MASK = 0x0FFF;
68 : constexpr flag_t SYSTEM_OPTION_OPTIONS_MASK = 0xF000;
69 :
70 :
71 :
72 315 : class getopt
73 : {
74 : public:
75 : typedef std::shared_ptr<getopt> pointer_t;
76 :
77 : getopt(options_environment const & opts);
78 : getopt(options_environment const & opts
79 : , int argc
80 : , char * argv[]);
81 :
82 : options_environment const &
83 : get_options_environment() const;
84 : bool has_flag(flag_t flag) const;
85 : void reset();
86 :
87 : void parse_options_info(
88 : option const * opts
89 : , bool ignore_duplicates = false);
90 : void link_aliases();
91 : void set_short_name(
92 : std::string const & name
93 : , short_name_t short_name);
94 :
95 : void finish_parsing(int argc, char * argv[]);
96 :
97 : void parse_program_name(char * argv[]);
98 :
99 : void parse_configuration_files(
100 : int argc = 0
101 : , char * argv[] = nullptr);
102 : void process_configuration_file(std::string const & filename);
103 :
104 : std::string get_environment_variable() const;
105 : static string_list_t split_environment(std::string const & environment);
106 : void parse_environment_variable();
107 :
108 : void parse_string(
109 : std::string const & str
110 : , option_source_t source = option_source_t::SOURCE_DIRECT
111 : , bool only_environment_variable = false);
112 : void parse_arguments(
113 : int argc
114 : , char * argv[]
115 : , option_source_t source = option_source_t::SOURCE_DIRECT
116 : , bool only_environment_variable = false);
117 :
118 : flag_t process_system_options(std::basic_ostream<char> & out);
119 :
120 : option_info::map_by_name_t const &
121 : get_options() const;
122 : option_info::pointer_t get_option(std::string const & name, bool exact_option = false) const;
123 : option_info::pointer_t get_option(short_name_t name, bool exact_option = false) const;
124 : bool is_defined(std::string const & name) const;
125 : std::size_t size(std::string const & name) const;
126 : bool has_default(std::string const & name) const;
127 : std::string get_default(std::string const & name) const;
128 : long get_long(
129 : std::string const & name
130 : , int idx = 0
131 : , long min = std::numeric_limits<long>::min()
132 : , long max = std::numeric_limits<long>::max()) const;
133 : std::string get_string(std::string const & name, int idx = 0) const;
134 : std::string operator [] (std::string const & name) const;
135 : option_info_ref operator [] (std::string const & name);
136 :
137 : std::string get_program_name() const;
138 : std::string get_program_fullname() const;
139 : std::string get_project_name() const;
140 : std::string get_group_name() const;
141 : std::string get_environment_variable_name() const;
142 : size_t get_configuration_filename_size() const;
143 : std::string get_configuration_filename(int idx) const;
144 : string_list_t get_configuration_filenames(
145 : bool exists
146 : , bool writable
147 : , int argc = 0
148 : , char * argv[] = nullptr) const;
149 :
150 : group_description const *
151 : find_group(flag_t group) const;
152 : std::string usage(flag_t show = GETOPT_FLAG_SHOW_MOST) const;
153 : std::string process_help_string(char const * help) const;
154 : static std::string breakup_line(
155 : std::string line
156 : , size_t const option_width
157 : , size_t const line_width);
158 : static std::string format_usage_string(
159 : std::string const & argument
160 : , std::string const & help
161 : , size_t const option_width
162 : , size_t const line_width);
163 : static size_t get_line_width();
164 :
165 : private:
166 : void initialize_parser(options_environment const & opt_env);
167 : void parse_options_from_group_names();
168 : void parse_options_from_file();
169 : void show_option_sources(std::basic_ostream<char> & out);
170 : option_info::pointer_t get_alias_destination(option_info::pointer_t opt) const;
171 : void define_environment_variable_data();
172 : void is_parsed() const;
173 : static string_list_t find_config_dir(int argc, char * argv[]);
174 :
175 : void add_options(
176 : option_info::pointer_t opt
177 : , int & i
178 : , int argc
179 : , char ** argv
180 : , option_source_t source = option_source_t::SOURCE_DIRECT);
181 : void add_option_from_string(
182 : option_info::pointer_t opt
183 : , std::string const & value
184 : , std::string const & filename
185 : , option_source_t source = option_source_t::SOURCE_DIRECT);
186 :
187 : std::string f_program_fullname = std::string();
188 : std::string f_program_name = std::string();
189 :
190 : options_environment f_options_environment = options_environment();
191 : option_info::map_by_name_t f_options_by_name = option_info::map_by_name_t();
192 : option_info::map_by_short_name_t f_options_by_short_name = option_info::map_by_short_name_t();
193 : option_info::pointer_t f_default_option = option_info::pointer_t();
194 : std::string f_environment_variable = std::string();
195 : bool f_parsed = false;
196 : };
197 :
198 :
199 :
200 :
201 : } // namespace advgetopt
202 : // vim: ts=4 sw=4 et
|