Claw 1.7.3
arguments_table.cpp
Go to the documentation of this file.
1/*
2 CLAW - a C++ Library Absolutely Wonderful
3
4 CLAW is a free library without any particular aim but being useful to
5 anyone.
6
7 Copyright (C) 2005-2011 Julien Jorge
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23 contact: julien.jorge@gamned.org
24*/
31#include <claw/assert.hpp>
32#include <iostream>
33
34/*----------------------------------------------------------------------------*/
43claw::arguments_table::argument_attributes::argument_attributes
44( const std::string& name, const std::string& second_name,
45 const std::string& help_message, bool optional,
46 const std::string& value_type )
47 : m_name(name), m_second_name(second_name), m_help_message(help_message),
48 m_optional(optional), m_value_type(value_type)
49{
50
51} // arguments_table::argument_attributes::argument_attributes()
52
53/*----------------------------------------------------------------------------*/
58bool claw::arguments_table::argument_attributes::operator<
59 ( const argument_attributes& that ) const
60{
61 return m_name < that.m_name;
62} // arguments_table::argument_attributes::operator<()
63
64/*----------------------------------------------------------------------------*/
68std::string
69claw::arguments_table::argument_attributes::format_short_help() const
70{
71 std::string result(m_name);
72
73 if (!m_value_type.empty())
74 result += "=" + m_value_type;
75
76 if (m_optional)
77 return "[" + result + "]";
78 else
79 return result;
80} // arguments_table::argument_attributes::format_short_help()
81
82/*----------------------------------------------------------------------------*/
86std::string claw::arguments_table::argument_attributes::format_long_help() const
87{
88 std::string result(m_name);
89
90 if ( !m_second_name.empty() )
91 result += ", " + m_second_name;
92
93 return result + "\t" + m_help_message;
94} // arguments_table::argument_attributes::format_long_help()
95
96/*----------------------------------------------------------------------------*/
100const std::string& claw::arguments_table::argument_attributes::get_name() const
101{
102 return m_name;
103} // arguments_table::argument_attributes::get_name()
104
105/*----------------------------------------------------------------------------*/
109const std::string&
110claw::arguments_table::argument_attributes::get_second_name() const
111{
112 return m_second_name;
113} // arguments_table::argument_attributes::get_second_name()
114
115/*----------------------------------------------------------------------------*/
119bool claw::arguments_table::argument_attributes::is_optional() const
120{
121 return m_optional;
122} // arguments_table::argument_attributes::is_optional()
123
124
125
126
127/*----------------------------------------------------------------------------*/
132claw::arguments_table::arguments_table( const std::string& prog_name )
133 : m_arguments(prog_name)
134{
135
136} // arguments_table::arguments_table()
137
138/*----------------------------------------------------------------------------*/
147 : m_arguments(argc, argv, claw::math::ordered_set<std::string>() )
148{
149
150} // arguments_table::arguments_table()
151
152/*----------------------------------------------------------------------------*/
161void claw::arguments_table::add( const std::string& short_name,
162 const std::string& long_name,
163 const std::string& help_msg, bool optional,
164 const std::string& val_name )
165{
166 m_short_arguments.insert( argument_attributes(short_name, long_name, help_msg,
167 optional, val_name) );
168 m_long_arguments.insert( argument_attributes(long_name, short_name, help_msg,
169 optional, val_name) );
170} // arguments_table::add()
171
172/*----------------------------------------------------------------------------*/
180void claw::arguments_table::add_long( const std::string& long_name,
181 const std::string& help_msg,
182 bool optional,
183 const std::string& val_name )
184{
185 m_long_arguments.insert( argument_attributes(long_name, "", help_msg,
186 optional, val_name) );
187} // arguments_table::add_long()
188
189/*----------------------------------------------------------------------------*/
197void claw::arguments_table::add_short( const std::string& short_name,
198 const std::string& help_msg,
199 bool optional,
200 const std::string& val_name )
201{
202 m_short_arguments.insert( argument_attributes(short_name, "", help_msg,
203 optional, val_name) );
204} // arguments_table::add_short()
205
206/*----------------------------------------------------------------------------*/
214void claw::arguments_table::parse( int& argc, char** &argv )
215{
218
219 for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
220 allowed.insert(it->get_name());
221
222 for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
223 allowed.insert(it->get_name());
224
225 m_arguments.parse( argc, argv, allowed );
226} // arguments_table::parse()
227
228/*----------------------------------------------------------------------------*/
239void claw::arguments_table::help( const std::string& free_args ) const
240{
241 std::cout << m_arguments.get_program_name();
242
244
245 std::list<set_iterator> optional;
246 std::list<set_iterator>::const_iterator it_opt;
247 set_iterator it;
248
249 for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
250 if ( it->is_optional() )
251 optional.push_back(it);
252 else
253 std::cout << ' ' << it->format_short_help();
254
255 for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
256 if (it->get_second_name().empty())
257 {
258 if ( it->is_optional() )
259 optional.push_back(it);
260 else
261 std::cout << ' ' << it->format_short_help();
262 }
263
264 for (it_opt=optional.begin(); it_opt!=optional.end(); ++it_opt)
265 std::cout << ' ' << (*it_opt)->format_short_help();
266
267 if ( !free_args.empty() )
268 std::cout << ' ' << free_args;
269
270 std::cout << "\n\n";
271
272 for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
273 std::cout << "\t" << it->format_long_help() << std::endl;
274
275 for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
276 if (it->get_second_name().empty())
277 std::cout << "\t" << it->format_long_help() << std::endl;
278} // arguments_table::help()
279
280/*----------------------------------------------------------------------------*/
290{
291 bool ok = true;
293
294 for (it=m_short_arguments.begin(); (it!=m_short_arguments.end()) && ok; ++it)
295 if ( !it->is_optional() )
296 ok = ok && has_value(it->get_name());
297
298 for (it=m_long_arguments.begin(); (it!=m_long_arguments.end()) && ok; ++it)
299 if ( !it->is_optional() )
300 ok = ok && has_value(it->get_name());
301
302 return ok;
303} // arguments_table::required_fields_are_set()
304
305/*----------------------------------------------------------------------------*/
310bool claw::arguments_table::has_value( const std::string& arg_name ) const
311{
312 bool result = false;
313 std::string short_name, long_name;
314
315 get_argument_names( arg_name, short_name, long_name );
316
317 if ( !short_name.empty() )
318 result = m_arguments.has_value(short_name);
319
320 if (!result)
321 if ( !long_name.empty() )
322 result = m_arguments.has_value(long_name);
323
324 return result;
325} // arguments_table::has_value()
326
327/*----------------------------------------------------------------------------*/
332bool
333claw::arguments_table::only_integer_values( const std::string& arg_name ) const
334{
335 bool result = true;
336 std::string short_name, long_name;
337
338 get_argument_names( arg_name, short_name, long_name );
339
340 if ( short_name.empty() && long_name.empty() )
341 result = false;
342 else
343 {
344 if ( !short_name.empty() )
345 result = m_arguments.only_integer_values(short_name);
346
347 if ( !long_name.empty() )
348 result = result && m_arguments.only_integer_values(long_name);
349 }
350
351 return result;
352} // arguments_table::only_integer_values()
353
354/*----------------------------------------------------------------------------*/
359bool
360claw::arguments_table::only_real_values( const std::string& arg_name ) const
361{
362 bool result = true;
363 std::string short_name, long_name;
364
365 get_argument_names( arg_name, short_name, long_name );
366
367 if ( short_name.empty() && long_name.empty() )
368 result = false;
369 else
370 {
371 if ( !short_name.empty() )
372 result = m_arguments.only_real_values(short_name);
373
374 if ( !long_name.empty() )
375 result = result && m_arguments.only_real_values(long_name);
376 }
377
378 return result;
379} // arguments_table::only_real_values()
380
381/*----------------------------------------------------------------------------*/
386{
387 return m_arguments.get_program_name();
388} // arguments_table::has_value()
389
390/*----------------------------------------------------------------------------*/
395bool claw::arguments_table::get_bool( const std::string& arg_name ) const
396{
397 std::string short_name, long_name;
398
399 get_argument_names( arg_name, short_name, long_name );
400
401 return m_arguments.get_bool(short_name) || m_arguments.get_bool(long_name);
402} // arguments_table::get_bool()
403
404/*----------------------------------------------------------------------------*/
410int claw::arguments_table::get_integer( const std::string& arg_name ) const
411{
412 CLAW_PRECOND( has_value(arg_name) );
413
414 std::string short_name, long_name;
415
416 get_argument_names( arg_name, short_name, long_name );
417
418 if ( m_arguments.has_value(short_name) )
419 return m_arguments.get_integer(short_name);
420 else
421 return m_arguments.get_integer(long_name);
422} // arguments_table::get_integer()
423
424/*----------------------------------------------------------------------------*/
430double claw::arguments_table::get_real( const std::string& arg_name ) const
431{
432 CLAW_PRECOND( has_value(arg_name) );
433
434 std::string short_name, long_name;
435
436 get_argument_names( arg_name, short_name, long_name );
437
438 if ( m_arguments.has_value(short_name) )
439 return m_arguments.get_real(short_name);
440 else
441 return m_arguments.get_real(long_name);
442} // arguments_table::get_real()
443
444/*----------------------------------------------------------------------------*/
450const std::string&
451claw::arguments_table::get_string( const std::string& arg_name ) const
452{
453 CLAW_PRECOND( has_value(arg_name) );
454
455 std::string short_name, long_name;
456
457 get_argument_names( arg_name, short_name, long_name );
458
459 if ( m_arguments.has_value(short_name) )
460 return m_arguments.get_string(short_name);
461 else
462 return m_arguments.get_string(long_name);
463} // arguments_table::get_string()
464
465/*----------------------------------------------------------------------------*/
470std::list<int>
471claw::arguments_table::get_all_of_integer( const std::string& arg_name ) const
472{
473 std::list<int> result;
474 std::string short_name, long_name;
475
476 get_argument_names( arg_name, short_name, long_name );
477
478 if ( !short_name.empty() )
479 result = m_arguments.get_all_of_integer(short_name);
480
481 if ( !long_name.empty() )
482 {
483 const std::list<int> p(m_arguments.get_all_of_integer(long_name));
484 result.insert( result.end(), p.begin(), p.end() );
485 }
486
487 return result;
488} // arguments_table::get_all_of_integer()
489
490/*----------------------------------------------------------------------------*/
495std::list<double>
496claw::arguments_table::get_all_of_real( const std::string& arg_name ) const
497{
498 std::list<double> result;
499 std::string short_name, long_name;
500
501 get_argument_names( arg_name, short_name, long_name );
502
503 if ( !short_name.empty() )
504 result = m_arguments.get_all_of_real(short_name);
505
506 if ( !long_name.empty() )
507 {
508 const std::list<double> p(m_arguments.get_all_of_real(long_name));
509 result.insert( result.end(), p.begin(), p.end() );
510 }
511
512 return result;
513} // arguments_table::get_all_of_real()
514
515/*----------------------------------------------------------------------------*/
520std::list<std::string>
521claw::arguments_table::get_all_of_string( const std::string& arg_name ) const
522{
523 std::list<std::string> result;
524 std::string short_name, long_name;
525
526 get_argument_names( arg_name, short_name, long_name );
527
528 if ( !short_name.empty() )
529 result = m_arguments.get_all_of_string(short_name);
530
531 if ( !long_name.empty() )
532 {
533 const std::list<std::string> p(m_arguments.get_all_of_string(long_name));
534 result.insert( result.end(), p.begin(), p.end() );
535 }
536
537 return result;
538} // arguments_table::get_all_of_string()
539
540/*----------------------------------------------------------------------------*/
550void claw::arguments_table::add_argument( const std::string& arg )
551{
552 m_arguments.add_argument( arg );
553} // arguments_table::add_argument()
554
555/*----------------------------------------------------------------------------*/
562void claw::arguments_table::get_argument_names
563( const std::string& arg_name, std::string& short_name,
564 std::string& long_name ) const
565{
566 argument_attributes attr(arg_name, "", "", false, "");
568
569 // if arg_name is short, try to find the long version
570 it = m_short_arguments.find( attr );
571
572 if (it != m_short_arguments.end())
573 {
574 short_name = arg_name;
575 long_name = it->get_second_name();
576 }
577 else
578 {
579 // if arg_name is long, try to find the short version
580 it = m_long_arguments.find( attr );
581
582 if (it != m_long_arguments.end())
583 {
584 short_name = it->get_second_name();
585 long_name = arg_name;
586 }
587 }
588} // arguments_table::get_argument_names()
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.
Definition assert.hpp:98
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.
This is the main namespace.
Definition algorithm.hpp:34