parsers.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. {}
  37. template<class charT>
  38. basic_command_line_parser<charT>&
  39. basic_command_line_parser<charT>::options(const options_description& desc)
  40. {
  41. detail::cmdline::set_options_description(desc);
  42. m_desc = &desc;
  43. return *this;
  44. }
  45. template<class charT>
  46. basic_command_line_parser<charT>&
  47. basic_command_line_parser<charT>::positional(
  48. const positional_options_description& desc)
  49. {
  50. detail::cmdline::set_positional_options(desc);
  51. return *this;
  52. }
  53. template<class charT>
  54. basic_command_line_parser<charT>&
  55. basic_command_line_parser<charT>::style(int xstyle)
  56. {
  57. detail::cmdline::style(xstyle);
  58. return *this;
  59. }
  60. template<class charT>
  61. basic_command_line_parser<charT>&
  62. basic_command_line_parser<charT>::extra_parser(ext_parser ext)
  63. {
  64. detail::cmdline::set_additional_parser(ext);
  65. return *this;
  66. }
  67. template<class charT>
  68. basic_command_line_parser<charT>&
  69. basic_command_line_parser<charT>::allow_unregistered()
  70. {
  71. detail::cmdline::allow_unregistered();
  72. return *this;
  73. }
  74. template<class charT>
  75. basic_command_line_parser<charT>&
  76. basic_command_line_parser<charT>::extra_style_parser(style_parser s)
  77. {
  78. detail::cmdline::extra_style_parser(s);
  79. return *this;
  80. }
  81. template<class charT>
  82. basic_parsed_options<charT>
  83. basic_command_line_parser<charT>::run()
  84. {
  85. // save the canonical prefixes which were used by this cmdline parser
  86. // eventually inside the parsed results
  87. // This will be handy to format recognisable options
  88. // for diagnostic messages if everything blows up much later on
  89. parsed_options result(m_desc, detail::cmdline::get_canonical_option_prefix());
  90. result.options = detail::cmdline::run();
  91. // Presense of parsed_options -> wparsed_options conversion
  92. // does the trick.
  93. return basic_parsed_options<charT>(result);
  94. }
  95. template<class charT>
  96. basic_parsed_options<charT>
  97. parse_command_line(int argc, const charT* const argv[],
  98. const options_description& desc,
  99. int style,
  100. function1<std::pair<std::string, std::string>,
  101. const std::string&> ext)
  102. {
  103. return basic_command_line_parser<charT>(argc, argv).options(desc).
  104. style(style).extra_parser(ext).run();
  105. }
  106. template<class charT>
  107. std::vector< std::basic_string<charT> >
  108. collect_unrecognized(const std::vector< basic_option<charT> >& options,
  109. enum collect_unrecognized_mode mode)
  110. {
  111. std::vector< std::basic_string<charT> > result;
  112. for(unsigned i = 0; i < options.size(); ++i)
  113. {
  114. if (options[i].unregistered ||
  115. (mode == include_positional && options[i].position_key != -1))
  116. {
  117. copy(options[i].original_tokens.begin(),
  118. options[i].original_tokens.end(),
  119. back_inserter(result));
  120. }
  121. }
  122. return result;
  123. }
  124. }}
  125. #endif