Line data Source code
1 : /*
2 : * License:
3 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
4 : *
5 : * https://snapwebsites.org/
6 : * contact@m2osw.com
7 : *
8 : * This program is free software; you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation; either version 2 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License along
19 : * with this program; if not, write to the Free Software Foundation, Inc.,
20 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 : *
22 : * Authors:
23 : * Alexis Wilke alexis@m2osw.com
24 : * Doug Barbieri doug@m2osw.com
25 : */
26 : #pragma once
27 :
28 : /** \file
29 : * \brief Definitions of the advanced getopt class.
30 : *
31 : * The advgetopt library offers an advanced way to manage your command
32 : * line tools options on the command line, in environment variables, and
33 : * in configuration files.
34 : */
35 :
36 : // advgetopt lib
37 : //
38 : #include "advgetopt/option_info.h"
39 : #include "advgetopt/options.h"
40 : #include "advgetopt/validator.h"
41 :
42 :
43 : // C++ lib
44 : //
45 : #include <limits>
46 : #include <map>
47 : #include <memory>
48 : #include <ostream>
49 : #include <vector>
50 :
51 :
52 :
53 : namespace advgetopt
54 : {
55 :
56 :
57 : constexpr char const CONFIGURATION_SECTIONS[] = "configuration_sections";
58 :
59 :
60 : constexpr flag_t SYSTEM_OPTION_NONE = 0x0000;
61 :
62 : constexpr flag_t SYSTEM_OPTION_HELP = 0x0001;
63 : constexpr flag_t SYSTEM_OPTION_VERSION = 0x0002;
64 : constexpr flag_t SYSTEM_OPTION_COPYRIGHT = 0x0004;
65 : constexpr flag_t SYSTEM_OPTION_LICENSE = 0x0008;
66 : constexpr flag_t SYSTEM_OPTION_BUILD_DATE = 0x0010;
67 : constexpr flag_t SYSTEM_OPTION_ENVIRONMENT_VARIABLE_NAME = 0x0020;
68 : constexpr flag_t SYSTEM_OPTION_CONFIGURATION_FILENAMES = 0x0040;
69 : constexpr flag_t SYSTEM_OPTION_PATH_TO_OPTION_DEFINITIONS = 0x0080;
70 : constexpr flag_t SYSTEM_OPTION_CONFIG_DIR = 0x0100; // option
71 :
72 : constexpr flag_t SYSTEM_OPTION_COMMANDS_MASK = 0x00FF;
73 : constexpr flag_t SYSTEM_OPTION_OPTIONS_MASK = 0x0100;
74 :
75 :
76 :
77 305 : class getopt
78 : {
79 : public:
80 : typedef std::shared_ptr<getopt> pointer_t;
81 :
82 : getopt(options_environment const & opts);
83 : getopt(options_environment const & opts
84 : , int argc
85 : , char * argv[]);
86 :
87 : options_environment const &
88 : get_options_environment() const;
89 : bool has_flag(flag_t flag) const;
90 : void reset();
91 :
92 : void parse_options_info(
93 : option const * opts
94 : , bool ignore_duplicates = false);
95 : void link_aliases();
96 : void set_short_name(
97 : std::string const & name
98 : , short_name_t short_name);
99 :
100 : void finish_parsing(int argc, char * argv[]);
101 :
102 : void parse_program_name(char * argv[]);
103 :
104 : void parse_configuration_files();
105 : void process_configuration_file(std::string const & filename);
106 :
107 : void parse_environment_variable();
108 :
109 : void parse_string(
110 : std::string const & str
111 : , bool only_environment_variable);
112 : void parse_arguments(
113 : int argc
114 : , char * argv[]
115 : , bool only_environment_variable = false);
116 :
117 : flag_t process_system_options(std::basic_ostream<char> & out);
118 :
119 : option_info::map_by_name_t const &
120 : get_options() const;
121 : option_info::pointer_t get_option(std::string const & name, bool exact_option = false) const;
122 : option_info::pointer_t get_option(short_name_t name, bool exact_option = false) const;
123 : bool is_defined(std::string const & name) const;
124 : size_t size(std::string const & name) const;
125 : bool has_default(std::string const & name) const;
126 : std::string get_default(std::string const & name) const;
127 : long get_long(
128 : std::string const & name
129 : , int idx = 0
130 : , long min = std::numeric_limits<long>::min()
131 : , long max = std::numeric_limits<long>::max());
132 : std::string get_string(std::string const & name, int idx = 0) const;
133 : std::string operator [] (std::string const & name) const;
134 : option_info_ref operator [] (std::string const & name);
135 :
136 : std::string get_program_name() const;
137 : std::string get_program_fullname() const;
138 : std::string get_project_name() const;
139 : std::string get_environment_variable_name() const;
140 : size_t get_configuration_filename_size() const;
141 : std::string get_configuration_filename(int idx) const;
142 : string_list_t get_configuration_filenames(bool exists
143 : , bool writable) const;
144 :
145 : group_description const *
146 : find_group(flag_t group) const;
147 : std::string usage(flag_t show = GETOPT_FLAG_SHOW_MOST) const;
148 : std::string process_help_string(char const * help) const;
149 : static std::string breakup_line(
150 : std::string line
151 : , size_t const option_width
152 : , size_t const line_width);
153 : static std::string format_usage_string(
154 : std::string const & argument
155 : , std::string const & help
156 : , size_t const option_width
157 : , size_t const line_width);
158 : static size_t get_line_width();
159 :
160 : private:
161 : void initialize_parser(options_environment const & opt_env);
162 : void parse_options_from_group_names();
163 : void parse_options_from_file();
164 : option_info::pointer_t get_alias_destination(option_info::pointer_t opt) const;
165 :
166 : void add_options(option_info::pointer_t opt
167 : , int & i
168 : , int argc
169 : , char ** argv);
170 : void add_option_from_string(option_info::pointer_t opt
171 : , std::string const & value
172 : , std::string const & filename);
173 :
174 : std::string f_program_fullname = std::string();
175 : std::string f_program_name = std::string();
176 :
177 : options_environment f_options_environment = options_environment();
178 : option_info::map_by_name_t f_options_by_name = option_info::map_by_name_t();
179 : option_info::map_by_short_name_t f_options_by_short_name = option_info::map_by_short_name_t();
180 : option_info::pointer_t f_default_option = option_info::pointer_t();
181 : };
182 :
183 :
184 :
185 :
186 : } // namespace advgetopt
187 : // vim: ts=4 sw=4 et
|