Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 1 | // (C) Copyright Gennadiy Rozental 2005-2008. |
| 2 | // Use, modification, and distribution are subject to the |
| 3 | // Boost Software License, Version 1.0. (See accompanying file |
| 4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | // See http://www.boost.org/libs/test for the library home page. |
| 7 | // |
| 8 | // File : $RCSfile$ |
| 9 | // |
| 10 | // Version : $Revision: 54633 $ |
| 11 | // |
| 12 | // Description : implements parser - public interface for CLA parsing and accessing |
| 13 | // *************************************************************************** |
| 14 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 15 | #ifndef NDNBOOST_RT_CLA_PARSER_IPP_062904GER |
| 16 | #define NDNBOOST_RT_CLA_PARSER_IPP_062904GER |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 17 | |
| 18 | // Boost.Runtime.Parameter |
| 19 | #include <ndnboost/test/utils/runtime/config.hpp> |
| 20 | #include <ndnboost/test/utils/runtime/trace.hpp> |
| 21 | #include <ndnboost/test/utils/runtime/argument.hpp> |
| 22 | |
| 23 | #include <ndnboost/test/utils/runtime/cla/argv_traverser.hpp> |
| 24 | #include <ndnboost/test/utils/runtime/cla/parameter.hpp> |
| 25 | #include <ndnboost/test/utils/runtime/cla/modifier.hpp> |
| 26 | #include <ndnboost/test/utils/runtime/cla/validation.hpp> |
| 27 | #include <ndnboost/test/utils/runtime/cla/parser.hpp> |
| 28 | |
| 29 | // Boost.Test |
| 30 | #include <ndnboost/test/utils/basic_cstring/io.hpp> |
| 31 | #include <ndnboost/test/utils/foreach.hpp> |
| 32 | |
| 33 | // Boost |
| 34 | #include <ndnboost/lexical_cast.hpp> |
| 35 | |
| 36 | namespace ndnboost { |
| 37 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 38 | namespace NDNBOOST_RT_PARAM_NAMESPACE { |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 39 | |
| 40 | namespace cla { |
| 41 | |
| 42 | // ************************************************************************** // |
| 43 | // ************** runtime::cla::parser ************** // |
| 44 | // ************************************************************************** // |
| 45 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 46 | NDNBOOST_RT_PARAM_INLINE |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 47 | parser::parser( cstring program_name ) |
| 48 | { |
| 49 | assign_op( m_program_name, program_name, 0 ); |
| 50 | } |
| 51 | |
| 52 | //____________________________________________________________________________// |
| 53 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 54 | NDNBOOST_RT_PARAM_INLINE parser::param_iterator |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 55 | parser::first_param() const |
| 56 | { |
| 57 | return m_parameters.begin(); |
| 58 | } |
| 59 | |
| 60 | //____________________________________________________________________________// |
| 61 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 62 | NDNBOOST_RT_PARAM_INLINE parser::param_iterator |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 63 | parser::last_param() const |
| 64 | { |
| 65 | return m_parameters.end(); |
| 66 | } |
| 67 | |
| 68 | //____________________________________________________________________________// |
| 69 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 70 | NDNBOOST_RT_PARAM_INLINE argument const& |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 71 | parser::valid_argument( cstring string_id ) const |
| 72 | { |
| 73 | const_argument_ptr arg = (*this)[string_id]; |
| 74 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 75 | NDNBOOST_RT_PARAM_VALIDATE_LOGIC( !!arg, "Actual argument for parameter " << string_id << " is not present" ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 76 | |
| 77 | return *arg; |
| 78 | } |
| 79 | |
| 80 | //____________________________________________________________________________// |
| 81 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 82 | NDNBOOST_RT_PARAM_INLINE parser& |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 83 | parser::operator<<( parameter_ptr new_param ) |
| 84 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 85 | NDNBOOST_TEST_FOREACH( parameter_ptr, old_param, m_parameters ) { |
| 86 | NDNBOOST_RT_PARAM_VALIDATE_LOGIC( !old_param->conflict_with( *new_param ), |
| 87 | NDNBOOST_RT_PARAM_LITERAL( "Definition of parameter " ) << new_param->id_2_report() << |
| 88 | NDNBOOST_RT_PARAM_LITERAL( " conflicts with defintion of parameter " ) << old_param->id_2_report() ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | m_parameters.push_back( new_param ); |
| 92 | |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | //____________________________________________________________________________// |
| 97 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 98 | NDNBOOST_RT_PARAM_INLINE void |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 99 | parser::parse( int& argc, char_type** argv ) |
| 100 | { |
| 101 | if( m_program_name.empty() ) { |
| 102 | m_program_name.assign( argv[0] ); |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 103 | dstring::size_type pos = m_program_name.find_last_of( NDNBOOST_RT_PARAM_LITERAL( "/\\" ) ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 104 | |
| 105 | if( pos != static_cast<dstring::size_type>(cstring::npos) ) |
| 106 | m_program_name.erase( 0, pos+1 ); |
| 107 | } |
| 108 | |
| 109 | m_traverser.init( argc, argv ); |
| 110 | |
| 111 | try { |
| 112 | while( !m_traverser.eoi() ) { |
| 113 | parameter_ptr found_param; |
| 114 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 115 | NDNBOOST_RT_PARAM_TRACE( "Total " << m_parameters.size() << " parameters registered" ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 116 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 117 | NDNBOOST_TEST_FOREACH( parameter_ptr const&, curr_param, m_parameters ) { |
| 118 | NDNBOOST_RT_PARAM_TRACE( "Try parameter " << curr_param->id_2_report() ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 119 | |
| 120 | if( curr_param->matching( m_traverser, !found_param ) ) { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 121 | NDNBOOST_RT_PARAM_TRACE( "Match found" ); |
| 122 | NDNBOOST_RT_CLA_VALIDATE_INPUT( !found_param, (m_traverser.rollback(),m_traverser), "Ambiguous input" ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 123 | |
| 124 | found_param = curr_param; |
| 125 | } |
| 126 | |
| 127 | m_traverser.rollback(); |
| 128 | } |
| 129 | |
| 130 | if( !found_param ) { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 131 | NDNBOOST_RT_PARAM_TRACE( "No match found" ); |
| 132 | NDNBOOST_RT_CLA_VALIDATE_INPUT( m_traverser.handle_mismatch(), m_traverser, |
| 133 | NDNBOOST_RT_PARAM_LITERAL( "Unexpected input" ) ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 134 | |
| 135 | continue; |
| 136 | } |
| 137 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 138 | NDNBOOST_RT_PARAM_TRACE( "Parse argument value" ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 139 | found_param->produce_argument( m_traverser ); |
| 140 | |
| 141 | m_traverser.commit(); |
| 142 | } |
| 143 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 144 | NDNBOOST_TEST_FOREACH( parameter_ptr const&, curr_param, m_parameters ) { |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 145 | if( !curr_param->p_optional && !curr_param->actual_argument() ) { |
| 146 | curr_param->produce_argument( *this ); |
| 147 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 148 | NDNBOOST_RT_PARAM_VALIDATE_LOGIC( curr_param->actual_argument(), |
| 149 | NDNBOOST_RT_PARAM_LITERAL( "Required argument for parameter " ) << curr_param->id_2_report() |
| 150 | << NDNBOOST_RT_PARAM_LITERAL( " is missing" ) ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | catch( bad_lexical_cast const& ) { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 155 | NDNBOOST_RT_PARAM_REPORT_LOGIC_ERROR( |
| 156 | NDNBOOST_RT_PARAM_LITERAL( "String to value convertion error during input parsing" ) ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | m_traverser.remainder( argc, argv ); |
| 160 | } |
| 161 | |
| 162 | //____________________________________________________________________________// |
| 163 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 164 | NDNBOOST_RT_PARAM_INLINE const_argument_ptr |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 165 | parser::operator[]( cstring string_id ) const |
| 166 | { |
| 167 | parameter_ptr found_param; |
| 168 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 169 | NDNBOOST_TEST_FOREACH( parameter_ptr const&, curr_param, m_parameters ) { |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 170 | if( curr_param->responds_to( string_id ) ) { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 171 | NDNBOOST_RT_PARAM_VALIDATE_LOGIC( !found_param, |
| 172 | NDNBOOST_RT_PARAM_LITERAL( "Ambiguous parameter string id: " ) << string_id ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 173 | |
| 174 | found_param = curr_param; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return found_param ? found_param->actual_argument() : argument_ptr(); |
| 179 | } |
| 180 | |
| 181 | //____________________________________________________________________________// |
| 182 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 183 | NDNBOOST_RT_PARAM_INLINE cstring |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 184 | parser::get( cstring string_id ) const |
| 185 | { |
| 186 | return get<cstring>( string_id ); |
| 187 | } |
| 188 | |
| 189 | //____________________________________________________________________________// |
| 190 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 191 | NDNBOOST_RT_PARAM_INLINE void |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 192 | parser::usage( out_stream& ostr ) |
| 193 | { |
| 194 | if( m_program_name.empty() ) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 195 | assign_op( m_program_name, NDNBOOST_RT_PARAM_CSTRING_LITERAL( "<program>" ), 0 ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 196 | |
| 197 | format_stream fs; |
| 198 | |
| 199 | fs << m_program_name; |
| 200 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 201 | NDNBOOST_TEST_FOREACH( parameter_ptr const&, curr_param, m_parameters ) { |
| 202 | fs << NDNBOOST_RT_PARAM_LITERAL( ' ' ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 203 | |
| 204 | if( curr_param->p_optional ) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 205 | fs << NDNBOOST_RT_PARAM_LITERAL( '[' ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 206 | |
| 207 | curr_param->usage_info( fs ); |
| 208 | |
| 209 | if( curr_param->p_optional ) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 210 | fs << NDNBOOST_RT_PARAM_LITERAL( ']' ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 211 | |
| 212 | if( curr_param->p_multiplicable ) { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 213 | fs << NDNBOOST_RT_PARAM_CSTRING_LITERAL( " ... " ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 214 | |
| 215 | if( curr_param->p_optional ) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 216 | fs << NDNBOOST_RT_PARAM_LITERAL( '[' ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 217 | |
| 218 | curr_param->usage_info( fs ); |
| 219 | |
| 220 | if( curr_param->p_optional ) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 221 | fs << NDNBOOST_RT_PARAM_LITERAL( ']' ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 225 | ostr << NDNBOOST_RT_PARAM_CSTRING_LITERAL( "Usage:\n" ) << fs.str() << std::endl; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | //____________________________________________________________________________// |
| 229 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 230 | NDNBOOST_RT_PARAM_INLINE void |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 231 | parser::help( out_stream& ostr ) |
| 232 | { |
| 233 | usage( ostr ); |
| 234 | |
| 235 | bool need_where = true; |
| 236 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 237 | NDNBOOST_TEST_FOREACH( parameter_ptr const&, curr_param, m_parameters ) { |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 238 | if( curr_param->p_description->empty() ) |
| 239 | continue; |
| 240 | |
| 241 | if( need_where ) { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 242 | ostr << NDNBOOST_RT_PARAM_CSTRING_LITERAL( "where:\n" ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 243 | need_where = false; |
| 244 | } |
| 245 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 246 | ostr << curr_param->id_2_report() << NDNBOOST_RT_PARAM_CSTRING_LITERAL( " - " ) << curr_param->p_description << std::endl; |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | //____________________________________________________________________________// |
| 251 | |
| 252 | } // namespace cla |
| 253 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 254 | } // namespace NDNBOOST_RT_PARAM_NAMESPACE |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 255 | |
| 256 | } // namespace ndnboost |
| 257 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 258 | #endif // NDNBOOST_RT_CLA_PARSER_IPP_062904GER |