CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
|
A class to manage the arguments of your program. More...
#include <arguments.hpp>
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. |
A class to manage the arguments of your program.
This class will handle all arguments of type -l[=val] or --long[=val].
Definition at line 50 of file arguments.hpp.
typedef std::map< std::string, std::list<std::string> > claw::arguments::valued_arguments_map [private] |
Definition at line 54 of file arguments.hpp.
claw::arguments::arguments | ( | ) |
Constructor.
Definition at line 41 of file arguments.cpp.
: m_program_name( claw_gettext("<unknow>") ) { } // arguments::arguments()
claw::arguments::arguments | ( | const std::string & | prog_name | ) | [explicit] |
Constructor.
prog_name | Force the name of the program. |
Definition at line 52 of file arguments.cpp.
: m_program_name(prog_name) { } // arguments::arguments()
claw::arguments::arguments | ( | int & | argc, |
char **& | argv | ||
) |
Constructor.
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 67 of file arguments.cpp.
{ parse(argc, argv); } // arguments::arguments()
claw::arguments::arguments | ( | int & | argc, |
char **& | argv, | ||
const claw::math::ordered_set< std::string > & | allowed | ||
) |
Constructor.
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 82 of file arguments.cpp.
{ parse(argc, argv, allowed); } // arguments::arguments()
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.
arg | The argument to add. |
Definition at line 325 of file arguments.cpp.
References CLAW_ASSERT.
{ 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.
arg_name | The name of the argument to get. |
Definition at line 248 of file arguments.cpp.
{ 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.
arg_name | The name of the argument to get. |
Definition at line 276 of file arguments.cpp.
{ 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.
arg_name | The name of the argument to get. |
Definition at line 304 of file arguments.cpp.
bool claw::arguments::get_bool | ( | const std::string & | arg_name | ) | const |
int claw::arguments::get_integer | ( | const std::string & | arg_name | ) | const |
Get the integer value of an argument.
arg_name | The name of the argument to get. |
Definition at line 197 of file arguments.cpp.
References CLAW_ASSERT.
{ 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 176 of file arguments.cpp.
{ 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.
arg_name | The name of the argument to get. |
Definition at line 215 of file arguments.cpp.
References CLAW_ASSERT.
{ 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.
arg_name | The name of the argument to get. |
Definition at line 234 of file arguments.cpp.
References CLAW_ASSERT.
{ 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.
arg_name | The name of the argument to test. |
Definition at line 123 of file arguments.cpp.
bool claw::arguments::only_integer_values | ( | const std::string & | arg_name | ) | const |
Tell if only integer values are associated to an argument.
arg_name | The name of the argument to test. |
Definition at line 133 of file arguments.cpp.
{ const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name)); bool result = true; if ( itk == m_pairs.end() ) result = false; else { std::list<std::string>::const_iterator it; 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.
arg_name | The name of the argument to test. |
Definition at line 155 of file arguments.cpp.
{ const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name)); bool result = true; if ( itk == m_pairs.end() ) result = false; else { std::list<std::string>::const_iterator it; 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.
argc | Number of arguments. |
argv | Arguments. |
allowed | The set of allowed arguments. |
All supported arguments will be removed from argv.
Definition at line 112 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.
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 352 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.
argc | Number of arguments. |
argv | Arguments. |
All supported arguments will be removed from argv.
Definition at line 97 of file arguments.cpp.
{ 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.
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 473 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.
argc | The number of arguments. |
argv | The arguments. |
Definition at line 429 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.
arg | The argument to split. |
name | (out) The name of the argument. |
value | (out) The value of the argument. |
Definition at line 402 of file arguments.cpp.
References CLAW_ASSERT.
{ 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()
claw::math::ordered_set<std::string> claw::arguments::m_flags [private] |
yes/no arguments.
Definition at line 102 of file arguments.hpp.
valued_arguments_map claw::arguments::m_pairs [private] |
Arguments of type key=value.
Definition at line 105 of file arguments.hpp.
std::string claw::arguments::m_program_name [private] |
The name of the program.
Definition at line 99 of file arguments.hpp.