claw::arguments_table Class Reference

#include <arguments_table.hpp>

List of all members.


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.


Public Member Functions

 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.
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.
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.

Classes

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

Constructor & Destructor Documentation

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 135 of file arguments_table.cpp.

00136   : m_arguments(argc, argv)
00137 {
00138 
00139 } // 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 150 of file arguments_table.cpp.

References m_long_arguments, and m_short_arguments.

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 } // arguments_table::add()

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 169 of file arguments_table.cpp.

References m_long_arguments.

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

00173 {
00174   m_long_arguments.insert( argument_attributes(long_name, "", help_msg,
00175                                                optional, val_name) );
00176 } // 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 186 of file arguments_table.cpp.

References m_short_arguments.

00190 {
00191   m_short_arguments.insert( argument_attributes(short_name, "", help_msg,
00192                                                 optional, val_name) );
00193 } // arguments_table::add_short()

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 203 of file arguments_table.cpp.

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

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 } // arguments_table::parse()

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 228 of file arguments_table.cpp.

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

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 } // arguments_table::help()

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 276 of file arguments_table.cpp.

References has_value(), m_long_arguments, and m_short_arguments.

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 } // arguments_table::required_fields_are_set()

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

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 } // arguments_table::has_value()

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

Get the name of the program.

Definition at line 318 of file arguments_table.cpp.

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

00319 {
00320   return m_arguments.get_program_name();
00321 } // arguments_table::has_value()

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 328 of file arguments_table.cpp.

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

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 } // 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 343 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().

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 } // arguments_table::get_integer()

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 363 of file arguments_table.cpp.

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

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 } // 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 384 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().

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 } // arguments_table::get_string()

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 408 of file arguments_table.cpp.

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

00409 {
00410   m_arguments.add_argument( arg );
00411 } // arguments_table::add_argument()

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 421 of file arguments_table.cpp.

References m_long_arguments, and m_short_arguments.

Referenced by get_bool(), get_integer(), get_real(), get_string(), and has_value().

00423 {
00424   argument_attributes attr(arg_name, "", "", false, "");
00425   math::ordered_set<argument_attributes>::const_iterator it;
00426 
00427   // if arg_name is short, try to find the long version
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       // if arg_name is long, try to find the short version
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 } // arguments_table::get_argument_names()


Member Data Documentation

The class that will store arguments values.

Definition at line 126 of file arguments_table.hpp.

Referenced by add_argument(), get_bool(), get_integer(), get_program_name(), get_real(), get_string(), has_value(), help(), and parse().

The arguments with a short version.

Definition at line 129 of file arguments_table.hpp.

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

The arguments with a long version.

Definition at line 132 of file arguments_table.hpp.

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


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

Generated on Thu Jun 26 09:35:05 2008 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.5.6