Chris@16
|
1 // Copyright Vladimir Prus 2002-2004.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 // (See accompanying file LICENSE_1_0.txt
|
Chris@16
|
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6
|
Chris@16
|
7 #ifndef BOOST_PARSERS_VP_2003_05_19
|
Chris@16
|
8 #define BOOST_PARSERS_VP_2003_05_19
|
Chris@16
|
9
|
Chris@16
|
10 #include <boost/program_options/config.hpp>
|
Chris@16
|
11 #include <boost/program_options/option.hpp>
|
Chris@16
|
12 #include <boost/program_options/detail/cmdline.hpp>
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/function/function1.hpp>
|
Chris@16
|
15
|
Chris@16
|
16 #include <iosfwd>
|
Chris@16
|
17 #include <vector>
|
Chris@16
|
18 #include <utility>
|
Chris@16
|
19
|
Chris@16
|
20 #if defined(BOOST_MSVC)
|
Chris@16
|
21 # pragma warning (push)
|
Chris@16
|
22 # pragma warning (disable:4251) // class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class 'boost::program_options::basic_parsed_options<wchar_t>'
|
Chris@16
|
23 #endif
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost { namespace program_options {
|
Chris@16
|
26
|
Chris@16
|
27 class options_description;
|
Chris@16
|
28 class positional_options_description;
|
Chris@16
|
29
|
Chris@16
|
30
|
Chris@16
|
31 /** Results of parsing an input source.
|
Chris@16
|
32 The primary use of this class is passing information from parsers
|
Chris@16
|
33 component to value storage component. This class does not makes
|
Chris@16
|
34 much sense itself.
|
Chris@16
|
35 */
|
Chris@16
|
36 template<class charT>
|
Chris@16
|
37 class basic_parsed_options {
|
Chris@16
|
38 public:
|
Chris@16
|
39 explicit basic_parsed_options(const options_description* xdescription, int options_prefix = 0)
|
Chris@16
|
40 : description(xdescription), m_options_prefix(options_prefix) {}
|
Chris@16
|
41 /** Options found in the source. */
|
Chris@16
|
42 std::vector< basic_option<charT> > options;
|
Chris@16
|
43 /** Options description that was used for parsing.
|
Chris@16
|
44 Parsers should return pointer to the instance of
|
Chris@16
|
45 option_description passed to them, and issues of lifetime are
|
Chris@16
|
46 up to the caller. Can be NULL.
|
Chris@16
|
47 */
|
Chris@16
|
48 const options_description* description;
|
Chris@16
|
49
|
Chris@16
|
50 /** Mainly used for the diagnostic messages in exceptions.
|
Chris@16
|
51 * The canonical option prefix for the parser which generated these results,
|
Chris@16
|
52 * depending on the settings for basic_command_line_parser::style() or
|
Chris@16
|
53 * cmdline::style(). In order of precedence of command_line_style enums:
|
Chris@16
|
54 * allow_long
|
Chris@16
|
55 * allow_long_disguise
|
Chris@16
|
56 * allow_dash_for_short
|
Chris@16
|
57 * allow_slash_for_short
|
Chris@16
|
58 */
|
Chris@16
|
59 int m_options_prefix;
|
Chris@16
|
60 };
|
Chris@16
|
61
|
Chris@16
|
62 /** Specialization of basic_parsed_options which:
|
Chris@16
|
63 - provides convenient conversion from basic_parsed_options<char>
|
Chris@16
|
64 - stores the passed char-based options for later use.
|
Chris@16
|
65 */
|
Chris@16
|
66 template<>
|
Chris@16
|
67 class BOOST_PROGRAM_OPTIONS_DECL basic_parsed_options<wchar_t> {
|
Chris@16
|
68 public:
|
Chris@16
|
69 /** Constructs wrapped options from options in UTF8 encoding. */
|
Chris@16
|
70 explicit basic_parsed_options(const basic_parsed_options<char>& po);
|
Chris@16
|
71
|
Chris@16
|
72 std::vector< basic_option<wchar_t> > options;
|
Chris@16
|
73 const options_description* description;
|
Chris@16
|
74
|
Chris@16
|
75 /** Stores UTF8 encoded options that were passed to constructor,
|
Chris@16
|
76 to avoid reverse conversion in some cases. */
|
Chris@16
|
77 basic_parsed_options<char> utf8_encoded_options;
|
Chris@16
|
78
|
Chris@16
|
79 /** Mainly used for the diagnostic messages in exceptions.
|
Chris@16
|
80 * The canonical option prefix for the parser which generated these results,
|
Chris@16
|
81 * depending on the settings for basic_command_line_parser::style() or
|
Chris@16
|
82 * cmdline::style(). In order of precedence of command_line_style enums:
|
Chris@16
|
83 * allow_long
|
Chris@16
|
84 * allow_long_disguise
|
Chris@16
|
85 * allow_dash_for_short
|
Chris@16
|
86 * allow_slash_for_short
|
Chris@16
|
87 */
|
Chris@16
|
88 int m_options_prefix;
|
Chris@16
|
89 };
|
Chris@16
|
90
|
Chris@16
|
91 typedef basic_parsed_options<char> parsed_options;
|
Chris@16
|
92 typedef basic_parsed_options<wchar_t> wparsed_options;
|
Chris@16
|
93
|
Chris@16
|
94 /** Augments basic_parsed_options<wchar_t> with conversion from
|
Chris@16
|
95 'parsed_options' */
|
Chris@16
|
96
|
Chris@16
|
97
|
Chris@16
|
98 typedef function1<std::pair<std::string, std::string>, const std::string&> ext_parser;
|
Chris@16
|
99
|
Chris@16
|
100 /** Command line parser.
|
Chris@16
|
101
|
Chris@16
|
102 The class allows one to specify all the information needed for parsing
|
Chris@16
|
103 and to parse the command line. It is primarily needed to
|
Chris@16
|
104 emulate named function parameters -- a regular function with 5
|
Chris@16
|
105 parameters will be hard to use and creating overloads with a smaller
|
Chris@16
|
106 nuber of parameters will be confusing.
|
Chris@16
|
107
|
Chris@16
|
108 For the most common case, the function parse_command_line is a better
|
Chris@16
|
109 alternative.
|
Chris@16
|
110
|
Chris@16
|
111 There are two typedefs -- command_line_parser and wcommand_line_parser,
|
Chris@16
|
112 for charT == char and charT == wchar_t cases.
|
Chris@16
|
113 */
|
Chris@16
|
114 template<class charT>
|
Chris@16
|
115 class basic_command_line_parser : private detail::cmdline {
|
Chris@16
|
116 public:
|
Chris@16
|
117 /** Creates a command line parser for the specified arguments
|
Chris@16
|
118 list. The 'args' parameter should not include program name.
|
Chris@16
|
119 */
|
Chris@16
|
120 basic_command_line_parser(const std::vector<
|
Chris@16
|
121 std::basic_string<charT> >& args);
|
Chris@16
|
122 /** Creates a command line parser for the specified arguments
|
Chris@16
|
123 list. The parameters should be the same as passed to 'main'.
|
Chris@16
|
124 */
|
Chris@16
|
125 basic_command_line_parser(int argc, const charT* const argv[]);
|
Chris@16
|
126
|
Chris@16
|
127 /** Sets options descriptions to use. */
|
Chris@16
|
128 basic_command_line_parser& options(const options_description& desc);
|
Chris@16
|
129 /** Sets positional options description to use. */
|
Chris@16
|
130 basic_command_line_parser& positional(
|
Chris@16
|
131 const positional_options_description& desc);
|
Chris@16
|
132
|
Chris@16
|
133 /** Sets the command line style. */
|
Chris@16
|
134 basic_command_line_parser& style(int);
|
Chris@16
|
135 /** Sets the extra parsers. */
|
Chris@16
|
136 basic_command_line_parser& extra_parser(ext_parser);
|
Chris@16
|
137
|
Chris@16
|
138 /** Parses the options and returns the result of parsing.
|
Chris@16
|
139 Throws on error.
|
Chris@16
|
140 */
|
Chris@16
|
141 basic_parsed_options<charT> run();
|
Chris@16
|
142
|
Chris@16
|
143 /** Specifies that unregistered options are allowed and should
|
Chris@16
|
144 be passed though. For each command like token that looks
|
Chris@16
|
145 like an option but does not contain a recognized name, an
|
Chris@16
|
146 instance of basic_option<charT> will be added to result,
|
Chris@16
|
147 with 'unrecognized' field set to 'true'. It's possible to
|
Chris@16
|
148 collect all unrecognized options with the 'collect_unrecognized'
|
Chris@16
|
149 funciton.
|
Chris@16
|
150 */
|
Chris@16
|
151 basic_command_line_parser& allow_unregistered();
|
Chris@16
|
152
|
Chris@16
|
153 using detail::cmdline::style_parser;
|
Chris@16
|
154
|
Chris@16
|
155 basic_command_line_parser& extra_style_parser(style_parser s);
|
Chris@16
|
156
|
Chris@16
|
157 private:
|
Chris@16
|
158 const options_description* m_desc;
|
Chris@16
|
159 };
|
Chris@16
|
160
|
Chris@16
|
161 typedef basic_command_line_parser<char> command_line_parser;
|
Chris@16
|
162 typedef basic_command_line_parser<wchar_t> wcommand_line_parser;
|
Chris@16
|
163
|
Chris@16
|
164 /** Creates instance of 'command_line_parser', passes parameters to it,
|
Chris@16
|
165 and returns the result of calling the 'run' method.
|
Chris@16
|
166 */
|
Chris@16
|
167 template<class charT>
|
Chris@16
|
168 basic_parsed_options<charT>
|
Chris@16
|
169 parse_command_line(int argc, const charT* const argv[],
|
Chris@16
|
170 const options_description&,
|
Chris@16
|
171 int style = 0,
|
Chris@16
|
172 function1<std::pair<std::string, std::string>,
|
Chris@16
|
173 const std::string&> ext
|
Chris@16
|
174 = ext_parser());
|
Chris@16
|
175
|
Chris@16
|
176 /** Parse a config file.
|
Chris@16
|
177
|
Chris@16
|
178 Read from given stream.
|
Chris@16
|
179 */
|
Chris@16
|
180 template<class charT>
|
Chris@16
|
181 #if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
|
Chris@16
|
182 BOOST_PROGRAM_OPTIONS_DECL
|
Chris@16
|
183 #endif
|
Chris@16
|
184 basic_parsed_options<charT>
|
Chris@16
|
185 parse_config_file(std::basic_istream<charT>&, const options_description&,
|
Chris@16
|
186 bool allow_unregistered = false);
|
Chris@16
|
187
|
Chris@16
|
188 /** Parse a config file.
|
Chris@16
|
189
|
Chris@16
|
190 Read from file with the given name. The character type is
|
Chris@16
|
191 passed to the file stream.
|
Chris@16
|
192 */
|
Chris@16
|
193 template<class charT>
|
Chris@16
|
194 #if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
|
Chris@16
|
195 BOOST_PROGRAM_OPTIONS_DECL
|
Chris@16
|
196 #endif
|
Chris@16
|
197 basic_parsed_options<charT>
|
Chris@16
|
198 parse_config_file(const char* filename, const options_description&,
|
Chris@16
|
199 bool allow_unregistered = false);
|
Chris@16
|
200
|
Chris@16
|
201 /** Controls if the 'collect_unregistered' function should
|
Chris@16
|
202 include positional options, or not. */
|
Chris@16
|
203 enum collect_unrecognized_mode
|
Chris@16
|
204 { include_positional, exclude_positional };
|
Chris@16
|
205
|
Chris@16
|
206 /** Collects the original tokens for all named options with
|
Chris@16
|
207 'unregistered' flag set. If 'mode' is 'include_positional'
|
Chris@16
|
208 also collects all positional options.
|
Chris@16
|
209 Returns the vector of origianl tokens for all collected
|
Chris@16
|
210 options.
|
Chris@16
|
211 */
|
Chris@16
|
212 template<class charT>
|
Chris@16
|
213 std::vector< std::basic_string<charT> >
|
Chris@16
|
214 collect_unrecognized(const std::vector< basic_option<charT> >& options,
|
Chris@16
|
215 enum collect_unrecognized_mode mode);
|
Chris@16
|
216
|
Chris@16
|
217 /** Parse environment.
|
Chris@16
|
218
|
Chris@16
|
219 For each environment variable, the 'name_mapper' function is called to
|
Chris@16
|
220 obtain the option name. If it returns empty string, the variable is
|
Chris@16
|
221 ignored.
|
Chris@16
|
222
|
Chris@16
|
223 This is done since naming of environment variables is typically
|
Chris@16
|
224 different from the naming of command line options.
|
Chris@16
|
225 */
|
Chris@16
|
226 BOOST_PROGRAM_OPTIONS_DECL parsed_options
|
Chris@16
|
227 parse_environment(const options_description&,
|
Chris@16
|
228 const function1<std::string, std::string>& name_mapper);
|
Chris@16
|
229
|
Chris@16
|
230 /** Parse environment.
|
Chris@16
|
231
|
Chris@16
|
232 Takes all environment variables which start with 'prefix'. The option
|
Chris@16
|
233 name is obtained from variable name by removing the prefix and
|
Chris@16
|
234 converting the remaining string into lower case.
|
Chris@16
|
235 */
|
Chris@16
|
236 BOOST_PROGRAM_OPTIONS_DECL parsed_options
|
Chris@16
|
237 parse_environment(const options_description&, const std::string& prefix);
|
Chris@16
|
238
|
Chris@16
|
239 /** @overload
|
Chris@16
|
240 This function exists to resolve ambiguity between the two above
|
Chris@16
|
241 functions when second argument is of 'char*' type. There's implicit
|
Chris@16
|
242 conversion to both function1 and string.
|
Chris@16
|
243 */
|
Chris@16
|
244 BOOST_PROGRAM_OPTIONS_DECL parsed_options
|
Chris@16
|
245 parse_environment(const options_description&, const char* prefix);
|
Chris@16
|
246
|
Chris@16
|
247 /** Splits a given string to a collection of single strings which
|
Chris@16
|
248 can be passed to command_line_parser. The second parameter is
|
Chris@16
|
249 used to specify a collection of possible seperator chars used
|
Chris@16
|
250 for splitting. The seperator is defaulted to space " ".
|
Chris@16
|
251 Splitting is done in a unix style way, with respect to quotes '"'
|
Chris@16
|
252 and escape characters '\'
|
Chris@16
|
253 */
|
Chris@16
|
254 BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
|
Chris@16
|
255 split_unix(const std::string& cmdline, const std::string& seperator = " \t",
|
Chris@16
|
256 const std::string& quote = "'\"", const std::string& escape = "\\");
|
Chris@16
|
257
|
Chris@16
|
258 #ifndef BOOST_NO_STD_WSTRING
|
Chris@16
|
259 /** @overload */
|
Chris@16
|
260 BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
|
Chris@16
|
261 split_unix(const std::wstring& cmdline, const std::wstring& seperator = L" \t",
|
Chris@16
|
262 const std::wstring& quote = L"'\"", const std::wstring& escape = L"\\");
|
Chris@16
|
263 #endif
|
Chris@16
|
264
|
Chris@16
|
265 #ifdef _WIN32
|
Chris@16
|
266 /** Parses the char* string which is passed to WinMain function on
|
Chris@16
|
267 windows. This function is provided for convenience, and because it's
|
Chris@16
|
268 not clear how to portably access split command line string from
|
Chris@16
|
269 runtime library and if it always exists.
|
Chris@16
|
270 This function is available only on Windows.
|
Chris@16
|
271 */
|
Chris@16
|
272 BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
|
Chris@16
|
273 split_winmain(const std::string& cmdline);
|
Chris@16
|
274
|
Chris@16
|
275 #ifndef BOOST_NO_STD_WSTRING
|
Chris@16
|
276 /** @overload */
|
Chris@16
|
277 BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
|
Chris@16
|
278 split_winmain(const std::wstring& cmdline);
|
Chris@16
|
279 #endif
|
Chris@16
|
280 #endif
|
Chris@16
|
281
|
Chris@16
|
282
|
Chris@16
|
283 }}
|
Chris@16
|
284
|
Chris@16
|
285 #if defined(BOOST_MSVC)
|
Chris@16
|
286 # pragma warning (pop)
|
Chris@16
|
287 #endif
|
Chris@16
|
288
|
Chris@16
|
289 #undef DECL
|
Chris@16
|
290
|
Chris@16
|
291 #include "boost/program_options/detail/parsers.hpp"
|
Chris@16
|
292
|
Chris@16
|
293 #endif
|