File: arguments_table.cpp

package info (click to toggle)
libclaw 1.7.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 13,287; sh: 227; makefile: 8
file content (588 lines) | stat: -rw-r--r-- 19,290 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
  CLAW - a C++ Library Absolutely Wonderful

  CLAW is a free library without any particular aim but being useful to 
  anyone.

  Copyright (C) 2005-2011 Julien Jorge

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  contact: julien.jorge@gamned.org
*/
/**
 * \file arguments_table.cpp
 * \brief Implementation of the claw::arguments_table class.
 * \author Julien Jorge
 */
#include <claw/arguments_table.hpp>
#include <claw/assert.hpp>
#include <iostream>

/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param name The principal name of the argument.
 * \param second_name The second name of the argument.
 * \param help_message Message describing the role of the argument.
 * \param optional Tell if this argument is optional.
 * \param value_type The kind of value needed for this argument.
 */
claw::arguments_table::argument_attributes::argument_attributes
( const std::string& name, const std::string& second_name,
  const std::string& help_message, bool optional,
  const std::string& value_type )
  : m_name(name), m_second_name(second_name), m_help_message(help_message),
    m_optional(optional), m_value_type(value_type)
{

} // arguments_table::argument_attributes::argument_attributes()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if the principal name of this argument is alphabetically less
 *        than the name of an other argument.
 */
bool claw::arguments_table::argument_attributes::operator<
  ( const argument_attributes& that ) const
{
  return m_name < that.m_name;
} // arguments_table::argument_attributes::operator<()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get a short description of the arguement.
 */
std::string
claw::arguments_table::argument_attributes::format_short_help() const
{
  std::string result(m_name);

  if (!m_value_type.empty())
    result += "=" + m_value_type;

  if (m_optional)
    return "[" + result + "]";
  else
    return result;
} // arguments_table::argument_attributes::format_short_help()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get a long description of the argument.
 */
std::string claw::arguments_table::argument_attributes::format_long_help() const
{
  std::string result(m_name);

  if ( !m_second_name.empty() )
    result += ", " + m_second_name;

  return result + "\t" + m_help_message;
} // arguments_table::argument_attributes::format_long_help()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the principal name of the argument.
 */
const std::string& claw::arguments_table::argument_attributes::get_name() const
{
  return m_name;
} // arguments_table::argument_attributes::get_name()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the second name of the argument.
 */
const std::string&
claw::arguments_table::argument_attributes::get_second_name() const
{
  return m_second_name;
} // arguments_table::argument_attributes::get_second_name()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if the argument is optional.
 */
bool claw::arguments_table::argument_attributes::is_optional() const
{
  return m_optional;
} // arguments_table::argument_attributes::is_optional()




/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param prog_name Force the name of the program.
 */
claw::arguments_table::arguments_table( const std::string& prog_name )
  : m_arguments(prog_name)
{

} // arguments_table::arguments_table()

/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param argc Number of arguments.
 * \param argv Arguments.
 *
 * All supported arguments will be removed from argv.
 */
claw::arguments_table::arguments_table( int& argc, char** &argv )
  : m_arguments(argc, argv, claw::math::ordered_set<std::string>() )
{

} // arguments_table::arguments_table()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add an argument in the table.
 * \param short_name The short name of the argument.
 * \param long_name The long name of the argument.
 * \param help_msg A description of the argument.
 * \param optional Tell if the argument is optional.
 * \param val_name The type of the value needed for this argument.
 */
void claw::arguments_table::add( const std::string& short_name,
                                 const std::string& long_name,
                                 const std::string& help_msg, bool optional,
                                 const std::string& val_name )
{
  m_short_arguments.insert( argument_attributes(short_name, long_name, help_msg,
                                                optional, val_name) );
  m_long_arguments.insert( argument_attributes(long_name, short_name, help_msg,
                                               optional, val_name) );
} // arguments_table::add()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add an argument in the table.
 * \param long_name The long name of the argument.
 * \param help_msg A description of the argument.
 * \param optional Tell if the argument is optional.
 * \param val_name The type of the value needed for this argument.
 */
void claw::arguments_table::add_long( const std::string& long_name,
                                      const std::string& help_msg,
                                      bool optional,
                                      const std::string& val_name )
{
  m_long_arguments.insert( argument_attributes(long_name, "", help_msg,
                                               optional, val_name) );
} // arguments_table::add_long()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add an argument in the table.
 * \param short_name The short name of the argument.
 * \param help_msg A description of the argument.
 * \param optional Tell if the argument is optional.
 * \param val_name The type of the value needed for this argument.
 */
