arguments_table.cpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#include <claw/arguments_table.hpp>
#include <claw/assert.hpp>
#include <iostream>
/*----------------------------------------------------------------------------*/
claw::arguments_table::argument_attributes::argument_attributes
( const std::string& name, const std::string& second_name,
const std::string& help_message, bool optional,
const std::string& value_type )
: m_name(name), m_second_name(second_name), m_help_message(help_message),
m_optional(optional), m_value_type(value_type)
{
} // arguments_table::argument_attributes::argument_attributes()
/*----------------------------------------------------------------------------*/
bool claw::arguments_table::argument_attributes::operator<
( const argument_attributes& that ) const
{
return m_name < that.m_name;
} // arguments_table::argument_attributes::operator<()
/*----------------------------------------------------------------------------*/
std::string
claw::arguments_table::argument_attributes::format_short_help() const
{
std::string result(m_name);
if (!m_value_type.empty())
result += "=" + m_value_type;
if (m_optional)
return "[" + result + "]";
else
return result;
} // arguments_table::argument_attributes::format_short_help()
/*----------------------------------------------------------------------------*/
std::string claw::arguments_table::argument_attributes::format_long_help() const
{
std::string result(m_name);
if ( !m_second_name.empty() )
result += ", " + m_second_name;
return result + "\t" + m_help_message;
} // arguments_table::argument_attributes::format_long_help()
/*----------------------------------------------------------------------------*/
const std::string& claw::arguments_table::argument_attributes::get_name() const
{
return m_name;
} // arguments_table::argument_attributes::get_name()
/*----------------------------------------------------------------------------*/
const std::string&
claw::arguments_table::argument_attributes::get_second_name() const
{
return m_second_name;
} // arguments_table::argument_attributes::get_second_name()
/*----------------------------------------------------------------------------*/
bool claw::arguments_table::argument_attributes::is_optional() const
{
return m_optional;
} // arguments_table::argument_attributes::is_optional()
/*----------------------------------------------------------------------------*/
claw::arguments_table::arguments_table( const std::string& prog_name )
: m_arguments(prog_name)
{
} // arguments_table::arguments_table()
/*----------------------------------------------------------------------------*/
claw::arguments_table::arguments_table( int& argc, char** &argv )
: m_arguments(argc, argv, claw::math::ordered_set<std::string>() )
{
} // arguments_table::arguments_table()
/*----------------------------------------------------------------------------*/
void claw::arguments_table::add( const std::string& short_name,
const std::string& long_name,
const std::string& help_msg, bool optional,
const std::string& val_name )
{
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_long( const std::string& long_name,
const std::string& help_msg,
bool optional,
const std::string& val_name )
{
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,
const std::string& val_name )
{
m_short_arguments.insert( argument_attributes(short_name, "", help_msg,
optional, val_name) );
} // arguments_table::add_short()
/*----------------------------------------------------------------------------*/
void claw::arguments_table::parse( int& argc, char** &argv )
{
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()
/*----------------------------------------------------------------------------*/
void claw::arguments_table::help( const std::string& free_args ) const
{
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::required_fields_are_set() const
{
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()
/*----------------------------------------------------------------------------*/
bool claw::arguments_table::has_value( const std::string& arg_name ) const
{
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()
/*----------------------------------------------------------------------------*/
bool
claw::arguments_table::only_integer_values( const std::string& arg_name ) const
{
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
{
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()
/*----------------------------------------------------------------------------*/
const std::string& claw::arguments_table::get_program_name() const
{
return m_arguments.get_program_name();
} // arguments_table::has_value()
/*----------------------------------------------------------------------------*/
bool claw::arguments_table::get_bool( const std::string& arg_name ) const
{
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
{
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()
/*----------------------------------------------------------------------------*/
double claw::arguments_table::get_real( const std::string& arg_name ) const
{
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
{
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()
/*----------------------------------------------------------------------------*/
std::list<int>
claw::arguments_table::get_all_of_integer( const std::string& arg_name ) const
{
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
{
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
{
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::add_argument( const std::string& arg )
{
m_arguments.add_argument( arg );
} // 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
{
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()
arguments_table.hpp
A class to manage the arguments of your program, with automatic management of short/long arguments an...
assert.hpp
Some assert macros to strengthen you code.
CLAW_PRECOND
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition assert.hpp:98
claw::arguments_table::help
void help(const std::string &free_args="") const
Print some help about the arguments.
Definition arguments_table.cpp:239
claw::arguments_table::get_string
const std::string & get_string(const std::string &arg_name) const
Get the string value of an argument.
Definition arguments_table.cpp:451
claw::arguments_table::get_all_of_real
std::list< double > get_all_of_real(const std::string &arg_name) const
Get all real values of an argument.
Definition arguments_table.cpp:496
claw::arguments_table::arguments_table
arguments_table(const std::string &prog_name)
Constructor.
Definition arguments_table.cpp:132
claw::arguments_table::has_value
bool has_value(const std::string &arg_name) const
Tell if an argument has a value.
Definition arguments_table.cpp:310
claw::arguments_table::only_integer_values
bool only_integer_values(const std::string &arg_name) const
Tell if only integer values are associated to an argument.
Definition arguments_table.cpp:333
claw::arguments_table::get_integer
int get_integer(const std::string &arg_name) const
Get the integer value of an argument.
Definition arguments_table.cpp:410
claw::arguments_table::get_program_name
const std::string & get_program_name() const
Get the name of the program.
Definition arguments_table.cpp:385
claw::arguments_table::add_short
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.
Definition arguments_table.cpp:197
claw::arguments_table::add_long
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.
Definition arguments_table.cpp:180
claw::arguments_table::add_argument
void add_argument(const std::string &arg)
Add an argument in our list.
Definition arguments_table.cpp:550
claw::arguments_table::parse
void parse(int &argc, char **&argv)
Parse the command line arguments.
Definition arguments_table.cpp:214
claw::arguments_table::get_all_of_string
std::list< std::string > get_all_of_string(const std::string &arg_name) const
Get all string values of an argument.
Definition arguments_table.cpp:521
claw::arguments_table::get_bool
bool get_bool(const std::string &arg_name) const
Get the boolean state of an argument.
Definition arguments_table.cpp:395
claw::arguments_table::get_all_of_integer
std::list< int > get_all_of_integer(const std::string &arg_name) const
Get all integer values of an argument.
Definition arguments_table.cpp:471
claw::arguments_table::add
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.
Definition arguments_table.cpp:161
claw::arguments_table::only_real_values
bool only_real_values(const std::string &arg_name) const
Tell if only real values are associated to an argument.
Definition arguments_table.cpp:360
claw::arguments_table::required_fields_are_set
bool required_fields_are_set() const
Tell if all arguments not marqued as "optional" have been specified in the command line.
Definition arguments_table.cpp:289
claw::arguments_table::get_real
double get_real(const std::string &arg_name) const
Get the real value of an argument.
Definition arguments_table.cpp:430
claw::math::ordered_set
A class to manage sets of ordered items.
Definition ordered_set.hpp:46
claw::math::ordered_set::const_iterator
super::const_iterator const_iterator
The type of the iterator used to access non modifiable values.
Definition ordered_set.hpp:54
claw::math
Manipulation of mathematic, geometric, etc. items.
Definition box_2d.hpp:38
claw
This is the main namespace.
Definition algorithm.hpp:34