parsers.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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', meaning:
  101. @param argc Must be non-negative i.e. >= 0
  102. @param argv Argv[argc] must be 0 e.g. nullptr and
  103. if argc is >0 argv[0] up to argv[argc-1] must point to
  104. null terminated strings
  105. */
  106. basic_command_line_parser(int argc, const charT* const argv[]);
  107. /** Sets options descriptions to use. */
  108. basic_command_line_parser& options(const options_description& desc);
  109. /** Sets positional options description to use. */
  110. basic_command_line_parser& positional(
  111. const positional_options_description& desc);
  112. /** Sets the command line style. */
  113. basic_command_line_parser& style(int);
  114. /** Sets the extra parsers. */
  115. basic_command_line_parser& extra_parser(ext_parser);
  116. /** Parses the options and returns the result of parsing.
  117. Throws on error.
  118. */
  119. basic_parsed_options<charT> run();
  120. /** Specifies that unregistered options are allowed and should
  121. be passed though. For each command like token that looks
  122. like an option but does not contain a recognized name, an
  123. instance of basic_option<charT> will be added to result,
  124. with 'unrecognized' field set to 'true'. It's possible to
  125. collect all unrecognized options with the 'collect_unrecognized'
  126. funciton.
  127. */
  128. basic_command_line_parser& allow_unregistered();
  129. using detail::cmdline::style_parser;
  130. basic_command_line_parser& extra_style_parser(style_parser s);
  131. private:
  132. const options_description* m_desc;
  133. };
  134. typedef basic_command_line_parser<char> command_line_parser;
  135. typedef basic_command_line_parser<wchar_t> wcommand_line_parser;
  136. /** Creates instance of 'command_line_parser', passes parameters to it,
  137. and returns the result of calling the 'run' method.
  138. */
  139. template<class charT>
  140. basic_parsed_options<charT>
  141. parse_command_line(int argc, const charT* const argv[],
  142. const options_description&,
  143. int style = 0,
  144. function1<std::pair<std::string, std::string>,
  145. const std::string&> ext
  146. = ext_parser());
  147. /** Parse a config file.
  148. Read from given stream.
  149. */
  150. template<class charT>
  151. #if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
  152. BOOST_PROGRAM_OPTIONS_DECL
  153. #endif
  154. basic_parsed_options<charT>
  155. parse_config_file(std::basic_istream<charT>&, const options_description&,
  156. bool allow_unregistered = false);
  157. /** Parse a config file.
  158. Read from file with the given name. The character type is
  159. passed to the file stream.
  160. */
  161. #ifdef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
  162. template<class charT>
  163. #else
  164. template<class charT = char>
  165. #endif
  166. #if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700))
  167. BOOST_PROGRAM_OPTIONS_DECL
  168. #endif
  169. basic_parsed_options<charT>
  170. parse_config_file(const char* filename, const options_description&,
  171. bool allow_unregistered = false);
  172. /** Controls if the 'collect_unregistered' function should
  173. include positional options, or not. */
  174. enum collect_unrecognized_mode
  175. { include_positional, exclude_positional };
  176. /** Collects the original tokens for all named options with
  177. 'unregistered' flag set. If 'mode' is 'include_positional'
  178. also collects all positional options.
  179. Returns the vector of origianl tokens for all collected
  180. options.
  181. */
  182. template<class charT>
  183. std::vector< std::basic_string<charT> >
  184. collect_unrecognized(const std::vector< basic_option<charT> >& options,
  185. enum collect_unrecognized_mode mode);
  186. /** Parse environment.
  187. For each environment variable, the 'name_mapper' function is called to
  188. obtain the option name. If it returns empty string, the variable is
  189. ignored.
  190. This is done since naming of environment variables is typically
  191. different from the naming of command line options.
  192. */
  193. BOOST_PROGRAM_OPTIONS_DECL parsed_options
  194. parse_environment(const options_description&,
  195. const function1<std::string, std::string>& name_mapper);
  196. /** Parse environment.
  197. Takes all environment variables which start with 'prefix'. The option
  198. name is obtained from variable name by removing the prefix and
  199. converting the remaining string into lower case.
  200. */
  201. BOOST_PROGRAM_OPTIONS_DECL parsed_options
  202. parse_environment(const options_description&, const std::string& prefix);
  203. /** @overload
  204. This function exists to resolve ambiguity between the two above
  205. functions when second argument is of 'char*' type. There's implicit
  206. conversion to both function1 and string.
  207. */
  208. BOOST_PROGRAM_OPTIONS_DECL parsed_options
  209. parse_environment(const options_description&, const char* prefix);
  210. /** Splits a given string to a collection of single strings which
  211. can be passed to command_line_parser. The second parameter is
  212. used to specify a collection of possible seperator chars used
  213. for splitting. The seperator is defaulted to space " ".
  214. Splitting is done in a unix style way, with respect to quotes '"'
  215. and escape characters '\'
  216. */
  217. BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
  218. split_unix(const std::string& cmdline, const std::string& seperator = " \t",
  219. const std::string& quote = "'\"", const std::string& escape = "\\");
  220. #ifndef BOOST_NO_STD_WSTRING
  221. /** @overload */
  222. BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
  223. split_unix(const std::wstring& cmdline, const std::wstring& seperator = L" \t",
  224. const std::wstring& quote = L"'\"", const std::wstring& escape = L"\\");
  225. #endif
  226. #ifdef _WIN32
  227. /** Parses the char* string which is passed to WinMain function on
  228. windows. This function is provided for convenience, and because it's
  229. not clear how to portably access split command line string from
  230. runtime library and if it always exists.
  231. This function is available only on Windows.
  232. */
  233. BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
  234. split_winmain(const std::string& cmdline);
  235. #ifndef BOOST_NO_STD_WSTRING
  236. /** @overload */
  237. BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
  238. split_winmain(const std::wstring& cmdline);
  239. #endif
  240. #endif
  241. }}
  242. #if defined(BOOST_MSVC)
  243. # pragma warning (pop)
  244. #endif
  245. #undef DECL
  246. #include "boost/program_options/detail/parsers.hpp"
  247. #endif