parsers.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright Vladimir Prus 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_HPP_VP_2004_05_06
  6. #define BOOST_PARSERS_HPP_VP_2004_05_06
  7. #include <boost/program_options/detail/convert.hpp>
  8. #include <iterator>
  9. namespace boost { namespace program_options {
  10. namespace detail {
  11. template<class charT, class Iterator>
  12. std::vector<std::basic_string<charT> >
  13. make_vector(Iterator i, Iterator e)
  14. {
  15. std::vector<std::basic_string<charT> > result;
  16. // Some compilers don't have templated constructor for
  17. // vector, so we can't create vector from (argv+1, argv+argc) range
  18. for(; i != e; ++i)
  19. result.push_back(*i);
  20. return result;
  21. }
  22. }
  23. template<class charT>
  24. basic_command_line_parser<charT>::
  25. basic_command_line_parser(const std::vector<
  26. std::basic_string<charT> >& xargs)
  27. : detail::cmdline(to_internal(xargs))
  28. {}
  29. template<class charT>
  30. basic_command_line_parser<charT>::
  31. basic_command_line_parser(int argc, const charT* const argv[])
  32. : detail::cmdline(
  33. // Explicit template arguments are required by gcc 3.3.1
  34. // (at least mingw version), and do no harm on other compilers.
  35. to_internal(detail::make_vector<charT, const charT* const*>(argv+1, argv+argc+!argc))),
  36. m_desc()
  37. {}
  38. template<class charT>
  39. basic_command_line_parser<charT>&
  40. basic_command_line_parser<charT>::options(const options_description& desc)
  41. {
  42. detail::cmdline::set_options_description(desc);
  43. m_desc = &desc;
  44. return *this;
  45. }
  46. template<class charT>
  47. basic_command_line_parser<charT>&
  48. basic_command_line_parser<charT>::positional(
  49. const positional_options_description& desc)
  50. {
  51. detail::cmdline::set_positional_options(desc);
  52. return *this;
  53. }
  54. template<class charT>
  55. basic_command_line_parser<charT>&
  56. basic_command_line_parser<charT>::style(int xstyle)
  57. {
  58. detail::cmdline::style(xstyle);
  59. return *this;
  60. }
  61. template<class charT>
  62. basic_command_line_parser<charT>&
  63. basic_command_line_parser<charT>::extra_parser(ext_parser ext)
  64. {
  65. detail::cmdline::set_additional_parser(ext);
  66. return *this;
  67. }
  68. template<class charT>
  69. basic_command_line_parser<charT>&
  70. basic_command_line_parser<charT>::allow_unregistered()
  71. {
  72. detail::cmdline::allow_unregistered();
  73. return *this;
  74. }
  75. template<class charT>
  76. basic_command_line_parser<charT>&
  77. basic_command_line_parser<charT>::extra_style_parser(style_parser s)
  78. {
  79. detail::cmdline::extra_style_parser(s);
  80. return *this;
  81. }
  82. template<class charT>
  83. basic_parsed_options<charT>
  84. basic_command_line_parser<charT>::run()
  85. {
  86. // save the canonical prefixes which were used by this cmdline parser
  87. // eventually inside the parsed results
  88. // This will be handy to format recognisable options
  89. // for diagnostic messages if everything blows up much later on
  90. parsed_options result(m_desc, detail::cmdline::get_canonical_option_prefix());
  91. result.options = detail::cmdline::run();
  92. // Presense of parsed_options -> wparsed_options conversion
  93. // does the trick.
  94. return basic_parsed_options<charT>(result);
  95. }
  96. template<class charT>
  97. basic_parsed_options<charT>
  98. parse_command_line(int argc, const charT* const argv[],
  99. const options_description& desc,
  100. int style,
  101. function1<std::pair<std::string, std::string>,
  102. const std::string&> ext)
  103. {
  104. return basic_command_line_parser<charT>(argc, argv).options(desc).
  105. style(style).extra_parser(ext).run();
  106. }
  107. template<class charT>
  108. std::vector< std::basic_string<charT> >
  109. collect_unrecognized(const std::vector< basic_option<charT> >& options,
  110. enum collect_unrecognized_mode mode)
  111. {
  112. std::vector< std::basic_string<charT> > result;
  113. for(unsigned i = 0; i < options.size(); ++i)
  114. {
  115. if (options[i].unregistered ||
  116. (mode == include_positional && options[i].position_key != -1))
  117. {
  118. copy(options[i].original_tokens.begin(),
  119. options[i].original_tokens.end(),
  120. back_inserter(result));
  121. }
  122. }
  123. return result;
  124. }
  125. }}
  126. #endif