#include <arguments.hpp>
This class will handle all arguments of type -l[=val] or --long[=val].
Definition at line 50 of file 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. |
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 40 of file arguments.cpp.
00041 : m_program_name("<unknow>") 00042 { 00043 00044 } // arguments::arguments()
claw::arguments::arguments | ( | const std::string & | prog_name | ) | [explicit] |
Constructor.
prog_name | Force the name of the program. |
Definition at line 51 of file arguments.cpp.
00052 : m_program_name(prog_name) 00053 { 00054 00055 } // arguments::arguments()
claw::arguments::arguments | ( | int & | argc, | |
char **& | argv | |||
) |
Constructor.
argc | Number of arguments. | |
argv | Arguments. |
Definition at line 66 of file arguments.cpp.
References parse().
00067 { 00068 parse(argc, argv); 00069 } // 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. |
Definition at line 81 of file arguments.cpp.
References parse().
00084 { 00085 parse(argc, argv, allowed); 00086 } // 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 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().
00329 { 00330 CLAW_ASSERT( arg != "--", "arguments::add_argument(): arg can't be '--'" ); 00331 CLAW_ASSERT( arg[0] == '-', 00332 "arguments::add_argument(): arg must begin by '-'" ); 00333 00334 std::string name, value; 00335 split_argument(arg, name, value); 00336 00337 if ( value.empty() ) 00338 m_flags.insert( arg ); 00339 else 00340 m_pairs[name].push_back(value); 00341 } // 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 251 of file arguments.cpp.
References m_pairs.
Referenced by claw::arguments_table::get_all_of_integer().
00252 { 00253 std::list<int> result; 00254 const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name)); 00255 00256 if ( itk != m_pairs.end() ) 00257 { 00258 std::list<std::string>::const_iterator it; 00259 00260 for( it=itk->second.begin(); it!=itk->second.end(); ++it ) 00261 if ( text::is_of_type<int>(*it) ) 00262 { 00263 std::istringstream iss(*it); 00264 int val; 00265 iss >> val; 00266 result.push_back(val); 00267 } 00268 } 00269 00270 return result; 00271 } // 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 279 of file arguments.cpp.
References m_pairs.
Referenced by claw::arguments_table::get_all_of_real().
00280 { 00281 std::list<double> result; 00282 const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name)); 00283 00284 if ( itk != m_pairs.end() ) 00285 { 00286 std::list<std::string>::const_iterator it; 00287 00288 for( it=itk->second.begin(); it!=itk->second.end(); ++it ) 00289 if ( text::is_of_type<double>(*it) ) 00290 { 00291 std::istringstream iss(*it); 00292 double val; 00293 iss >> val; 00294 result.push_back(val); 00295 } 00296 } 00297 00298 return result; 00299 } // 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 307 of file arguments.cpp.
References m_pairs.
Referenced by claw::arguments_table::get_all_of_string().
00308 { 00309 std::list<std::string> result; 00310 const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name)); 00311 00312 if ( itk != m_pairs.end() ) 00313 result = itk->second; 00314 00315 return result; 00316 } // arguments::get_all_of_string()
bool claw::arguments::get_bool | ( | const std::string & | arg_name | ) | const |
Get the boolean state of an argument.
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().
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 200 of file arguments.cpp.
References CLAW_ASSERT, has_value(), and m_pairs.
Referenced by claw::arguments_table::get_integer().
00201 { 00202 CLAW_ASSERT( has_value(arg_name), 00203 "arguments::get_integer(): argument is not set." ); 00204 00205 std::istringstream iss( m_pairs.find( arg_name )->second.back() ); 00206 int val; 00207 iss >> val; 00208 00209 return val; 00210 } // 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().
00180 { 00181 return m_program_name; 00182 } // 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 218 of file arguments.cpp.
References CLAW_ASSERT, has_value(), and m_pairs.
Referenced by claw::arguments_table::get_real().
00219 { 00220 CLAW_ASSERT( has_value(arg_name), 00221 "arguments::get_real(): argument is not set." ); 00222 00223 std::istringstream iss( m_pairs.find( arg_name )->second.back() ); 00224 double val; 00225 iss >> val; 00226 00227 return val; 00228 } // 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 237 of file arguments.cpp.
References CLAW_ASSERT, has_value(), and m_pairs.
Referenced by claw::arguments_table::get_string().
00238 { 00239 CLAW_ASSERT( has_value(arg_name), 00240 "arguments::get_string(): argument is not set." ); 00241 00242 return m_pairs.find( arg_name )->second.back(); 00243 } // 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 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().
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 132 of file arguments.cpp.
References m_pairs.
Referenced by claw::arguments_table::only_integer_values().
00133 { 00134 const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name)); 00135 bool result; 00136 00137 if ( itk == m_pairs.end() ) 00138 result = false; 00139 else 00140 { 00141 std::list<std::string>::const_iterator it; 00142 result = true; 00143 00144 for( it=itk->second.begin(); result && (it!=itk->second.end()); ++it ) 00145 result = result && text::is_of_type<int>(*it); 00146 } 00147 00148 return result; 00149 } // 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 156 of file arguments.cpp.
References m_pairs.
Referenced by claw::arguments_table::only_real_values().
00157 { 00158 const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name)); 00159 bool result; 00160 00161 if ( itk == m_pairs.end() ) 00162 result = false; 00163 else 00164 { 00165 std::list<std::string>::const_iterator it; 00166 result = true; 00167 00168 for( it=itk->second.begin(); result && (it!=itk->second.end()); ++it ) 00169 result = result && text::is_of_type<double>(*it); 00170 } 00171 00172 return result; 00173 } // arguments::only_real_values()
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. |
Definition at line 355 of file arguments.cpp.
References claw::avl< K, Comp >::end(), and claw::avl< K, Comp >::find().
00357 { 00358 bool stop = false; 00359 int base = 0; 00360 00361 if (m_program_name.empty() && (argc!=0)) 00362 { 00363 m_program_name = argv[0]; 00364 argv[0] = NULL; 00365 base = 1; 00366 } 00367 00368 for (int argi=base; (argi!=argc) && !stop; ++argi) 00369 { 00370 std::string arg(argv[argi]); 00371 00372 if ( !arg.empty() ) 00373 if ( (arg[0] == '-') && (arg.length() > 1) ) 00374 { 00375 if (arg == "--") 00376 stop = true; 00377 else 00378 { 00379 std::string name, value; 00380 split_argument( arg, name, value ); 00381 00382 if ( value.empty() ) 00383 process_boolean( argv[argi], always_allowed, allowed ); 00384 else if ( always_allowed 00385 || (allowed.find( name ) != allowed.end()) ) 00386 { 00387 add_argument( arg ); 00388 argv[argi] = NULL; 00389 } 00390 } 00391 } 00392 } 00393 00394 remove_null_arguments( argc, argv ); 00395 } // arguments::parse()
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. |
Definition at line 111 of file arguments.cpp.
00113 { 00114 parse( argc, argv, false, allowed ); 00115 } // arguments::parse()
void claw::arguments::parse | ( | int & | argc, | |
char **& | argv | |||
) |
Parse arguments.
argc | Number of arguments. | |
argv | Arguments. |
Definition at line 96 of file arguments.cpp.
Referenced by arguments(), and claw::arguments_table::parse().
00097 { 00098 parse( argc, argv, true, claw::math::ordered_set<std::string>() ); 00099 } // 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 476 of file arguments.cpp.
References CLAW_ASSERT, claw::avl< K, Comp >::end(), and claw::avl< K, Comp >::find().
00478 { 00479 CLAW_ASSERT( std::string(arg) != "--", "arg can't be '--'" ); 00480 CLAW_ASSERT( std::string(arg).length() > 1, 00481 "arg must be at least two characters long" ); 00482 CLAW_ASSERT( arg[0] == '-', "arg must begin by '-'" ); 00483 00484 if ( arg[1] == '-' ) // long boolean 00485 { 00486 if ( always_allowed || (allowed.find(arg) != allowed.end()) ) 00487 { 00488 add_argument(arg); 00489 arg = NULL; 00490 } 00491 } 00492 else // short boolean(s) 00493 { 00494 int i(1); 00495 std::string s("-?"); // equivalent single character argument 00496 00497 while ( arg[i] != '\0' ) 00498 { 00499 s[1] = arg[i]; 00500 00501 if ( always_allowed || (allowed.find(s) != allowed.end()) ) 00502 { 00503 add_argument(s); 00504 00505 // shift remaining arguments 00506 for ( int j=i; arg[j]!='\0'; ++j ) 00507 arg[j] = arg[j+1]; 00508 } 00509 else 00510 ++i; 00511 } 00512 00513 if ( i==1 ) // all arguments have been accepted 00514 arg = NULL; 00515 } 00516 } // 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 432 of file arguments.cpp.
00433 { 00434 unsigned int c=0; // number of non-NULL arguments 00435 00436 for (int i=0; i!=argc; ++i) 00437 if ( argv[i] != NULL ) 00438 ++c; 00439 else 00440 { 00441 bool ok = false; 00442 int j=i; 00443 00444 while ( (j!=argc) && !ok ) 00445 if ( argv[j] == NULL ) 00446 ++j; 00447 else 00448 ok = true; 00449 00450 if (ok) 00451 { 00452 argv[i] = argv[j]; 00453 argv[j] = NULL; 00454 ++c; 00455 } 00456 } 00457 00458 if ( c > 0 ) 00459 if ( (std::string(argv[c-1]) == "--") ) 00460 --c; 00461 00462 argc=c; 00463 } // 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 405 of file arguments.cpp.
References CLAW_ASSERT.
Referenced by add_argument().
00407 { 00408 CLAW_ASSERT( arg != "--", "arguments::split_argument(): arg can't be '--'" ); 00409 CLAW_ASSERT( arg[0] == '-', 00410 "arguments::split_argument(): arg must begin by '-'" ); 00411 00412 std::string::size_type pos = arg.find_first_of('='); 00413 00414 if ( pos == std::string::npos ) 00415 { 00416 name = arg; 00417 value.clear(); 00418 } 00419 else 00420 { 00421 name = arg.substr(0, pos); 00422 value = arg.substr(pos+1, arg.length() - pos - 1); 00423 } 00424 } // 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.
Referenced by add_argument(), and get_bool().
valued_arguments_map claw::arguments::m_pairs [private] |
Arguments of type key=value.
Definition at line 105 of file arguments.hpp.
Referenced by add_argument(), get_all_of_integer(), get_all_of_real(), get_all_of_string(), get_integer(), get_real(), get_string(), has_value(), only_integer_values(), and only_real_values().
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().