Public Member Functions | Private Types | Private Member Functions | Private Attributes

claw::arguments Class Reference

A class to manage the arguments of your program. More...

#include <arguments.hpp>

List of all members.

Public Member Functions

 arguments ()
 Constructor.
 arguments (const std::string &prog_name)
 Constructor.
 arguments (int &argc, char **&argv)
 Constructor.
 arguments (int &argc, char **&argv, const claw::math::ordered_set< std::string > &allowed)
 Constructor.
void parse (int &argc, char **&argv)
 Parse arguments.
void parse (int &argc, char **&argv, const claw::math::ordered_set< std::string > &allowed)
 Parse arguments.
bool has_value (const std::string &arg_name) const
 Tell if a value is associated to an argument.
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 Types

typedef std::map< std::string,
std::list< std::string > > 
valued_arguments_map

Private Member Functions

void parse (int &argc, char **&argv, bool always_allowed, const claw::math::ordered_set< std::string > &allowed)
 Parse arguments.
void split_argument (const std::string &arg, std::string &name, std::string &value) const
 Split an argument to get its name and its value.
void remove_null_arguments (int &argc, char **&argv) const
 Remove all NULL arguments from argv and update argc.
void process_boolean (char *&arg, bool always_allowed, const claw::math::ordered_set< std::string > &allowed)
 Process a boolean option.

Private Attributes

std::string m_program_name
 The name of the program.
claw::math::ordered_set
< std::string > 
m_flags
 yes/no arguments.
valued_arguments_map m_pairs
 Arguments of type key=value.

Detailed Description

A class to manage the arguments of your program.

This class will handle all arguments of type -l[=val] or --long[=val].

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 50 of file arguments.hpp.


Member Typedef Documentation

typedef std::map< std::string, std::list<std::string> > claw::arguments::valued_arguments_map [private]

Definition at line 54 of file arguments.hpp.


Constructor & Destructor Documentation

claw::arguments::arguments (  ) 

Constructor.

Definition at line 40 of file arguments.cpp.

  : m_program_name("<unknow>")
{

} // arguments::arguments()

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

Constructor.

Parameters:
prog_name Force the name of the program.

Definition at line 51 of file arguments.cpp.

  : m_program_name(prog_name)
{

} // arguments::arguments()

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

Constructor.

Parameters:
argc Number of arguments.
argv Arguments.

You should construct an instance with the parameters given to your function main(). The constructor will remove all supported arguments from argv.

Definition at line 66 of file arguments.cpp.

References parse().

{
  parse(argc, argv);
} // arguments::arguments()

claw::arguments::arguments ( int &  argc,
char **&  argv,
const claw::math::ordered_set< std::string > &  allowed 
)

Constructor.

Parameters:
argc Number of arguments.
argv Arguments.
allowed The set of allowed arguments.

You should construct an instance with the parameters given to your function main(). The constructor will remove all supported arguments from argv.

Definition at line 81 of file arguments.cpp.

References parse().

{
  parse(argc, argv, allowed);
} // arguments::arguments()


Member Function Documentation

void claw::arguments::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_arguments.

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

Definition at line 328 of file arguments.cpp.

References CLAW_ASSERT, claw::avl< K, Comp >::insert(), m_flags, m_pairs, and split_argument().

Referenced by claw::arguments_table::add_argument().

{
  CLAW_ASSERT( arg != "--", "arguments::add_argument(): arg can't be '--'" );
  CLAW_ASSERT( arg[0] == '-',
               "arguments::add_argument(): arg must begin by '-'" );
  
  std::string name, value;
  split_argument(arg, name, value);

  if ( value.empty() )
    m_flags.insert( arg );
  else
    m_pairs[name].push_back(value);
} // arguments::add_argument()

std::list< int > claw::arguments::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 251 of file arguments.cpp.

References m_pairs.

Referenced by claw::arguments_table::get_all_of_integer().

{
  std::list<int> result;
  const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name));

  if ( itk != m_pairs.end() )
    {
      std::list<std::string>::const_iterator it;

      for( it=itk->second.begin(); it!=itk->second.end(); ++it )
        if ( text::is_of_type<int>(*it) )
          {
            std::istringstream iss(*it);
            int val;
            iss >> val;
            result.push_back(val);
          }
    }

  return result;
} // arguments::get_all_of_integer()

std::list< double > claw::arguments::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 279 of file arguments.cpp.

References m_pairs.

Referenced by claw::arguments_table::get_all_of_real().

{
  std::list<double> result;
  const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name));

  if ( itk != m_pairs.end() )
    {
      std::list<std::string>::const_iterator it;

      for( it=itk->second.begin(); it!=itk->second.end(); ++it )
        if ( text::is_of_type<double>(*it) )
          {
            std::istringstream iss(*it);
            double val;
            iss >> val;
            result.push_back(val);
          }
    }

  return result;
} // arguments::get_all_of_real()

