00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 }
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 }
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 != "")
00074 result += "=" + m_value_type;
00075
00076 if (m_optional)
00077 return "[" + result + "]";
00078 else
00079 return result;
00080 }
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 != "")
00091 result += ", " + m_second_name;
00092
00093 return result + "\t" + m_help_message;
00094 }
00095
00096
00100 const std::string& claw::arguments_table::argument_attributes::get_name() const
00101 {
00102 return m_name;
00103 }
00104
00105
00109 const std::string&
00110 claw::arguments_table::argument_attributes::get_second_name() const
00111 {
00112 return m_second_name;
00113 }
00114
00115
00119 bool claw::arguments_table::argument_attributes::is_optional() const
00120 {
00121 return m_optional;
00122 }
00123
00124
00125
00126
00127
00135 claw::arguments_table::arguments_table( int& argc, char** &argv )
00136 : m_arguments(argc, argv)
00137 {
00138
00139 }
00140
00141
00150 void claw::arguments_table::add( const std::string& short_name,
00151 const std::string& long_name,
00152 const std::string& help_msg, bool optional,
00153 const std::string& val_name )
00154 {
00155 m_short_arguments.insert( argument_attributes(short_name, long_name, help_msg,
00156 optional, val_name) );
00157 m_long_arguments.insert( argument_attributes(long_name, short_name, help_msg,
00158 optional, val_name) );
00159 }
00160
00161
00169 void claw::arguments_table::add_long( const std::string& long_name,
00170 const std::string& help_msg,
00171 bool optional,
00172 const std::string& val_name )
00173 {
00174 m_long_arguments.insert( argument_attributes(long_name, "", help_msg,
00175 optional, val_name) );
00176 }
00177
00178
00186 void claw::arguments_table::add_short( const std::string& short_name,
00187 const std::string& help_msg,
00188 bool optional,
00189 const std::string& val_name )
00190 {
00191 m_short_arguments.insert( argument_attributes(short_name, "", help_msg,
00192 optional, val_name) );
00193 }
00194
00195
00203 void claw::arguments_table::parse( int& argc, char** &argv )
00204 {
00205 math::ordered_set<std::string> allowed;
00206 math::ordered_set<argument_attributes>::const_iterator it;
00207
00208 for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
00209 allowed.insert(it->get_name());
00210
00211 for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
00212 allowed.insert(it->get_name());
00213
00214 m_arguments.parse( argc, argv, allowed );
00215 }
00216
00217
00228 void claw::arguments_table::help( const std::string& free_args ) const
00229 {
00230 std::cout << m_arguments.get_program_name();
00231
00232 std::list<argument_attributes> optional;
00233 std::list<argument_attributes>::const_iterator it_opt;
00234 math::ordered_set<argument_attributes>::const_iterator it;
00235
00236 for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
00237 if ( it->is_optional() )
00238 optional.push_back(*it);
00239 else
00240 std::cout << " " << it->format_short_help();
00241
00242 for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
00243 if (it->get_second_name() == "")
00244 {
00245 if ( it->is_optional() )
00246 optional.push_back(*it);
00247 else
00248 std::cout << " " << it->format_short_help();
00249 }
00250
00251 for (it_opt=optional.begin(); it_opt!=optional.end(); ++it_opt)
00252 std::cout << " " << it_opt->format_short_help();
00253
00254 if ( free_args != "" )
00255 std::cout << " " << free_args;
00256
00257 std::cout << "\n\n";
00258
00259 for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
00260 std::cout << "\t" << it->format_long_help() << std::endl;
00261
00262 for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
00263 if (it->get_second_name() == "")
00264 std::cout << "\t" << it->format_long_help() << std::endl;
00265 }
00266
00267
00276 bool claw::arguments_table::required_fields_are_set() const
00277 {
00278 bool ok = true;
00279 math::ordered_set<argument_attributes>::const_iterator it;
00280
00281 for (it=m_short_arguments.begin(); (it!=m_short_arguments.end()) && ok; ++it)
00282 if ( !it->is_optional() )
00283 ok = ok && has_value(it->get_name());
00284
00285 for (it=m_long_arguments.begin(); (it!=m_long_arguments.end()) && ok; ++it)
00286 if ( !it->is_optional() )
00287 ok = ok && has_value(it->get_name());
00288
00289 return ok;
00290 }
00291
00292
00297 bool claw::arguments_table::has_value( const std::string& arg_name ) const
00298 {
00299 bool result = false;
00300 std::string short_name, long_name;
00301
00302 get_argument_names( arg_name, short_name, long_name );
00303
00304 if ( short_name != "" )
00305 result = m_arguments.has_value(short_name);
00306
00307 if (!result)
00308 if ( long_name != "" )
00309 result = m_arguments.has_value(long_name);
00310
00311 return result;
00312 }
00313
00314
00318 const std::string& claw::arguments_table::get_program_name() const
00319 {
00320 return m_arguments.get_program_name();
00321 }
00322
00323
00328 bool claw::arguments_table::get_bool( const std::string& arg_name ) const
00329 {
00330 std::string short_name, long_name;
00331
00332 get_argument_names( arg_name, short_name, long_name );
00333
00334 return m_arguments.get_bool(short_name) || m_arguments.get_bool(long_name);
00335 }
00336
00337
00343 int claw::arguments_table::get_integer( const std::string& arg_name ) const
00344 {
00345 CLAW_PRECOND( has_value(arg_name) );
00346
00347 std::string short_name, long_name;
00348
00349 get_argument_names( arg_name, short_name, long_name );
00350
00351 if ( m_arguments.has_value(short_name) )
00352 return m_arguments.get_integer(short_name);
00353 else
00354 return m_arguments.get_integer(long_name);
00355 }
00356
00357
00363 double claw::arguments_table::get_real( const std::string& arg_name ) const
00364 {
00365 CLAW_PRECOND( has_value(arg_name) );
00366
00367 std::string short_name, long_name;
00368
00369 get_argument_names( arg_name, short_name, long_name );
00370
00371 if ( m_arguments.has_value(short_name) )
00372 return m_arguments.get_real(short_name);
00373 else
00374 return m_arguments.get_real(long_name);
00375 }
00376
00377
00383 const std::string&
00384 claw::arguments_table::get_string( const std::string& arg_name ) const
00385 {
00386 CLAW_PRECOND( has_value(arg_name) );
00387
00388 std::string short_name, long_name;
00389
00390 get_argument_names( arg_name, short_name, long_name );
00391
00392 if ( m_arguments.has_value(short_name) )
00393 return m_arguments.get_string(short_name);
00394 else
00395 return m_arguments.get_string(long_name);
00396 }
00397
00398
00408 void claw::arguments_table::add_argument( const std::string& arg )
00409 {
00410 m_arguments.add_argument( arg );
00411 }
00412
00413
00420 void claw::arguments_table::get_argument_names
00421 ( const std::string& arg_name, std::string& short_name,
00422 std::string& long_name ) const
00423 {
00424 argument_attributes attr(arg_name, "", "", false, "");
00425 math::ordered_set<argument_attributes>::const_iterator it;
00426
00427
00428 it = m_short_arguments.find( attr );
00429
00430 if (it != m_short_arguments.end())
00431 {
00432 short_name = arg_name;
00433 long_name = it->get_second_name();
00434 }
00435 else
00436 {
00437
00438 it = m_long_arguments.find( attr );
00439
00440 if (it != m_long_arguments.end())
00441 {
00442 short_name = it->get_second_name();
00443 long_name = arg_name;
00444 }
00445 }
00446 }