44claw::arguments_table::argument_attributes::argument_attributes(
45 const std::string& name,
const std::string& second_name,
46 const std::string& help_message,
bool optional,
47 const std::string& value_type)
49 , m_second_name(second_name)
50 , m_help_message(help_message)
51 , m_optional(optional)
52 , m_value_type(value_type)
59bool claw::arguments_table::argument_attributes::operator<(
60 const argument_attributes& that)
const
62 return m_name < that.m_name;
69claw::arguments_table::argument_attributes::format_short_help()
const
71 std::string result(m_name);
73 if(!m_value_type.empty())
74 result +=
"=" + m_value_type;
77 return "[" + result +
"]";
87std::string claw::arguments_table::argument_attributes::format_long_help(
88 std::size_t arguments_width)
const
90 std::ostringstream result;
92 result << std::left << std::setw(arguments_width)
93 << format_long_help_arguments() <<
' ' << m_help_message;
103claw::arguments_table::argument_attributes::format_long_help_arguments()
const
105 std::string result(m_name);
107 if(!m_second_name.empty())
108 result +=
", " + m_second_name;
116const std::string& claw::arguments_table::argument_attributes::get_name()
const
125claw::arguments_table::argument_attributes::get_second_name()
const
127 return m_second_name;
133bool claw::arguments_table::argument_attributes::is_optional()
const
143 : m_arguments(prog_name)
154 : m_arguments(argc, argv,
claw::
math::ordered_set<std::string>())
166 const std::string& long_name,
167 const std::string& help_msg,
bool optional,
168 const std::string& val_name)
170 m_short_arguments.insert(argument_attributes(short_name, long_name, help_msg,
171 optional, val_name));
172 m_long_arguments.insert(argument_attributes(long_name, short_name, help_msg,
173 optional, val_name));
184 const std::string& help_msg,
186 const std::string& val_name)
188 m_long_arguments.insert(
189 argument_attributes(long_name,
"", help_msg, optional, val_name));
200 const std::string& help_msg,
202 const std::string& val_name)
204 m_short_arguments.insert(
205 argument_attributes(short_name,
"", help_msg, optional, val_name));
220 for(it = m_short_arguments.begin(); it != m_short_arguments.end(); ++it)
221 allowed.insert(it->get_name());
223 for(it = m_long_arguments.begin(); it != m_long_arguments.end(); ++it)
224 allowed.insert(it->get_name());
226 m_arguments.parse(argc, argv, allowed);
241 std::cout << m_arguments.get_program_name();
245 std::list<set_iterator> optional;
246 std::list<set_iterator>::const_iterator it_opt;
249 for(it = m_short_arguments.begin(); it != m_short_arguments.end(); ++it)
250 if(it->is_optional())
251 optional.push_back(it);
253 std::cout <<
' ' << it->format_short_help();
255 for(it = m_long_arguments.begin(); it != m_long_arguments.end(); ++it)
256 if(it->get_second_name().empty())
258 if(it->is_optional())
259 optional.push_back(it);
261 std::cout <<
' ' << it->format_short_help();
264 for(it_opt = optional.begin(); it_opt != optional.end(); ++it_opt)
265 std::cout <<
' ' << (*it_opt)->format_short_help();
267 if(!free_args.empty())
268 std::cout <<
' ' << free_args;
272 const std::size_t description_column(
273 get_maximum_long_help_arguments_width());
275 for(it = m_short_arguments.begin(); it != m_short_arguments.end(); ++it)
276 std::cout <<
" " << it->format_long_help(description_column) <<
'\n';
278 for(it = m_long_arguments.begin(); it != m_long_arguments.end(); ++it)
279 if(it->get_second_name().empty())
280 std::cout <<
" " << it->format_long_help(description_column) <<
'\n';
296 for(it = m_short_arguments.begin(); (it != m_short_arguments.end()) && ok;
298 if(!it->is_optional())
301 for(it = m_long_arguments.begin(); (it != m_long_arguments.end()) && ok;
303 if(!it->is_optional())
316 std::string short_name, long_name;
318 get_argument_names(arg_name, short_name, long_name);
320 if(!short_name.empty())
321 result = m_arguments.has_value(short_name);
324 if(!long_name.empty())
325 result = m_arguments.has_value(long_name);
335 const std::string& arg_name)
const
338 std::string short_name, long_name;
340 get_argument_names(arg_name, short_name, long_name);
342 if(short_name.empty() && long_name.empty())
346 if(!short_name.empty())
347 result = m_arguments.only_integer_values(short_name);
349 if(!long_name.empty())
350 result = result && m_arguments.only_integer_values(long_name);
363 std::string short_name, long_name;
365 get_argument_names(arg_name, short_name, long_name);
367 if(short_name.empty() && long_name.empty())
371 if(!short_name.empty())
372 result = m_arguments.only_real_values(short_name);
374 if(!long_name.empty())
375 result = result && m_arguments.only_real_values(long_name);
386 return m_arguments.get_program_name();
395 std::string short_name, long_name;
397 get_argument_names(arg_name, short_name, long_name);
399 return m_arguments.get_bool(short_name) || m_arguments.get_bool(long_name);
411 std::string short_name, long_name;
413 get_argument_names(arg_name, short_name, long_name);
415 if(m_arguments.has_value(short_name))
416 return m_arguments.get_integer(short_name);
418 return m_arguments.get_integer(long_name);
430 std::string short_name, long_name;
432 get_argument_names(arg_name, short_name, long_name);
434 if(m_arguments.has_value(short_name))
435 return m_arguments.get_real(short_name);
437 return m_arguments.get_real(long_name);
450 std::string short_name, long_name;
452 get_argument_names(arg_name, short_name, long_name);
454 if(m_arguments.has_value(short_name))
455 return m_arguments.get_string(short_name);
457 return m_arguments.get_string(long_name);
467 std::list<int> result;
468 std::string short_name, long_name;
470 get_argument_names(arg_name, short_name, long_name);
472 if(!short_name.empty())
473 result = m_arguments.get_all_of_integer(short_name);
475 if(!long_name.empty())
477 const std::list<int> p(m_arguments.get_all_of_integer(long_name));
478 result.insert(result.end(), p.begin(), p.end());
491 std::list<double> result;
492 std::string short_name, long_name;
494 get_argument_names(arg_name, short_name, long_name);
496 if(!short_name.empty())
497 result = m_arguments.get_all_of_real(short_name);
499 if(!long_name.empty())
501 const std::list<double> p(m_arguments.get_all_of_real(long_name));
502 result.insert(result.end(), p.begin(), p.end());
512std::list<std::string>
515 std::list<std::string> result;
516 std::string short_name, long_name;
518 get_argument_names(arg_name, short_name, long_name);
520 if(!short_name.empty())
521 result = m_arguments.get_all_of_string(short_name);
523 if(!long_name.empty())
525 const std::list<std::string> p(m_arguments.get_all_of_string(long_name));
526 result.insert(result.end(), p.begin(), p.end());
543 m_arguments.add_argument(arg);
552void claw::arguments_table::get_argument_names(
const std::string& arg_name,
553 std::string& short_name,
554 std::string& long_name)
const
556 argument_attributes attr(arg_name,
"",
"",
false,
"");
560 it = m_short_arguments.find(attr);
562 if(it != m_short_arguments.end())
564 short_name = arg_name;
565 long_name = it->get_second_name();
570 it = m_long_arguments.find(attr);
572 if(it != m_long_arguments.end())
574 short_name = it->get_second_name();
575 long_name = arg_name;
585claw::arguments_table::get_maximum_long_help_arguments_width()
const
588 std::size_t result(0);
590 for(it = m_short_arguments.begin(); it != m_short_arguments.end(); ++it)
591 result = std::max(result, it->format_long_help_arguments().size());
593 for(it = m_long_arguments.begin(); it != m_long_arguments.end(); ++it)
594 if(it->get_second_name().empty())
595 result = std::max(result, it->format_long_help_arguments().size());
A class to manage the arguments of your program, with automatic management of short/long arguments an...
Some assert macros to strengthen you code.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
void help(const std::string &free_args="") const
Print some help about the arguments.
const std::string & get_string(const std::string &arg_name) const
Get the string value of an argument.
std::list< double > get_all_of_real(const std::string &arg_name) const
Get all real values of an argument.
arguments_table(const std::string &prog_name)
Constructor.
bool has_value(const std::string &arg_name) const
Tell if an argument has a value.
bool only_integer_values(const std::string &arg_name) const
Tell if only integer values are associated to an argument.
int get_integer(const std::string &arg_name) const
Get the integer value of an argument.
const std::string & get_program_name() const
Get the name of the program.
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.
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.
void add_argument(const std::string &arg)
Add an argument in our list.
void parse(int &argc, char **&argv)
Parse the command line arguments.
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.
std::list< int > get_all_of_integer(const std::string &arg_name) const
Get all integer values of an argument.
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.
bool only_real_values(const std::string &arg_name) const
Tell if only real values are associated to an argument.
bool required_fields_are_set() const
Tell if all arguments not marqued as "optional" have been specified in the command line.
double get_real(const std::string &arg_name) const
Get the real value of an argument.
A class to manage sets of ordered items.
super::const_iterator const_iterator
The type of the iterator used to access non modifiable values.
Manipulation of mathematic, geometric, etc. items.
This is the main namespace.