PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/program_options/detail/parsers.hpp

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