1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/program_options/parsers.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,228 @@
1.4 +// Copyright Vladimir Prus 2002-2004.
1.5 +// Distributed under the Boost Software License, Version 1.0.
1.6 +// (See accompanying file LICENSE_1_0.txt
1.7 +// or copy at http://www.boost.org/LICENSE_1_0.txt)
1.8 +
1.9 +
1.10 +#ifndef BOOST_PARSERS_VP_2003_05_19
1.11 +#define BOOST_PARSERS_VP_2003_05_19
1.12 +
1.13 +#include <boost/program_options/config.hpp>
1.14 +#include <boost/program_options/option.hpp>
1.15 +#include <boost/program_options/detail/cmdline.hpp>
1.16 +
1.17 +#include <boost/function/function1.hpp>
1.18 +
1.19 +#include <iosfwd>
1.20 +#include <vector>
1.21 +#include <utility>
1.22 +
1.23 +namespace boost { namespace program_options {
1.24 +
1.25 + class options_description;
1.26 + class positional_options_description;
1.27 +
1.28 +
1.29 + /** Results of parsing an input source.
1.30 + The primary use of this class is passing information from parsers
1.31 + component to value storage component. This class does not makes
1.32 + much sense itself.
1.33 + */
1.34 + template<class charT>
1.35 + class basic_parsed_options {
1.36 + public:
1.37 + explicit basic_parsed_options(const options_description* description)
1.38 + : description(description) {}
1.39 + /** Options found in the source. */
1.40 + std::vector< basic_option<charT> > options;
1.41 + /** Options description that was used for parsing.
1.42 + Parsers should return pointer to the instance of
1.43 + option_description passed to them, and issues of lifetime are
1.44 + up to the caller. Can be NULL.
1.45 + */
1.46 + const options_description* description;
1.47 + };
1.48 +
1.49 + /** Specialization of basic_parsed_options which:
1.50 + - provides convenient conversion from basic_parsed_options<char>
1.51 + - stores the passed char-based options for later use.
1.52 + */
1.53 + template<>
1.54 + class BOOST_PROGRAM_OPTIONS_DECL basic_parsed_options<wchar_t> {
1.55 + public:
1.56 + /** Constructs wrapped options from options in UTF8 encoding. */
1.57 + explicit basic_parsed_options(const basic_parsed_options<char>& po);
1.58 +
1.59 + std::vector< basic_option<wchar_t> > options;
1.60 + const options_description* description;
1.61 +
1.62 + /** Stores UTF8 encoded options that were passed to constructor,
1.63 + to avoid reverse conversion in some cases. */
1.64 + basic_parsed_options<char> utf8_encoded_options;
1.65 + };
1.66 +
1.67 + typedef basic_parsed_options<char> parsed_options;
1.68 + typedef basic_parsed_options<wchar_t> wparsed_options;
1.69 +
1.70 + /** Augments basic_parsed_options<wchar_t> with conversion from
1.71 + 'parsed_options' */
1.72 +
1.73 +
1.74 + typedef function1<std::pair<std::string, std::string>, const std::string&> ext_parser;
1.75 +
1.76 + /** Command line parser.
1.77 +
1.78 + The class allows one to specify all the information needed for parsing
1.79 + and to parse the command line. It is primarily needed to
1.80 + emulate named function parameters -- a regular function with 5
1.81 + parameters will be hard to use and creating overloads with a smaller
1.82 + nuber of parameters will be confusing.
1.83 +
1.84 + For the most common case, the function parse_command_line is a better
1.85 + alternative.
1.86 +
1.87 + There are two typedefs -- command_line_parser and wcommand_line_parser,
1.88 + for charT == char and charT == wchar_t cases.
1.89 + */
1.90 + template<class charT>
1.91 + class basic_command_line_parser : private detail::cmdline {
1.92 + public:
1.93 + /** Creates a command line parser for the specified arguments
1.94 + list. The 'args' parameter should not include program name.
1.95 + */
1.96 + basic_command_line_parser(const std::vector<
1.97 + std::basic_string<charT> >& args);
1.98 + /** Creates a command line parser for the specified arguments
1.99 + list. The parameters should be the same as passed to 'main'.
1.100 + */
1.101 + basic_command_line_parser(int argc, charT* argv[]);
1.102 +
1.103 + /** Sets options descriptions to use. */
1.104 + basic_command_line_parser& options(const options_description& desc);
1.105 + /** Sets positional options description to use. */
1.106 + basic_command_line_parser& positional(
1.107 + const positional_options_description& desc);
1.108 +
1.109 + /** Sets the command line style. */
1.110 + basic_command_line_parser& style(int);
1.111 + /** Sets the extra parsers. */
1.112 + basic_command_line_parser& extra_parser(ext_parser);
1.113 +
1.114 + /** Parses the options and returns the result of parsing.
1.115 + Throws on error.
1.116 + */
1.117 + basic_parsed_options<charT> run();
1.118 +
1.119 + /** Specifies that unregistered options are allowed and should
1.120 + be passed though. For each command like token that looks
1.121 + like an option but does not contain a recognized name, an
1.122 + instance of basic_option<charT> will be added to result,
1.123 + with 'unrecognized' field set to 'true'. It's possible to
1.124 + collect all unrecognized options with the 'collect_unrecognized'
1.125 + funciton.
1.126 + */
1.127 + basic_command_line_parser& allow_unregistered();
1.128 +
1.129 + using detail::cmdline::style_parser;
1.130 +
1.131 + basic_command_line_parser& extra_style_parser(style_parser s);
1.132 +
1.133 + private:
1.134 + const options_description* m_desc;
1.135 + };
1.136 +
1.137 + typedef basic_command_line_parser<char> command_line_parser;
1.138 + typedef basic_command_line_parser<wchar_t> wcommand_line_parser;
1.139 +
1.140 + /** Creates instance of 'command_line_parser', passes parameters to it,
1.141 + and returns the result of calling the 'run' method.
1.142 + */
1.143 + template<class charT>
1.144 + basic_parsed_options<charT>
1.145 + parse_command_line(int argc, charT* argv[],
1.146 + const options_description&,
1.147 + int style = 0,
1.148 + function1<std::pair<std::string, std::string>,
1.149 + const std::string&> ext
1.150 + = ext_parser());
1.151 +
1.152 + /** Parse a config file.
1.153 + */
1.154 + template<class charT>
1.155 +#if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
1.156 + BOOST_PROGRAM_OPTIONS_DECL
1.157 +#endif
1.158 + basic_parsed_options<charT>
1.159 + parse_config_file(std::basic_istream<charT>&, const options_description&);
1.160 +
1.161 + /** Controls if the 'collect_unregistered' function should
1.162 + include positional options, or not. */
1.163 + enum collect_unrecognized_mode
1.164 + { include_positional, exclude_positional };
1.165 +
1.166 + /** Collects the original tokens for all named options with
1.167 + 'unregistered' flag set. If 'mode' is 'include_positional'
1.168 + also collects all positional options.
1.169 + Returns the vector of origianl tokens for all collected
1.170 + options.
1.171 + */
1.172 + template<class charT>
1.173 + std::vector< std::basic_string<charT> >
1.174 + collect_unrecognized(const std::vector< basic_option<charT> >& options,
1.175 + enum collect_unrecognized_mode mode);
1.176 +
1.177 + /** Parse environment.
1.178 +
1.179 + For each environment variable, the 'name_mapper' function is called to
1.180 + obtain the option name. If it returns empty string, the variable is
1.181 + ignored.
1.182 +
1.183 + This is done since naming of environment variables is typically
1.184 + different from the naming of command line options.
1.185 + */
1.186 + BOOST_PROGRAM_OPTIONS_DECL parsed_options
1.187 + parse_environment(const options_description&,
1.188 + const function1<std::string, std::string>& name_mapper);
1.189 +
1.190 + /** Parse environment.
1.191 +
1.192 + Takes all environment variables which start with 'prefix'. The option
1.193 + name is obtained from variable name by removing the prefix and
1.194 + converting the remaining string into lower case.
1.195 + */
1.196 + BOOST_PROGRAM_OPTIONS_DECL parsed_options
1.197 + parse_environment(const options_description&, const std::string& prefix);
1.198 +
1.199 + /** @overload
1.200 + This function exists to resolve ambiguity between the two above
1.201 + functions when second argument is of 'char*' type. There's implicit
1.202 + conversion to both function1 and string.
1.203 + */
1.204 + BOOST_PROGRAM_OPTIONS_DECL parsed_options
1.205 + parse_environment(const options_description&, const char* prefix);
1.206 +
1.207 + #ifdef _WIN32
1.208 + /** Parses the char* string which is passed to WinMain function on
1.209 + windows. This function is provided for convenience, and because it's
1.210 + not clear how to portably access split command line string from
1.211 + runtime library and if it always exists.
1.212 + This function is available only on Windows.
1.213 + */
1.214 + BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
1.215 + split_winmain(const std::string& cmdline);
1.216 +
1.217 +#ifndef BOOST_NO_STD_WSTRING
1.218 + /** @overload */
1.219 + BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
1.220 + split_winmain(const std::wstring& cmdline);
1.221 + #endif
1.222 +#endif
1.223 +
1.224 +
1.225 +}}
1.226 +
1.227 +#undef DECL
1.228 +
1.229 +#include "boost/program_options/detail/parsers.hpp"
1.230 +
1.231 +#endif