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