parsers.hpp 11 KB

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