CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2010 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien_jorge@yahoo.fr 00024 */ 00030 #include <claw/arguments_table.hpp> 00031 #include <claw/assert.hpp> 00032 #include <iostream> 00033 00034 /*----------------------------------------------------------------------------*/ 00043 claw::arguments_table::argument_attributes::argument_attributes 00044 ( const std::string& name, const std::string& second_name, 00045 const std::string& help_message, bool optional, 00046 const std::string& value_type ) 00047 : m_name(name), m_second_name(second_name), m_help_message(help_message), 00048 m_optional(optional), m_value_type(value_type) 00049 { 00050 00051 } // arguments_table::argument_attributes::argument_attributes() 00052 00053 /*----------------------------------------------------------------------------*/ 00058 bool claw::arguments_table::argument_attributes::operator< 00059 ( const argument_attributes& that ) const 00060 { 00061 return m_name < that.m_name; 00062 } // arguments_table::argument_attributes::operator<() 00063 00064 /*----------------------------------------------------------------------------*/ 00068 std::string 00069 claw::arguments_table::argument_attributes::format_short_help() const 00070 { 00071 std::string result(m_name); 00072 00073 if (!m_value_type.empty()) 00074 result += "=" + m_value_type; 00075 00076 if (m_optional) 00077 return "[" + result + "]"; 00078 else 00079 return result; 00080 } // arguments_table::argument_attributes::format_short_help() 00081 00082 /*----------------------------------------------------------------------------*/ 00086 std::string claw::arguments_table::argument_attributes::format_long_help() const 00087 { 00088 std::string result(m_name); 00089 00090 if ( !m_second_name.empty() ) 00091 result += ", " + m_second_name; 00092 00093 return result + "\t" + m_help_message; 00094 } // arguments_table::argument_attributes::format_long_help() 00095 00096 /*----------------------------------------------------------------------------*/ 00100 const std::string& claw::arguments_table::argument_attributes::get_name() const 00101 { 00102 return m_name; 00103 } // arguments_table::argument_attributes::get_name() 00104 00105 /*----------------------------------------------------------------------------*/ 00109 const std::string& 00110 claw::arguments_table::argument_attributes::get_second_name() const 00111 { 00112 return m_second_name; 00113 } // arguments_table::argument_attributes::get_second_name() 00114 00115 /*----------------------------------------------------------------------------*/ 00119 bool claw::arguments_table::argument_attributes::is_optional() const 00120 { 00121 return m_optional; 00122 } // arguments_table::argument_attributes::is_optional() 00123 00124 00125 00126 00127 /*----------------------------------------------------------------------------*/ 00132 claw::arguments_table::arguments_table( const std::string& prog_name ) 00133 : m_arguments(prog_name) 00134 { 00135 00136 } // arguments_table::arguments_table() 00137 00138 /*----------------------------------------------------------------------------*/ 00146 claw::arguments_table::arguments_table( int& argc, char** &argv ) 00147 : m_arguments(argc, argv, claw::math::ordered_set<std::string>() ) 00148 { 00149 00150 } // arguments_table::arguments_table() 00151 00152 /*----------------------------------------------------------------------------*/ 00161 void claw::arguments_table::add( const std::string& short_name, 00162 const std::string& long_name, 00163 const std::string& help_msg, bool optional, 00164 const std::string& val_name ) 00165 { 00166 m_short_arguments.insert( argument_attributes(short_name, long_name, help_msg, 00167 optional, val_name) ); 00168 m_long_arguments.insert( argument_attributes(long_name, short_name, help_msg, 00169 optional, val_name) ); 00170 } // arguments_table::add() 00171 00172 /*----------------------------------------------------------------------------*/ 00180 void claw::arguments_table::add_long( const std::string& long_name, 00181 const std::string& help_msg, 00182 bool optional, 00183 const std::string& val_name ) 00184 { 00185 m_long_arguments.insert( argument_attributes(long_name, "", help_msg, 00186 optional, val_name) ); 00187 } // arguments_table::add_long() 00188 00189 /*----------------------------------------------------------------------------*/ 00197 void claw::arguments_table::add_short( const std::string& short_name, 00198 const std::string& help_msg, 00199 bool optional, 00200 const std::string& val_name ) 00201 { 00202 m_short_arguments.insert( argument_attributes(short_name, "", help_msg, 00203 optional, val_name) ); 00204 } // arguments_table::add_short() 00205 00206 /*----------------------------------------------------------------------------*/ 00214 void claw::arguments_table::parse( int& argc, char** &argv ) 00215 { 00216 math::ordered_set<std::string> allowed; 00217 math::ordered_set<argument_attributes>::const_iterator it; 00218 00219 for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it) 00220 allowed.insert(it->get_name()); 00221 00222 for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it) 00223 allowed.insert(it->get_name()); 00224 00225 m_arguments.parse( argc, argv, allowed ); 00226 } // arguments_table::parse() 00227 00228 /*----------------------------------------------------------------------------*/ 00239 void claw::arguments_table::help( const std::string& free_args ) const 00240 { 00241 std::cout << m_arguments.get_program_name(); 00242 00243 typedef math::ordered_set<argument_attributes>::const_iterator set_iterator; 00244 00245 std::list<set_iterator> optional; 00246 std::list<set_iterator>::const_iterator it_opt; 00247 set_iterator it; 00248 00249 for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it) 00250 if ( it->is_optional() ) 00251 optional.push_back(it); 00252 else 00253 std::cout << ' ' << it->format_short_help(); 00254 00255 for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it) 00256 if (it->get_second_name().empty()) 00257 { 00258 if ( it->is_optional() ) 00259 optional.push_back(it); 00260 else 00261 std::cout << ' ' << it->format_short_help(); 00262 } 00263 00264 for (it_opt=optional.begin(); it_opt!=optional.end(); ++it_opt) 00265 std::cout << ' ' << (*it_opt)->format_short_help(); 00266 00267 if ( !free_args.empty() ) 00268 std::cout << ' ' << free_args; 00269 00270 std::cout << "\n\n"; 00271 00272 for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it) 00273 std::cout << "\t" << it->format_long_help() << std::endl; 00274 00275 for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it) 00276 if (it->get_second_name().empty()) 00277 std::cout << "\t" << it->format_long_help() << std::endl; 00278 } // arguments_table::help() 00279 00280 /*----------------------------------------------------------------------------*/ 00289 bool claw::arguments_table::required_fields_are_set() const 00290 { 00291 bool ok = true; 00292 math::ordered_set<argument_attributes>::const_iterator it; 00293 00294 for (it=m_short_arguments.begin(); (it!=m_short_arguments.end()) && ok; ++it) 00295 if ( !it->is_optional() ) 00296 ok = ok && has_value(it->get_name()); 00297 00298 for (it=m_long_arguments.begin(); (it!=m_long_arguments.end()) && ok; ++it) 00299 if ( !it->is_optional() ) 00300 ok = ok && has_value(it->get_name()); 00301 00302 return ok; 00303 } // arguments_table::required_fields_are_set() 00304 00305 /*----------------------------------------------------------------------------*/ 00310 bool claw::arguments_table::has_value( const std::string& arg_name ) const 00311 { 00312 bool result = false; 00313 std::string short_name, long_name; 00314 00315 get_argument_names( arg_name, short_name, long_name ); 00316 00317 if ( !short_name.empty() ) 00318 result = m_arguments.has_value(short_name); 00319 00320 if (!result) 00321 if ( !long_name.empty() ) 00322 result = m_arguments.has_value(long_name); 00323 00324 return result; 00325 } // arguments_table::has_value() 00326 00327 /*----------------------------------------------------------------------------*/ 00332 bool 00333 claw::arguments_table::only_integer_values( const std::string& arg_name ) const 00334 { 00335 bool result = true; 00336 std::string short_name, long_name; 00337 00338 get_argument_names( arg_name, short_name, long_name ); 00339 00340 if ( short_name.empty() && long_name.empty() ) 00341 result = false; 00342 else 00343 { 00344 if ( !short_name.empty() ) 00345 result = m_arguments.only_integer_values(short_name); 00346 00347 if ( !long_name.empty() ) 00348 result = result && m_arguments.only_integer_values(long_name); 00349 } 00350 00351 return result; 00352 } // arguments_table::only_integer_values() 00353 00354 /*----------------------------------------------------------------------------*/ 00359 bool 00360 claw::arguments_table::only_real_values( const std::string& arg_name ) const 00361 { 00362 bool result = true; 00363 std::string short_name, long_name; 00364 00365 get_argument_names( arg_name, short_name, long_name ); 00366 00367 if ( short_name.empty() && long_name.empty() ) 00368 result = false; 00369 else 00370 { 00371 if ( !short_name.empty() ) 00372 result = m_arguments.only_real_values(short_name); 00373 00374 if ( !long_name.empty() ) 00375 result = result && m_arguments.only_real_values(long_name); 00376 } 00377 00378 return result; 00379 } // arguments_table::only_real_values() 00380 00381 /*----------------------------------------------------------------------------*/ 00385 const std::string& claw::arguments_table::get_program_name() const 00386 { 00387 return m_arguments.get_program_name(); 00388 } // arguments_table::has_value() 00389 00390 /*----------------------------------------------------------------------------*/ 00395 bool claw::arguments_table::get_bool( const std::string& arg_name ) const 00396 { 00397 std::string short_name, long_name; 00398 00399 get_argument_names( arg_name, short_name, long_name ); 00400 00401 return m_arguments.get_bool(short_name) || m_arguments.get_bool(long_name); 00402 } // arguments_table::get_bool() 00403 00404 /*----------------------------------------------------------------------------*/ 00410 int claw::arguments_table::get_integer( const std::string& arg_name ) const 00411 { 00412 CLAW_PRECOND( has_value(arg_name) ); 00413 00414 std::string short_name, long_name; 00415 00416 get_argument_names( arg_name, short_name, long_name ); 00417 00418 if ( m_arguments.has_value(short_name) ) 00419 return m_arguments.get_integer(short_name); 00420 else 00421 return m_arguments.get_integer(long_name); 00422 } // arguments_table::get_integer() 00423 00424 /*----------------------------------------------------------------------------*/ 00430 double claw::arguments_table::get_real( const std::string& arg_name ) const 00431 { 00432 CLAW_PRECOND( has_value(arg_name) ); 00433 00434 std::string short_name, long_name; 00435 00436 get_argument_names( arg_name, short_name, long_name ); 00437 00438 if ( m_arguments.has_value(short_name) ) 00439 return m_arguments.get_real(short_name); 00440 else 00441 return m_arguments.get_real(long_name); 00442 } // arguments_table::get_real() 00443 00444 /*----------------------------------------------------------------------------*/ 00450 const std::string& 00451 claw::arguments_table::get_string( const std::string& arg_name ) const 00452 { 00453 CLAW_PRECOND( has_value(arg_name) ); 00454 00455 std::string short_name, long_name; 00456 00457 get_argument_names( arg_name, short_name, long_name ); 00458 00459 if ( m_arguments.has_value(short_name) ) 00460 return m_arguments.get_string(short_name); 00461 else 00462 return m_arguments.get_string(long_name); 00463 } // arguments_table::get_string() 00464 00465 /*----------------------------------------------------------------------------*/ 00470 std::list<int> 00471 claw::arguments_table::get_all_of_integer( const std::string& arg_name ) const 00472 { 00473 std::list<int> result; 00474 std::string short_name, long_name; 00475 00476 get_argument_names( arg_name, short_name, long_name ); 00477 00478 if ( !short_name.empty() ) 00479 result = m_arguments.get_all_of_integer(short_name); 00480 00481 if ( !long_name.empty() ) 00482 { 00483 const std::list<int> p(m_arguments.get_all_of_integer(long_name)); 00484 result.insert( result.end(), p.begin(), p.end() ); 00485 } 00486 00487 return result; 00488 } // arguments_table::get_all_of_integer() 00489 00490 /*----------------------------------------------------------------------------*/ 00495 std::list<double> 00496 claw::arguments_table::get_all_of_real( const std::string& arg_name ) const 00497 { 00498 std::list<double> result; 00499 std::string short_name, long_name; 00500 00501 get_argument_names( arg_name, short_name, long_name ); 00502 00503 if ( !short_name.empty() ) 00504 result = m_arguments.get_all_of_real(short_name); 00505 00506 if ( !long_name.empty() ) 00507 { 00508 const std::list<double> p(m_arguments.get_all_of_real(long_name)); 00509 result.insert( result.end(), p.begin(), p.end() ); 00510 } 00511 00512 return result; 00513 } // arguments_table::get_all_of_real() 00514 00515 /*----------------------------------------------------------------------------*/ 00520 std::list<std::string> 00521 claw::arguments_table::get_all_of_string( const std::string& arg_name ) const 00522 { 00523 std::list<std::string> result; 00524 std::string short_name, long_name; 00525 00526 get_argument_names( arg_name, short_name, long_name ); 00527 00528 if ( !short_name.empty() ) 00529 result = m_arguments.get_all_of_string(short_name); 00530 00531 if ( !long_name.empty() ) 00532 { 00533 const std::list<std::string> p(m_arguments.get_all_of_string(long_name)); 00534 result.insert( result.end(), p.begin(), p.end() ); 00535 } 00536 00537 return result; 00538 } // arguments_table::get_all_of_string() 00539 00540 /*----------------------------------------------------------------------------*/ 00550 void claw::arguments_table::add_argument( const std::string& arg ) 00551 { 00552 m_arguments.add_argument( arg ); 00553 } // arguments_table::add_argument() 00554 00555 /*----------------------------------------------------------------------------*/ 00562 void claw::arguments_table::get_argument_names 00563 ( const std::string& arg_name, std::string& short_name, 00564 std::string& long_name ) const 00565 { 00566 argument_attributes attr(arg_name, "", "", false, ""); 00567 math::ordered_set<argument_attributes>::const_iterator it; 00568 00569 // if arg_name is short, try to find the long version 00570 it = m_short_arguments.find( attr ); 00571 00572 if (it != m_short_arguments.end()) 00573 { 00574 short_name = arg_name; 00575 long_name = it->get_second_name(); 00576 } 00577 else 00578 { 00579 // if arg_name is long, try to find the short version 00580 it = m_long_arguments.find( attr ); 00581 00582 if (it != m_long_arguments.end()) 00583 { 00584 short_name = it->get_second_name(); 00585 long_name = arg_name; 00586 } 00587 } 00588 } // arguments_table::get_argument_names()