std::list< std::string > claw::arguments::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 307 of file arguments.cpp.

References m_pairs.

Referenced by claw::arguments_table::get_all_of_string().

{
  std::list<std::string> result;
  const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name));

  if ( itk != m_pairs.end() )
    result = itk->second;

  return result;
} // arguments::get_all_of_string()

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

Get the boolean state of an argument.

Parameters:
arg_name The name of the argument to get.

Definition at line 189 of file arguments.cpp.

References claw::avl< K, Comp >::end(), claw::avl< K, Comp >::find(), and m_flags.

Referenced by claw::arguments_table::get_bool().

{
  return m_flags.find( arg_name ) != m_flags.end();
} // arguments::get_bool()

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

Get the integer value of an argument.

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

Definition at line 200 of file arguments.cpp.

References CLAW_ASSERT, has_value(), and m_pairs.

Referenced by claw::arguments_table::get_integer().

{
  CLAW_ASSERT( has_value(arg_name),
               "arguments::get_integer(): argument is not set." );

  std::istringstream iss( m_pairs.find( arg_name )->second.back() );
  int val;
  iss >> val;

  return val;
} // arguments::get_integer()

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

Get the name of the program.

Definition at line 179 of file arguments.cpp.

References m_program_name.

Referenced by claw::arguments_table::get_program_name(), and claw::arguments_table::help().

{
  return m_program_name;
} // arguments::get_program_name()

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

Get the real value of an argument.

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

Definition at line 218 of file arguments.cpp.

References CLAW_ASSERT, has_value(), and m_pairs.

Referenced by claw::arguments_table::get_real().

{
  CLAW_ASSERT( has_value(arg_name),
               "arguments::get_real(): argument is not set." );

  std::istringstream iss( m_pairs.find( arg_name )->second.back() );
  double val;
  iss >> val;

  return val;
} // arguments::get_real()

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

Get the string value of an argument.

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

Definition at line 237 of file arguments.cpp.

References CLAW_ASSERT, has_value(), and m_pairs.

Referenced by claw::arguments_table::get_string().

{
  CLAW_ASSERT( has_value(arg_name),
               "arguments::get_string(): argument is not set." );

  return m_pairs.find( arg_name )->second.back();
} // arguments::get_string()

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

Tell if a value is associated to an argument.

Parameters:
arg_name The name of the argument to test.

Definition at line 122 of file arguments.cpp.

References m_pairs.

Referenced by claw::arguments_table::get_integer(), get_integer(), claw::arguments_table::get_real(), get_real(), claw::arguments_table::get_string(), get_string(), and claw::arguments_table::has_value().

{
  return m_pairs.find( arg_name ) != m_pairs.end();
} // arguments::has_value()

bool claw::arguments::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 132 of file arguments.cpp.

References m_pairs.

Referenced by claw::arguments_table::only_integer_values().

{
  const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name));
  bool result;

  if ( itk == m_pairs.end() )
    result = false;
  else
    {
      std::list<std::string>::const_iterator it;
      result = true;

      for( it=itk->second.begin(); result && (it!=itk->second.end()); ++it )
        result = result && text::is_of_type<int>(*it);
    }

  return result;
} // arguments::only_integer_values()

bool claw::arguments::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 156 of file arguments.cpp.

References m_pairs.

Referenced by claw::arguments_table::only_real_values().

{
  const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name));
  bool result;

  if ( itk == m_pairs.end() )
    result = false;
  else
    {
      std::list<std::string>::const_iterator it;
      result = true;

      for( it=itk->second.begin(); result && (it!=itk->second.end()); ++it )
        result = result && text::is_of_type<double>(*it);
    }

  return result;
} // arguments::only_real_values()

void claw::arguments::parse ( int &  argc,
char **&  argv,
const claw::math::ordered_set< std::string > &  allowed 
)

Parse arguments.

Parameters:
argc Number of arguments.
argv Arguments.
allowed The set of allowed arguments.

All supported arguments will be removed from argv.

Definition at line 111 of file arguments.cpp.

{
  parse( argc, argv, false, allowed );
} // arguments::parse()

void claw::arguments::parse ( int &  argc,
char **&  argv,
bool  always_allowed,
const claw::math::ordered_set< std::string > &  allowed 
) [private]

Parse arguments.

Parameters:
argc Number of arguments.
argv Arguments.
always_allowed If true, allowed is never used and all arguments with a valid format are accepted.
allowed The set of allowed arguments.

All supported arguments will be removed from argv.

Definition at line 355 of file arguments.cpp.

References claw::avl< K, Comp >::end(), and claw::avl< K, Comp >::find().