void claw::arguments_table::add_short( const std::string& short_name,
                                       const std::string& help_msg,
                                       bool optional,
                                       const std::string& val_name )
{
  m_short_arguments.insert( argument_attributes(short_name, "", help_msg,
                                                optional, val_name) );
} // arguments_table::add_short()
    
/*----------------------------------------------------------------------------*/
/**
 * \brief Parse the command line arguments.
 * \param argc Number of arguments.
 * \param argv Arguments.
 *
 * All supported arguments will be removed from argv.
 */
void claw::arguments_table::parse( int& argc, char** &argv )
{
  math::ordered_set<std::string> allowed;
  math::ordered_set<argument_attributes>::const_iterator it;

  for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
    allowed.insert(it->get_name());

  for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
    allowed.insert(it->get_name());

  m_arguments.parse( argc, argv, allowed );
} // arguments_table::parse()

/*----------------------------------------------------------------------------*/
/**
 * \brief Print some help about the arguments.
 * \param free_args The arguments of your program that are not managed by
 *        claw::arguments_table.
 *
 * The method prints the name of the program, required arguments, optional
 * arguments and, then, \a free_args. Arguments are printed in short format when
 * available. A line is then skipped and the long description of the arguments
 * is printed.
 */
void claw::arguments_table::help( const std::string& free_args ) const
{
  std::cout << m_arguments.get_program_name();

  typedef math::ordered_set<argument_attributes>::const_iterator set_iterator;

  std::list<set_iterator> optional;
  std::list<set_iterator>::const_iterator it_opt;
  set_iterator it;

  for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
    if ( it->is_optional() )
      optional.push_back(it);
    else
      std::cout << ' ' << it->format_short_help();

  for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
    if (it->get_second_name().empty())
      {
	if ( it->is_optional() )
	  optional.push_back(it);
	else
	  std::cout << ' ' << it->format_short_help();
      }

  for (it_opt=optional.begin(); it_opt!=optional.end(); ++it_opt)
    std::cout << ' ' << (*it_opt)->format_short_help();

  if ( !free_args.empty() )
    std::cout << ' ' << free_args;

  std::cout << "\n\n";

  for (it=m_short_arguments.begin(); it!=m_short_arguments.end(); ++it)
    std::cout << "\t" << it->format_long_help() << std::endl;

  for (it=m_long_arguments.begin(); it!=m_long_arguments.end(); ++it)
    if (it->get_second_name().empty())
      std::cout << "\t" << it->format_long_help() << std::endl;
} // arguments_table::help()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if all arguments not marqued as "optional" have been specified in
 *        the command line.
 *
 * \remark The method doesn't check the value of the arguments. If, for example,
 *         an argument needs to be an integer and the user use it as a boolean,
 *         the method will return true.
 */
bool claw::arguments_table::required_fields_are_set() const
{
  bool ok = true;
  math::ordered_set<argument_attributes>::const_iterator it;

  for (it=m_short_arguments.begin(); (it!=m_short_arguments.end()) && ok; ++it)
    if ( !it->is_optional() )
      ok = ok && has_value(it->get_name());

  for (it=m_long_arguments.begin(); (it!=m_long_arguments.end()) && ok; ++it)
    if ( !it->is_optional() )
      ok = ok && has_value(it->get_name());

  return ok;
} // arguments_table::required_fields_are_set()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if an argument has a value.
 * \param arg_name The name of the argument to find.
 */
bool claw::arguments_table::has_value( const std::string& arg_name ) const
{
  bool result = false;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( !short_name.empty() )
    result = m_arguments.has_value(short_name);

  if (!result)
    if ( !long_name.empty() )
      result = m_arguments.has_value(long_name);

  return result;
} // arguments_table::has_value()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if only integer values are associated to an argument.
 * \param arg_name The name of the argument to test.
 */
bool
claw::arguments_table::only_integer_values( const std::string& arg_name ) const
{
  bool result = true;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( short_name.empty() && long_name.empty() )
    result = false;
  else
    {
      if ( !short_name.empty() )
        result = m_arguments.only_integer_values(short_name);

      if ( !long_name.empty() )
        result = result && m_arguments.only_integer_values(long_name);
    }

  return result;
} // arguments_table::only_integer_values()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if only real values are associated to an argument.
 * \param arg_name The name of the argument to test.
 */
bool
claw::arguments_table::only_real_values( const std::string& arg_name ) const
{
  bool result = true;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( short_name.empty() && long_name.empty() )
    result = false;
  else
    {
      if ( !short_name.empty() )
        result = m_arguments.only_real_values(short_name);

      if ( !long_name.empty() )
        result = result && m_arguments.only_real_values(long_name);
    }

  return result;
} // arguments_table::only_real_values()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the name of the program.
 */
