50 : m_program_name(prog_name)
79 parse(argc, argv, allowed);
103 int& argc,
char**& argv,
106 parse(argc, argv,
false, allowed);
115 return m_pairs.find(arg_name) != m_pairs.end();
124 const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name));
127 if(itk == m_pairs.end())
131 std::list<std::string>::const_iterator it;
132 for(it = itk->second.begin(); result && (it != itk->second.end()); ++it)
133 result = result && text::is_of_type<int>(*it);
145 const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name));
148 if(itk == m_pairs.end())
152 std::list<std::string>::const_iterator it;
153 for(it = itk->second.begin(); result && (it != itk->second.end()); ++it)
154 result = result && text::is_of_type<double>(*it);
165 return m_program_name;
174 return m_flags.find(arg_name) != m_flags.end();
185 "arguments::get_integer(): argument is not set.");
187 std::istringstream iss(m_pairs.find(arg_name)->second.back());
202 "arguments::get_real(): argument is not set.");
204 std::istringstream iss(m_pairs.find(arg_name)->second.back());
220 "arguments::get_string(): argument is not set.");
222 return m_pairs.find(arg_name)->second.back();
232 std::list<int> result;
233 const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name));
235 if(itk != m_pairs.end())
237 std::list<std::string>::const_iterator it;
239 for(it = itk->second.begin(); it != itk->second.end(); ++it)
240 if(text::is_of_type<int>(*it))
242 std::istringstream iss(*it);
245 result.push_back(val);
259 std::list<double> result;
260 const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name));
262 if(itk != m_pairs.end())
264 std::list<std::string>::const_iterator it;
266 for(it = itk->second.begin(); it != itk->second.end(); ++it)
267 if(text::is_of_type<double>(*it))
269 std::istringstream iss(*it);
272 result.push_back(val);
283std::list<std::string>
286 std::list<std::string> result;
287 const valued_arguments_map::const_iterator itk(m_pairs.find(arg_name));
289 if(itk != m_pairs.end())
290 result = itk->second;
306 CLAW_ASSERT(arg !=
"--",
"arguments::add_argument(): arg can't be '--'");
308 "arguments::add_argument(): arg must begin by '-'");
310 std::string name, value;
311 const bool has_value = split_argument(arg, name, value);
316 m_pairs[name].push_back(value);
331 int& argc,
char**& argv,
bool always_allowed,
337 if(m_program_name.empty() && (argc != 0))
339 m_program_name = argv[0];
344 for(
int argi = base; (argi != argc) && !stop; ++argi)
346 std::string arg(argv[argi]);
349 if((arg[0] ==
'-') && (arg.length() > 1))
355 std::string name, value;
356 const bool has_value = split_argument(arg, name, value);
359 process_boolean(argv[argi], always_allowed, allowed);
360 else if(always_allowed
361 || (allowed.find(name) != allowed.end()))
370 remove_null_arguments(argc, argv);
381bool claw::arguments::split_argument(
const std::string& arg, std::string& name,
382 std::string& value)
const
384 CLAW_ASSERT(arg !=
"--",
"arguments::split_argument(): arg can't be '--'");
386 "arguments::split_argument(): arg must begin by '-'");
388 std::string::size_type pos = arg.find_first_of(
'=');
391 if(pos == std::string::npos)
398 name = arg.substr(0, pos);
399 value = arg.substr(pos + 1, arg.length() - pos - 1);
411void claw::arguments::remove_null_arguments(
int& argc,
char**& argv)
const
415 for(
int i = 0; i != argc; ++i)
423 while((j != argc) && !ok)
438 if((std::string(argv[c - 1]) ==
"--"))
453void claw::arguments::process_boolean(
454 char*& arg,
bool always_allowed,
455 const claw::math::ordered_set<std::string>& allowed)
457 CLAW_ASSERT(std::string(arg) !=
"--",
"arg can't be '--'");
459 "arg must be at least two characters long");
460 CLAW_ASSERT(arg[0] ==
'-',
"arg must begin by '-'");
464 if(always_allowed || (allowed.find(arg) != allowed.end()))
475 while(arg[i] !=
'\0')
479 if(always_allowed || (allowed.find(s) != allowed.end()))
484 for(
int j = i; arg[j] !=
'\0'; ++j)
A class to manage the arguments of your program.
Some assert macros to strengthen you code.
#define CLAW_ASSERT(b, s)
Print a message on std::cerr and stop the program if a condition is not true.
void add_argument(const std::string &arg)
Add an argument in our list.
void parse(int &argc, char **&argv)
Parse arguments.
double get_real(const std::string &arg_name) const
Get the real value of an argument.
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 only_integer_values(const std::string &arg_name) const
Tell if only integer values are associated to an argument.
std::list< std::string > get_all_of_string(const std::string &arg_name) const
Get all string values of an argument.
bool get_bool(const std::string &arg_name) const
Get the boolean state 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.
int get_integer(const std::string &arg_name) const
Get the integer value of an argument.
bool only_real_values(const std::string &arg_name) const
Tell if only real values are associated to an argument.
A class to manage sets of ordered items.
Macros to call gettext on the libclaw textdomain.
#define claw_gettext(s)
Call gettext on the default text domain used by Claw.
Generic algorithms on strings.