claw::arguments_table Class Reference

A class to manage the arguments of your program, with automatic management of short/long arguments and help message. More...

#include <arguments_table.hpp>

List of all members.

Classes

class  argument_attributes
 This class manage the description of an argument. More...

Public Member Functions

 arguments_table (const std::string &prog_name)
 Constructor.
 arguments_table (int &argc, char **&argv)
 Constructor.
void add (const std::string &short_name, const std::string &long_name, const std::string &help_msg="", bool optional=false, const std::string &val_name="")
 Add an argument in the table.
void add_long (const std::string &long_name, const std::string &help_msg="", bool optional=false, const std::string &val_name="")
 Add an argument in the table.
void add_short (const std::string &short_name, const std::string &help_msg="", bool optional=false, const std::string &val_name="")
 Add an argument in the table.
void parse (int &argc, char **&argv)
 Parse the command line arguments.
void help (const std::string &free_args="") const
 Print some help about the arguments.
bool required_fields_are_set () const
 Tell if all arguments not marqued as "optional" have been specified in the command line.
bool has_value (const std::string &arg_name) const
 Tell if an argument has a value.
bool only_integer_values (const std::string &arg_name) const
 Tell if only integer values are associated to an argument.
bool only_real_values (const std::string &arg_name) const
 Tell if only real values are associated to an argument.
const std::string & get_program_name () const
 Get the name of the program.
bool get_bool (const std::string &arg_name) const
 Get the boolean state of an argument.
int get_integer (const std::string &arg_name) const
 Get the integer value of an argument.
double get_real (const std::string &arg_name) const
 Get the real value of an argument.
const std::string & get_string (const std::string &arg_name) const
 Get the string value of an argument.
std::list< int > get_all_of_integer (const std::string &arg_name) const
 Get all integer values of an argument.
std::list< double > get_all_of_real (const std::string &arg_name) const
 Get all real values of an argument.
std::list< std::string > get_all_of_string (const std::string &arg_name) const
 Get all string values of an argument.
void add_argument (const std::string &arg)
 Add an argument in our list.

Private Member Functions

void get_argument_names (const std::string &arg_name, std::string &short_name, std::string &long_name) const
 Get the principal name and the second name of an argument.

Private Attributes

arguments m_arguments
 The class that will store arguments values.
math::ordered_set
< argument_attributes
m_short_arguments
 The arguments with a short version.
math::ordered_set
< argument_attributes
m_long_arguments
 The arguments with a long version.

Detailed Description

A class to manage the arguments of your program, with automatic management of short/long arguments and help message.

Remarks:
None of those methods is allowed to use claw::logger because when we are processing the arguments, we are at the really begining of the program and claw::logger is probably not initialised.
Author:
Julien Jorge

Definition at line 48 of file arguments_table.hpp.


Constructor & Destructor Documentation

claw::arguments_table::arguments_table ( const std::string &  prog_name  )  [explicit]

Constructor.

Parameters:
prog_name Force the name of the program.

Definition at line 132 of file arguments_table.cpp.

00133   : m_arguments(prog_name)
00134 {
00135 
00136 } // arguments_table::arguments_table()

claw::arguments_table::arguments_table ( int &  argc,
char **&  argv 
)

Constructor.

Parameters:
argc Number of arguments.
argv Arguments.

All supported arguments will be removed from argv.

Definition at line 146 of file arguments_table.cpp.

00147   : m_arguments(argc, argv, claw::math::ordered_set<std::string>() )
00148 {
00149 
00150 } // arguments_table::arguments_table()


Member Function Documentation

void claw::arguments_table::add ( const std::string &  short_name,
const std::string &  long_name,
const std::string &  help_msg = "",
bool  optional = false,
const std::string &  val_name = "" 
)

Add an argument in the table.

Parameters:
short_name The short name of the argument.
long_name The long name of the argument.
help_msg A description of the argument.
optional Tell if the argument is optional.
val_name The type of the value needed for this argument.

Definition at line 161 of file arguments_table.cpp.

References claw::avl< K, Comp >::insert(), m_long_arguments, and m_short_arguments.

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()