const std::string& claw::arguments_table::get_program_name() const
{
  return m_arguments.get_program_name();
} // arguments_table::has_value()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the boolean state of an argument.
 * \param arg_name The name of the argument to find.
 */
bool claw::arguments_table::get_bool( const std::string& arg_name ) const
{
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  return m_arguments.get_bool(short_name) || m_arguments.get_bool(long_name);
} // arguments_table::get_bool()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the integer value of an argument.
 * \param arg_name The name of the argument to find.
 * \pre has_value(arg_name)
 */
int claw::arguments_table::get_integer( const std::string& arg_name ) const
{
  CLAW_PRECOND( has_value(arg_name) );

  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( m_arguments.has_value(short_name) )
    return m_arguments.get_integer(short_name);
  else
    return m_arguments.get_integer(long_name);
} // arguments_table::get_integer()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the real value of an argument.
 * \param arg_name The name of the argument to find.
 * \pre has_value(arg_name)
 */
double claw::arguments_table::get_real( const std::string& arg_name ) const
{
  CLAW_PRECOND( has_value(arg_name) );

  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( m_arguments.has_value(short_name) )
    return m_arguments.get_real(short_name);
  else
    return m_arguments.get_real(long_name);
} // arguments_table::get_real()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the string value of an argument.
 * \param arg_name The name of the argument to find.
 * \pre has_value(arg_name)
 */
const std::string&
claw::arguments_table::get_string( const std::string& arg_name ) const
{
  CLAW_PRECOND( has_value(arg_name) );

  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( m_arguments.has_value(short_name) )
    return m_arguments.get_string(short_name);
  else
    return m_arguments.get_string(long_name);
} // arguments_table::get_string()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get all integer values of an argument.
 * \param arg_name The name of the argument to get.
 */
std::list<int>
claw::arguments_table::get_all_of_integer( const std::string& arg_name ) const
{
  std::list<int> result;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( !short_name.empty() )
    result = m_arguments.get_all_of_integer(short_name);

  if ( !long_name.empty() )
    {
      const std::list<int> p(m_arguments.get_all_of_integer(long_name));
      result.insert( result.end(), p.begin(), p.end() );
    }

  return result;
} // arguments_table::get_all_of_integer()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get all real values of an argument.
 * \param arg_name The name of the argument to get.
 */
std::list<double>
claw::arguments_table::get_all_of_real( const std::string& arg_name ) const
{
  std::list<double> result;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( !short_name.empty() )
    result = m_arguments.get_all_of_real(short_name);

  if ( !long_name.empty() )
    {
      const std::list<double> p(m_arguments.get_all_of_real(long_name));
      result.insert( result.end(), p.begin(), p.end() );
    }

  return result;
} // arguments_table::get_all_of_real()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get all string values of an argument.
 * \param arg_name The name of the argument to get.
 */
std::list<std::string>
claw::arguments_table::get_all_of_string( const std::string& arg_name ) const
{
  std::list<std::string> result;
  std::string short_name, long_name;

  get_argument_names( arg_name, short_name, long_name );

  if ( !short_name.empty() )
    result = m_arguments.get_all_of_string(short_name);

  if ( !long_name.empty() )
    {
      const std::list<std::string> p(m_arguments.get_all_of_string(long_name));
      result.insert( result.end(), p.begin(), p.end() );
    }

  return result;
} // arguments_table::get_all_of_string()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add an argument in our list.
 *
 * You can use this method to set default values to the parameters of your
 * program, before calling parse.
 *
 * \param arg The argument to add.
 * \pre (arg != "--") && (arg[0] == '-')
 */
void claw::arguments_table::add_argument( const std::string& arg )
{
  m_arguments.add_argument( arg );
} // arguments_table::add_argument()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the principal name and the second name of an argument.
 * \param arg_name The name of the argument to find.
 * \param short_name The short name of the argument.
 * \param long_name The long name of the argument.
 */
void claw::arguments_table::get_argument_names
( const std::string& arg_name, std::string& short_name,
  std::string& long_name ) const
{
  argument_attributes attr(arg_name, "", "", false, "");
  math::ordered_set<argument_attributes>::const_iterator it;

  // if arg_name is short, try to find the long version
  it = m_short_arguments.find( attr );

  if (it != m_short_arguments.end())
    {
      short_name = arg_name;
      long_name = it->get_second_name();
    }
  else
    {
      // if arg_name is long, try to find the short version
      it = m_long_arguments.find( attr );
          
      if (it != m_long_arguments.end())
        {
          short_name = it->get_second_name();
          long_name = arg_name;
        }
    }
} // arguments_table::get_argument_names()