{
  bool stop = false;
  int base = 0;

  if (m_program_name.empty() && (argc!=0))
    {
      m_program_name = argv[0];
      argv[0] = NULL;
      base = 1;
    }

  for (int argi=base; (argi!=argc) && !stop; ++argi)
    {
      std::string arg(argv[argi]);

      if ( !arg.empty() )
        if ( (arg[0] == '-') && (arg.length() > 1) )
          {
            if (arg == "--")
              stop = true;
            else
              {
                std::string name, value;
                split_argument( arg, name, value );

                if ( value.empty() )
                  process_boolean( argv[argi], always_allowed, allowed );
                else if ( always_allowed
                          || (allowed.find( name ) != allowed.end()) )
                  {
                    add_argument( arg );
                    argv[argi] = NULL;
                  }
              }
          }
    }

  remove_null_arguments( argc, argv );
} // arguments::parse()

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

Parse arguments.

Parameters:
argc Number of arguments.
argv Arguments.

All supported arguments will be removed from argv.

Definition at line 96 of file arguments.cpp.

Referenced by arguments(), and claw::arguments_table::parse().

{
  parse( argc, argv, true, claw::math::ordered_set<std::string>() );
} // arguments::parse()

void claw::arguments::process_boolean ( char *&  arg,
bool  always_allowed,
const claw::math::ordered_set< std::string > &  allowed 
) [private]

Process a boolean option.

Parameters:
arg (in) The argument to process, (out) the argument cleaned of all valid values.
always_allowed If true, allowed is never used and all arguments with a valid format are accepted.
allowed The set of allowed arguments.

Definition at line 476 of file arguments.cpp.

References CLAW_ASSERT, claw::avl< K, Comp >::end(), and claw::avl< K, Comp >::find().

{
  CLAW_ASSERT( std::string(arg) != "--", "arg can't be '--'" );
  CLAW_ASSERT( std::string(arg).length() > 1,
               "arg must be at least two characters long" );
  CLAW_ASSERT( arg[0] == '-', "arg must begin by '-'" );

  if ( arg[1] == '-' ) // long boolean
    {
      if ( always_allowed || (allowed.find(arg) != allowed.end()) )
        {
          add_argument(arg);
          arg = NULL;
        }
    }
  else // short boolean(s)
    {
      int i(1);
      std::string s("-?"); // equivalent single character argument

      while ( arg[i] != '\0' )
        {
          s[1] = arg[i];

          if ( always_allowed || (allowed.find(s) != allowed.end()) )
            {
              add_argument(s);

              // shift remaining arguments
              for ( int j=i; arg[j]!='\0'; ++j )
                arg[j] = arg[j+1];
            }
          else
            ++i;
        }

      if ( i==1 ) // all arguments have been accepted
        arg = NULL;
    }
} // arguments::process_boolean()

void claw::arguments::remove_null_arguments ( int &  argc,
char **&  argv 
) const [private]

Remove all NULL arguments from argv and update argc.

Parameters:
argc The number of arguments.
argv The arguments.

Definition at line 432 of file arguments.cpp.

{
  unsigned int c=0; // number of non-NULL arguments
  
  for (int i=0; i!=argc; ++i)
    if ( argv[i] != NULL )
      ++c;
    else
      {
        bool ok = false;
        int j=i;
        
        while ( (j!=argc) && !ok )
          if ( argv[j] == NULL )
            ++j;
          else
            ok = true;

        if (ok)
          {
            argv[i] = argv[j];
            argv[j] = NULL;
            ++c;
          }
      }

  if ( c > 0 )
    if ( (std::string(argv[c-1]) == "--") )
      --c;
    
  argc=c;
} // arguments::remove_null_arguments()

void claw::arguments::split_argument ( const std::string &  arg,
std::string &  name,
std::string &  value 
) const [private]

Split an argument to get its name and its value.

Parameters:
arg The argument to split.
name (out) The name of the argument.
value (out) The value of the argument.
Precondition:
(arg != "--") && (arg[0] == '-')

Definition at line 405 of file arguments.cpp.

References CLAW_ASSERT.

Referenced by add_argument().

{
  CLAW_ASSERT( arg != "--", "arguments::split_argument(): arg can't be '--'" );
  CLAW_ASSERT( arg[0] == '-',
               "arguments::split_argument(): arg must begin by '-'" );
  
  std::string::size_type pos = arg.find_first_of('=');

  if ( pos == std::string::npos )
    {
      name = arg;
      value.clear();
    }
  else
    {
      name = arg.substr(0, pos);
      value = arg.substr(pos+1, arg.length() - pos - 1);
    }
} // arguments::split_argument()


Member Data Documentation

yes/no arguments.

Definition at line 102 of file arguments.hpp.

Referenced by add_argument(), and get_bool().

std::string claw::arguments::m_program_name [private]

The name of the program.

Definition at line 99 of file arguments.hpp.

Referenced by get_program_name().


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