void claw::arguments_table::add_argument ( const std::string &  arg  ) 

Add an argument in our list.

You can use this method to set default values to the parameters of your program, before calling parse.

Parameters:
arg The argument to add.
Precondition:
(arg != "--") && (arg[0] == '-')

Definition at line 550 of file arguments_table.cpp.

References claw::arguments::add_argument(), and m_arguments.

00551 {
00552   m_arguments.add_argument( arg );
00553 } // arguments_table::add_argument()

void claw::arguments_table::add_long ( const std::string &  long_name,
const std::string &  help_msg = "",
bool  optional = false,
const std::string &  val_name = "" 
)

Add an argument in the table.

Parameters:
long_name The long name of the argument.
help_msg A description of the argument.
optional Tell if the argument is optional.
val_name The type of the value needed for this argument.

Definition at line 180 of file arguments_table.cpp.

References claw::avl< K, Comp >::insert(), and m_long_arguments.

Referenced by claw::application::application().

00184 {
00185   m_long_arguments.insert( argument_attributes(long_name, "", help_msg,
00186                                                optional, val_name) );
00187 } // arguments_table::add_long()

void claw::arguments_table::add_short ( const std::string &  short_name,
const std::string &  help_msg = "",
bool  optional = false,
const std::string &  val_name = "" 
)

Add an argument in the table.

Parameters:
short_name The short name of the argument.
help_msg A description of the argument.
optional Tell if the argument is optional.
val_name The type of the value needed for this argument.

Definition at line 197 of file arguments_table.cpp.

References claw::avl< K, Comp >::insert(), and m_short_arguments.

00201 {
00202   m_short_arguments.insert( argument_attributes(short_name, "", help_msg,
00203                                                 optional, val_name) );
00204 } // arguments_table::add_short()

std::list< int > claw::arguments_table::get_all_of_integer ( const std::string &  arg_name  )  const

Get all integer values of an argument.

Parameters:
arg_name The name of the argument to get.

Definition at line 471 of file arguments_table.cpp.

References claw::arguments::get_all_of_integer(), get_argument_names(), and m_arguments.

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()

std::list< double > claw::arguments_table::get_all_of_real ( const std::string &  arg_name  )  const

Get all real values of an argument.

Parameters:
arg_name The name of the argument to get.

Definition at line 496 of file arguments_table.cpp.

References claw::arguments::get_all_of_real(), get_argument_names(), and m_arguments.

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()

std::list< std::string > claw::arguments_table::get_all_of_string ( const std::string &  arg_name  )  const

Get all string values of an argument.

Parameters:
arg_name The name of the argument to get.

Definition at line 521 of file arguments_table.cpp.

References claw::arguments::get_all_of_string(), get_argument_names(), and m_arguments.

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()

void claw::arguments_table::get_argument_names ( const std::string &  arg_name,
std::string &  short_name,
std::string &  long_name 
) const [private]

Get the principal name and the second name of an argument.

Parameters:
arg_name The name of the argument to find.
short_name The short name of the argument.
long_name The long name of the argument.

Definition at line 563 of file arguments_table.cpp.

Referenced by get_all_of_integer(), get_all_of_real(), get_all_of_string(), get_bool(), get_integer(), get_real(), get_string(), has_value(), only_integer_values(), and only_real_values().

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()

bool claw::arguments_table::get_bool ( const std::string &  arg_name  )  const

Get the boolean state of an argument.

Parameters:
arg_name The name of the argument to find.

Definition at line 395 of file arguments_table.cpp.

References get_argument_names(), claw::arguments::get_bool(), and m_arguments.

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()

int claw::arguments_table::get_integer ( const std::string &  arg_name  )  const

Get the integer value of an argument.

Parameters:
arg_name The name of the argument to find.
Precondition:
has_value(arg_name)

Definition at line 410 of file arguments_table.cpp.

References CLAW_PRECOND, get_argument_names(), claw::arguments::get_integer(), claw::arguments::has_value(), has_value(), and m_arguments.

Referenced by claw::application::application().

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()

