1 // Copyright Vladimir Prus 2002-2004.
\r
2 // Distributed under the Boost Software License, Version 1.0.
\r
3 // (See accompanying file LICENSE_1_0.txt
\r
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
7 #ifndef BOOST_PARSERS_VP_2003_05_19
\r
8 #define BOOST_PARSERS_VP_2003_05_19
\r
10 #include <boost/program_options/config.hpp>
\r
11 #include <boost/program_options/option.hpp>
\r
12 #include <boost/program_options/detail/cmdline.hpp>
\r
14 #include <boost/function/function1.hpp>
\r
20 namespace boost { namespace program_options {
\r
22 class options_description;
\r
23 class positional_options_description;
\r
26 /** Results of parsing an input source.
\r
27 The primary use of this class is passing information from parsers
\r
28 component to value storage component. This class does not makes
\r
31 template<class charT>
\r
32 class basic_parsed_options {
\r
34 explicit basic_parsed_options(const options_description* description)
\r
35 : description(description) {}
\r
36 /** Options found in the source. */
\r
37 std::vector< basic_option<charT> > options;
\r
38 /** Options description that was used for parsing.
\r
39 Parsers should return pointer to the instance of
\r
40 option_description passed to them, and issues of lifetime are
\r
41 up to the caller. Can be NULL.
\r
43 const options_description* description;
\r
46 /** Specialization of basic_parsed_options which:
\r
47 - provides convenient conversion from basic_parsed_options<char>
\r
48 - stores the passed char-based options for later use.
\r
51 class BOOST_PROGRAM_OPTIONS_DECL basic_parsed_options<wchar_t> {
\r
53 /** Constructs wrapped options from options in UTF8 encoding. */
\r
54 explicit basic_parsed_options(const basic_parsed_options<char>& po);
\r
56 std::vector< basic_option<wchar_t> > options;
\r
57 const options_description* description;
\r
59 /** Stores UTF8 encoded options that were passed to constructor,
\r
60 to avoid reverse conversion in some cases. */
\r
61 basic_parsed_options<char> utf8_encoded_options;
\r
64 typedef basic_parsed_options<char> parsed_options;
\r
65 typedef basic_parsed_options<wchar_t> wparsed_options;
\r
67 /** Augments basic_parsed_options<wchar_t> with conversion from
\r
71 typedef function1<std::pair<std::string, std::string>, const std::string&> ext_parser;
\r
73 /** Command line parser.
\r
75 The class allows one to specify all the information needed for parsing
\r
76 and to parse the command line. It is primarily needed to
\r
77 emulate named function parameters -- a regular function with 5
\r
78 parameters will be hard to use and creating overloads with a smaller
\r
79 nuber of parameters will be confusing.
\r
81 For the most common case, the function parse_command_line is a better
\r
84 There are two typedefs -- command_line_parser and wcommand_line_parser,
\r
85 for charT == char and charT == wchar_t cases.
\r
87 template<class charT>
\r
88 class basic_command_line_parser : private detail::cmdline {
\r
90 /** Creates a command line parser for the specified arguments
\r
91 list. The 'args' parameter should not include program name.
\r
93 basic_command_line_parser(const std::vector<
\r
94 std::basic_string<charT> >& args);
\r
95 /** Creates a command line parser for the specified arguments
\r
96 list. The parameters should be the same as passed to 'main'.
\r
98 basic_command_line_parser(int argc, charT* argv[]);
\r
100 /** Sets options descriptions to use. */
\r
101 basic_command_line_parser& options(const options_description& desc);
\r
102 /** Sets positional options description to use. */
\r
103 basic_command_line_parser& positional(
\r
104 const positional_options_description& desc);
\r
106 /** Sets the command line style. */
\r
107 basic_command_line_parser& style(int);
\r
108 /** Sets the extra parsers. */
\r
109 basic_command_line_parser& extra_parser(ext_parser);
\r
111 /** Parses the options and returns the result of parsing.
\r
114 basic_parsed_options<charT> run();
\r
116 /** Specifies that unregistered options are allowed and should
\r
117 be passed though. For each command like token that looks
\r
118 like an option but does not contain a recognized name, an
\r
119 instance of basic_option<charT> will be added to result,
\r
120 with 'unrecognized' field set to 'true'. It's possible to
\r
121 collect all unrecognized options with the 'collect_unrecognized'
\r
124 basic_command_line_parser& allow_unregistered();
\r
126 using detail::cmdline::style_parser;
\r
128 basic_command_line_parser& extra_style_parser(style_parser s);
\r
131 const options_description* m_desc;
\r
134 typedef basic_command_line_parser<char> command_line_parser;
\r
135 typedef basic_command_line_parser<wchar_t> wcommand_line_parser;
\r
137 /** Creates instance of 'command_line_parser', passes parameters to it,
\r
138 and returns the result of calling the 'run' method.
\r
140 template<class charT>
\r
141 basic_parsed_options<charT>
\r
142 parse_command_line(int argc, charT* argv[],
\r
143 const options_description&,
\r
145 function1<std::pair<std::string, std::string>,
\r
146 const std::string&> ext
\r
149 /** Parse a config file.
\r
151 Read from given stream.
\r
153 template<class charT>
\r
154 #if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
\r
155 BOOST_PROGRAM_OPTIONS_DECL
\r
157 basic_parsed_options<charT>
\r
158 parse_config_file(std::basic_istream<charT>&, const options_description&,
\r
159 bool allow_unregistered = false);
\r
161 /** Parse a config file.
\r
163 Read from file with the given name. The character type is
\r
164 passed to the file stream.
\r
166 template<class charT>
\r
167 #if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
\r
168 BOOST_PROGRAM_OPTIONS_DECL
\r
170 basic_parsed_options<charT>
\r
171 parse_config_file(const char* filename, const options_description&,
\r
172 bool allow_unregistered = false);
\r
174 /** Controls if the 'collect_unregistered' function should
\r
175 include positional options, or not. */
\r
176 enum collect_unrecognized_mode
\r
177 { include_positional, exclude_positional };
\r
179 /** Collects the original tokens for all named options with
\r
180 'unregistered' flag set. If 'mode' is 'include_positional'
\r
181 also collects all positional options.
\r
182 Returns the vector of origianl tokens for all collected
\r
185 template<class charT>
\r
186 std::vector< std::basic_string<charT> >
\r
187 collect_unrecognized(const std::vector< basic_option<charT> >& options,
\r
188 enum collect_unrecognized_mode mode);
\r
190 /** Parse environment.
\r
192 For each environment variable, the 'name_mapper' function is called to
\r
193 obtain the option name. If it returns empty string, the variable is
\r
196 This is done since naming of environment variables is typically
\r
197 different from the naming of command line options.
\r
199 BOOST_PROGRAM_OPTIONS_DECL parsed_options
\r
200 parse_environment(const options_description&,
\r
201 const function1<std::string, std::string>& name_mapper);
\r
203 /** Parse environment.
\r
205 Takes all environment variables which start with 'prefix'. The option
\r
206 name is obtained from variable name by removing the prefix and
\r
207 converting the remaining string into lower case.
\r
209 BOOST_PROGRAM_OPTIONS_DECL parsed_options
\r
210 parse_environment(const options_description&, const std::string& prefix);
\r
213 This function exists to resolve ambiguity between the two above
\r
214 functions when second argument is of 'char*' type. There's implicit
\r
215 conversion to both function1 and string.
\r
217 BOOST_PROGRAM_OPTIONS_DECL parsed_options
\r
218 parse_environment(const options_description&, const char* prefix);
\r
220 /** Splits a given string to a collection of single strings which
\r
221 can be passed to command_line_parser. The second parameter is
\r
222 used to specify a collection of possible seperator chars used
\r
223 for splitting. The seperator is defaulted to space " ".
\r
224 Splitting is done in a unix style way, with respect to quotes '"'
\r
225 and escape characters '\'
\r
227 BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
\r
228 split_unix(const std::string& cmdline, const std::string& seperator = " \t",
\r
229 const std::string& quote = "'\"", const std::string& escape = "\\");
\r
231 #ifndef BOOST_NO_STD_WSTRING
\r
233 BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
\r
234 split_unix(const std::wstring& cmdline, const std::wstring& seperator = L" \t",
\r
235 const std::wstring& quote = L"'\"", const std::wstring& escape = L"\\");
\r
239 /** Parses the char* string which is passed to WinMain function on
\r
240 windows. This function is provided for convenience, and because it's
\r
241 not clear how to portably access split command line string from
\r
242 runtime library and if it always exists.
\r
243 This function is available only on Windows.
\r
245 BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
\r
246 split_winmain(const std::string& cmdline);
\r
248 #ifndef BOOST_NO_STD_WSTRING
\r
250 BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
\r
251 split_winmain(const std::wstring& cmdline);
\r
260 #include "boost/program_options/detail/parsers.hpp"
\r