#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 (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. | |
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. | |
void | add_argument (const std::string &arg) |
Add an argument in our list. | |
Private Member Functions | |
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. | |
Private Attributes | |
std::string | m_program_name |
The name of the program. | |
claw::math::ordered_set < std::string > | m_flags |
yes/no arguments. | |
std::map< std::string, std::string > | m_pairs |
Arguments of type key=value. |
claw::arguments::arguments | ( | ) |
Constructor.
Definition at line 38 of file arguments.cpp.
00039 : m_program_name("<unknow>") 00040 { 00041 00042 } // arguments::arguments()
claw::arguments::arguments | ( | int & | argc, | |
char **& | argv | |||
) |
Constructor.
argc | Number of arguments. | |
argv | Arguments. |
Definition at line 53 of file arguments.cpp.
References parse().
00054 { 00055 parse(argc, argv); 00056 } // arguments::arguments()
claw::arguments::arguments | ( | int & | argc, | |
char **& | argv, | |||
const claw::math::ordered_set< std::string > & | allowed | |||
) |
Constructor.
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 68 of file arguments.cpp.
References parse().
00071 { 00072 parse(argc, argv, allowed); 00073 } // arguments::arguments()
void claw::arguments::parse | ( | int & | argc, | |
char **& | argv | |||
) |
Parse arguments.
argc | Number of arguments. | |
argv | Arguments. |
Definition at line 83 of file arguments.cpp.
References add_argument(), m_program_name, and remove_null_arguments().
Referenced by arguments(), and claw::arguments_table::parse().
00084 { 00085 bool stop = false; 00086 int base = 0; 00087 00088 if (m_program_name == "") 00089 { 00090 m_program_name = argv[0]; 00091 argv[0] = NULL; 00092 base = 1; 00093 } 00094 00095 for (int argi=base; (argi!=argc) && !stop; ++argi) 00096 { 00097 std::string arg(argv[argi]); 00098 00099 if ( (arg[0] == '-') && (arg.length() > 1) ) 00100 { 00101 if (arg == "--") 00102 stop = true; 00103 else 00104 { 00105 add_argument( arg ); 00106 argv[argi] = NULL; 00107 } 00108 } 00109 } 00110 00111 remove_null_arguments( argc, argv ); 00112 } // arguments::parse()
void claw::arguments::parse | ( | int & | argc, | |
char **& | argv, | |||
const claw::math::ordered_set< std::string > & | allowed | |||
) |
Parse arguments.
All supported arguments will be removed from argv.
Definition at line 124 of file arguments.cpp.
References add_argument(), claw::avl< K, Comp >::end(), claw::avl< K, Comp >::find(), m_program_name, remove_null_arguments(), and split_argument().
00126 { 00127 bool stop = false; 00128 int base = 0; 00129 00130 if (m_program_name == "") 00131 { 00132 m_program_name = argv[0]; 00133 argv[0] = NULL; 00134 base = 1; 00135 } 00136 00137 for (int argi=base; (argi!=argc) && !stop; ++argi) 00138 { 00139 std::string arg(argv[argi]); 00140 00141 if ( (arg[0] == '-') && (arg.length() > 1) ) 00142 { 00143 if (arg == "--") 00144 stop = true; 00145 else 00146 { 00147 std::string name, value; 00148 split_argument( arg, name, value ); 00149 00150 if ( allowed.find( name ) != allowed.end() ) 00151 { 00152 add_argument( arg ); 00153 argv[argi] = NULL; 00154 } 00155 } 00156 } 00157 } 00158 00159 remove_null_arguments( argc, argv ); 00160 } // arguments::parse()
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 167 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().
const std::string & claw::arguments::get_program_name | ( | ) | const |
Get the name of the program.
Definition at line 176 of file arguments.cpp.
References m_program_name.
Referenced by claw::arguments_table::get_program_name(), and claw::arguments_table::help().
00177 { 00178 return m_program_name; 00179 } // arguments::get_program_name()
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 186 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 197 of file arguments.cpp.
References CLAW_ASSERT, has_value(), and m_pairs.
Referenced by claw::arguments_table::get_integer().
00198 { 00199 CLAW_ASSERT( has_value(arg_name), 00200 "arguments::get_integer(): argument is not set." ); 00201 00202 std::istringstream iss( m_pairs.find( arg_name )->second ); 00203 int val; 00204 iss >> val; 00205 00206 return val; 00207 } // arguments::get_integer()
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, has_value(), and m_pairs.
Referenced by claw::arguments_table::get_real().
00216 { 00217 CLAW_ASSERT( has_value(arg_name), 00218 "arguments::get_real(): argument is not set." ); 00219 00220 std::istringstream iss( m_pairs.find( arg_name )->second ); 00221 double val; 00222 iss >> val; 00223 00224 return val; 00225 } // 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, has_value(), and m_pairs.
Referenced by claw::arguments_table::get_string().
00235 { 00236 CLAW_ASSERT( has_value(arg_name), 00237 "arguments::get_string(): argument is not set." ); 00238 00239 return m_pairs.find( arg_name )->second; 00240 } // arguments::get_string()
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 252 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(), and parse().
00253 { 00254 CLAW_ASSERT( arg != "--", "arguments::add_argument(): arg can't be '--'" ); 00255 CLAW_ASSERT( arg[0] == '-', 00256 "arguments::add_argument(): arg must begin by '-'" ); 00257 00258 std::string name, value; 00259 split_argument(arg, name, value); 00260 00261 if ( value == "" ) 00262 m_flags.insert( arg ); 00263 else 00264 m_pairs[ name ] = value; 00265 } // arguments::add_argument()
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 275 of file arguments.cpp.
References CLAW_ASSERT.
Referenced by add_argument(), and parse().
00277 { 00278 CLAW_ASSERT( arg != "--", "arguments::split_argument(): arg can't be '--'" ); 00279 CLAW_ASSERT( arg[0] == '-', 00280 "arguments::split_argument(): arg must begin by '-'" ); 00281 00282 std::string::size_type pos = arg.find("="); 00283 00284 if ( pos == std::string::npos ) 00285 { 00286 name = arg; 00287 value = ""; 00288 } 00289 else 00290 { 00291 name = arg.substr(0, pos); 00292 value = arg.substr(pos+1, arg.length() - pos - 1); 00293 } 00294 } // arguments::split_argument()
void claw::arguments::remove_null_arguments | ( | int & | argc, | |
char **& | argv | |||
) | const [private] |
Remove all NULL arguments from argv and update argc.
Definition at line 302 of file arguments.cpp.
Referenced by parse().
00303 { 00304 unsigned int c=0; // number of non-NULL arguments 00305 00306 for (int i=0; i!=argc; ++i) 00307 if ( argv[i] != NULL ) 00308 ++c; 00309 else 00310 { 00311 bool ok = false; 00312 int j=i; 00313 00314 while ( (j!=argc) && !ok ) 00315 if ( argv[j] == NULL ) 00316 ++j; 00317 else 00318 ok = true; 00319 00320 if (ok) 00321 { 00322 argv[i] = argv[j]; 00323 argv[j] = NULL; 00324 ++c; 00325 } 00326 } 00327 00328 if ( c > 0 ) 00329 if ( (std::string(argv[c-1]) == "--") ) 00330 --c; 00331 00332 argc=c; 00333 } // arguments::remove_null_arguments()
std::string claw::arguments::m_program_name [private] |
The name of the program.
Definition at line 81 of file arguments.hpp.
Referenced by get_program_name(), and parse().
claw::math::ordered_set<std::string> claw::arguments::m_flags [private] |
yes/no arguments.
Definition at line 84 of file arguments.hpp.
Referenced by add_argument(), and get_bool().
std::map<std::string, std::string> claw::arguments::m_pairs [private] |
Arguments of type key=value.
Definition at line 87 of file arguments.hpp.
Referenced by add_argument(), get_integer(), get_real(), get_string(), and has_value().