const std::string & claw::arguments_table::get_program_name (  )  const

Get the name of the program.

Definition at line 385 of file arguments_table.cpp.

References claw::arguments::get_program_name(), and m_arguments.

00386 {
00387   return m_arguments.get_program_name();
00388 } // arguments_table::has_value()

double claw::arguments_table::get_real ( const std::string &  arg_name  )  const

Get the real value of an argument.

Parameters:
arg_name The name of the argument to find.
Precondition:
has_value(arg_name)

Definition at line 430 of file arguments_table.cpp.

References CLAW_PRECOND, get_argument_names(), claw::arguments::get_real(), claw::arguments::has_value(), has_value(), and m_arguments.

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()

const std::string & claw::arguments_table::get_string ( const std::string &  arg_name  )  const

Get the string value of an argument.

Parameters:
arg_name The name of the argument to find.
Precondition:
has_value(arg_name)

Definition at line 451 of file arguments_table.cpp.

References CLAW_PRECOND, get_argument_names(), claw::arguments::get_string(), claw::arguments::has_value(), has_value(), and m_arguments.

Referenced by claw::application::application().

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()

bool claw::arguments_table::has_value ( const std::string &  arg_name  )  const

Tell if an argument has a value.

Parameters:
arg_name The name of the argument to find.

Definition at line 310 of file arguments_table.cpp.

References get_argument_names(), claw::arguments::has_value(), and m_arguments.

Referenced by claw::application::application(), get_integer(), get_real(), get_string(), and required_fields_are_set().

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()

void claw::arguments_table::help ( const std::string &  free_args = ""  )  const

Print some help about the arguments.

Parameters:
free_args The arguments of your program that are not managed by claw::arguments_table.

The method prints the name of the program, required arguments, optional arguments and, then, free_args. Arguments are printed in short format when available. A line is then skipped and the long description of the arguments is printed.

Definition at line 239 of file arguments_table.cpp.

References claw::avl< K, Comp >::begin(), claw::avl< K, Comp >::end(), std::endl(), claw::arguments::get_program_name(), m_arguments, m_long_arguments, and m_short_arguments.

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()

bool claw::arguments_table::only_integer_values ( const std::string &  arg_name  )  const

Tell if only integer values are associated to an argument.

Parameters:
arg_name The name of the argument to test.

Definition at line 333 of file arguments_table.cpp.

References get_argument_names(), m_arguments, and claw::arguments::only_integer_values().

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()

bool claw::arguments_table::only_real_values ( const std::string &  arg_name  )  const

Tell if only real values are associated to an argument.

Parameters:
arg_name The name of the argument to test.

Definition at line 360 of file arguments_table.cpp.

References get_argument_names(), m_arguments, and claw::arguments::only_real_values().

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()

void claw::arguments_table::parse ( int &  argc,
char **&  argv 
)

Parse the command line arguments.

Parameters:
argc Number of arguments.
argv Arguments.

All supported arguments will be removed from argv.

Definition at line 214 of file arguments_table.cpp.

References claw::avl< K, Comp >::begin(), claw::avl< K, Comp >::end(), claw::avl< K, Comp >::insert(), m_arguments, m_long_arguments, m_short_arguments, and claw::arguments::parse().

Referenced by claw::application::application().

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()

bool claw::arguments_table::required_fields_are_set (  )  const

Tell if all arguments not marqued as "optional" have been specified in the command line.

Remarks:
The method doesn't check the value of the arguments. If, for example, an argument needs to be an integer and the user use it as a boolean, the method will return true.

Definition at line 289 of file arguments_table.cpp.

References claw::avl< K, Comp >::begin(), claw::avl< K, Comp >::end(), has_value(), m_long_arguments, and m_short_arguments.

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()


Member Data Documentation

The arguments with a long version.

Definition at line 140 of file arguments_table.hpp.

Referenced by add(), add_long(), help(), parse(), and required_fields_are_set().

The arguments with a short version.

Definition at line 137 of file arguments_table.hpp.

Referenced by add(), add_short(), help(), parse(), and required_fields_are_set().


The documentation for this class was generated from the following files:

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1