Classes | Public Member Functions | Private Member Functions | Private Attributes

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_nameForce the name of the program.

Definition at line 132 of file arguments_table.cpp.

  : m_arguments(prog_name)
{

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

Constructor.

Parameters:
argcNumber of arguments.
argvArguments.

All supported arguments will be removed from argv.

Definition at line 146 of file arguments_table.cpp.

  : m_arguments(argc, argv, claw::math::ordered_set<std::string>() )
{

} // 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_nameThe short name of the argument.
long_nameThe long name of the argument.
help_msgA description of the argument.
optionalTell if the argument is optional.
val_nameThe type of the value needed for this argument.

Definition at line 161 of file arguments_table.cpp.

{
  m_short_arguments.insert( argument_attributes(short_name, long_name, help_msg,
                                                optional, val_name) );
  m_long_arguments.insert( argument_attributes(long_name, short_name, help_msg,
                                               optional, val_name) );
} // 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:
argThe argument to add.
Precondition:
(arg != "--") && (arg[0] == '-')

Definition at line 550 of file arguments_table.cpp.

{
  m_arguments.add_argument( arg );
} // 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_nameThe long name of the argument.
help_msgA description of the argument.
optionalTell if the argument is optional.
val_nameThe type of the value needed for this argument.

Definition at line 180 of file arguments_table.cpp.

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

{
  m_long_arguments.insert( argument_attributes(long_name, "", help_msg,
                                               optional, val_name) );
} // 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_nameThe short name of the argument.
help_msgA description of the argument.
optionalTell if the argument is optional.
val_nameThe type of the value needed for this argument.

Definition at line 197 of file arguments_table.cpp.

{
  m_short_arguments.insert( argument_attributes(short_name, "", help_msg,
                                                optional, val_name) );
} // 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_nameThe name of the argument to get.

Definition at line 471 of file arguments_table.cpp.

{
  std::list<int> result;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( !short_name.empty() )
    result = m_arguments.get_all_of_integer(short_name);

  if ( !long_name.empty() )
    {
      const std::list<int> p(m_arguments.get_all_of_integer(long_name));
      result.insert( result.end(), p.begin(), p.end() );
    }

  return result;
} // 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_nameThe name of the argument to get.

Definition at line 496 of file arguments_table.cpp.

{
  std::list<double> result;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( !short_name.empty() )
    result = m_arguments.get_all_of_real(short_name);

  if ( !long_name.empty() )
    {
      const std::list<double> p(m_arguments.get_all_of_real(long_name));
      result.insert( result.end(), p.begin(), p.end() );
    }

  return result;
} // 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_nameThe name of the argument to get.

Definition at line 521 of file arguments_table.cpp.

{
  std::list<std::string> result;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( !short_name.empty() )
    result = m_arguments.get_all_of_string(short_name);

  if ( !long_name.empty() )
    {
      const std::list<std::string> p(m_arguments.get_all_of_string(long_name));
      result.insert( result.end(), p.begin(), p.end() );
    }

  return result;
} // 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_nameThe name of the argument to find.
short_nameThe short name of the argument.
long_nameThe long name of the argument.

Definition at line 563 of file arguments_table.cpp.

{
  argument_attributes attr(arg_name, "", "", false, "");
  math::ordered_set<argument_attributes>::const_iterator it;

  // if arg_name is short, try to find the long version
  it = m_short_arguments.find( attr );

  if (it != m_short_arguments.end())
    {
      short_name = arg_name;
      long_name = it->get_second_name();
    }
  else
    {
      // if arg_name is long, try to find the short version
      it = m_long_arguments.find( attr );
          
      if (it != m_long_arguments.end())
        {
          short_name = it->get_second_name();
          long_name = arg_name;
        }
    }
} // 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_nameThe name of the argument to find.

Definition at line 395 of file arguments_table.cpp.

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

{
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  return m_arguments.get_bool(short_name) || m_arguments.get_bool(long_name);
} // 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_nameThe name of the argument to find.
Precondition:
has_value(arg_name)

Definition at line 410 of file arguments_table.cpp.

References CLAW_PRECOND.

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

{
  CLAW_PRECOND( has_value(arg_name) );

  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( m_arguments.has_value(short_name) )
    return m_arguments.get_integer(short_name);
  else
    return m_arguments.get_integer(long_name);
} // 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.

{
  return m_arguments.get_program_name();
} // 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_nameThe name of the argument to find.
Precondition:
has_value(arg_name)

Definition at line 430 of file arguments_table.cpp.

References CLAW_PRECOND.

{
  CLAW_PRECOND( has_value(arg_name) );

  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( m_arguments.has_value(short_name) )
    return m_arguments.get_real(short_name);
  else
    return m_arguments.get_real(long_name);
} // 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_nameThe name of the argument to find.
Precondition:
has_value(arg_name)

Definition at line 451 of file arguments_table.cpp.

References CLAW_PRECOND.

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

{
  CLAW_PRECOND( has_value(arg_name) );

  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( m_arguments.has_value(short_name) )
    return m_arguments.get_string(short_name);
  else
    return m_arguments.get_string(long_name);
} // 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_nameThe name of the argument to find.

Definition at line 310 of file arguments_table.cpp.

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

{
  bool result = false;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( !short_name.empty() )
    result = m_arguments.has_value(short_name);

  if (!result)
    if ( !long_name.empty() )
      result = m_arguments.has_value(long_name);

  return result;
} // arguments_table::has_value()
void claw::arguments_table::help ( const std::string &  free_args = "" ) const

Print some help about the arguments.

Parameters:
free_argsThe 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 std::endl().

{
  std::cout << m_arguments.get_program_name();

  typedef math::ordered_set<argument_attributes>::const_iterator set_iterator;

  std::list<set_iterator> optional;
  std::list<set_iterator>::const_iterator it_opt;
  set_iterator it;

  for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
    if ( it->is_optional() )
      optional.push_back(it);
    else
      std::cout << ' ' << it->format_short_help();

  for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
    if (it->get_second_name().empty())
      {
  if ( it->is_optional() )
    optional.push_back(it);
  else
    std::cout << ' ' << it->format_short_help();
      }

  for (it_opt=optional.begin(); it_opt!=optional.end(); ++it_opt)
    std::cout << ' ' << (*it_opt)->format_short_help();

  if ( !free_args.empty() )
    std::cout << ' ' << free_args;

  std::cout << "\n\n";

  for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
    std::cout << "\t" << it->format_long_help() << std::endl;

  for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
    if (it->get_second_name().empty())
      std::cout << "\t" << it->format_long_help() << std::endl;
} // 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_nameThe name of the argument to test.

Definition at line 333 of file arguments_table.cpp.

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

{
  bool result = true;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( short_name.empty() && long_name.empty() )
    result = false;
  else
    {
      if ( !short_name.empty() )
        result = m_arguments.only_integer_values(short_name);

      if ( !long_name.empty() )
        result = result && m_arguments.only_integer_values(long_name);
    }

  return result;
} // 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_nameThe name of the argument to test.

Definition at line 360 of file arguments_table.cpp.

{
  bool result = true;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( short_name.empty() && long_name.empty() )
    result = false;
  else
    {
      if ( !short_name.empty() )
        result = m_arguments.only_real_values(short_name);

      if ( !long_name.empty() )
        result = result && m_arguments.only_real_values(long_name);
    }

  return result;
} // arguments_table::only_real_values()
void claw::arguments_table::parse ( int &  argc,
char **&  argv 
)

Parse the command line arguments.

Parameters:
argcNumber of arguments.
argvArguments.

All supported arguments will be removed from argv.

Definition at line 214 of file arguments_table.cpp.

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

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

{
  math::ordered_set<std::string> allowed;
  math::ordered_set<argument_attributes>::const_iterator it;

  for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
    allowed.insert(it->get_name());

  for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
    allowed.insert(it->get_name());

  m_arguments.parse( argc, argv, allowed );
} // 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.

{
  bool ok = true;
  math::ordered_set<argument_attributes>::const_iterator it;

  for (it=m_short_arguments.begin(); (it!=m_short_arguments.end()) && ok; ++it)
    if ( !it->is_optional() )
      ok = ok && has_value(it->get_name());

  for (it=m_long_arguments.begin(); (it!=m_long_arguments.end()) && ok; ++it)
    if ( !it->is_optional() )
      ok = ok && has_value(it->get_name());

  return ok;
} // arguments_table::required_fields_are_set()

Member Data Documentation

The class that will store arguments values.

Definition at line 134 of file arguments_table.hpp.

The arguments with a long version.

Definition at line 140 of file arguments_table.hpp.

The arguments with a short version.

Definition at line 137 of file arguments_table.hpp.


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