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