parsers.hpp 4.0 KB

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