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