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 | /* *****************************************************************
MESQUITE -- The Mesh Quality Improvement Toolkit
Copyright 2007 Sandia National Laboratories. Developed at the
University of Wisconsin--Madison under SNL contract number
624796. The U.S. Government and the University of Wisconsin
retain certain rights to this software.
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
(lgpl.txt) along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
(2008) [email protected]
***************************************************************** */
/** \file CLArgs.cpp
* \brief
* \author Jason Kraftcheck
*/
#include "Mesquite.hpp"
#include "CLArgs.hpp"
#include "CLArgFlag.hpp"
#include "ManPage.hpp"
#include <iostream>
#include <sstream>
#include <limits>
#include <cstdlib>
const char HELP_FLAG = 'h';
const char MAN_FLAG = 'M';
class CLArgImpl
{
private:
std::vector< CLArgFlag* > mFlags;
std::vector< std::string > reqArgNames, optArgNames;
std::vector< std::string > reqArg, optArg;
std::string progName, shortDesc, longDesc;
public:
CLArgImpl( const char* progname, const char* brief, const char* desc )
: progName( progname ), shortDesc( brief ), longDesc( desc )
{
}
void add_req_arg( const char* name )
{
reqArgNames.push_back( name );
}
void add_opt_arg( const char* name )
{
optArgNames.push_back( name );
}
bool add_parsed_arg( const char* arg );
bool add_flag( CLArgFlag* flag );
CLArgFlag* find_flag( char flag );
const CLArgFlag* find_flag( char flag ) const
{
return const_cast< CLArgImpl* >( this )->find_flag( flag );
}
bool have_required_args() const
{
return reqArgNames.size() == reqArg.size();
}
void get_args( std::vector< std::string >& result )
{
result = reqArg;
result.resize( reqArg.size() + optArg.size() );
std::copy( optArg.begin(), optArg.end(), result.begin() + reqArg.size() );
}
void print_help( std::ostream& stream );
void print_brief_help( std::ostream& stream );
void print_man( std::ostream& stream );
void print_arg_names( std::ostream& stream );
};
bool CLArgImpl::add_flag( CLArgFlag* arg )
{
if( find_flag( arg->flag() ) ) return false;
mFlags.push_back( arg );
return true;
}
bool CLArgImpl::add_parsed_arg( const char* arg )
{
if( reqArg.size() < reqArgNames.size() )
reqArg.push_back( arg );
else if( optArg.size() < optArgNames.size() )
optArg.push_back( arg );
else
return false;
return true;
}
CLArgFlag* CLArgImpl::find_flag( char flag )
{
for( unsigned i = 0; i < mFlags.size(); ++i )
if( mFlags[i]->flag() == flag ) return mFlags[i];
return 0;
}
void CLArgImpl::print_help( std::ostream& stream )
{
stream << progName << " : " << shortDesc << std::endl;
stream << std::endl << longDesc << std::endl << std::endl;
print_brief_help( stream );
stream << '-' << HELP_FLAG << " : Print this help text." << std::endl;
stream << '-' << MAN_FLAG << " : Print man page text to standard output stream." << std::endl;
stream << "--"
<< " : Treat all subsequent arguments as non-flag arguments." << std::endl;
for( unsigned i = 0; i < mFlags.size(); ++i )
{
stream << '-' << mFlags[i]->flag() << " : " << mFlags[i]->desc();
std::string extra = mFlags[i]->callback()->desc_append();
if( !extra.empty() ) stream << " " << extra;
std::string defval = mFlags[i]->callback()->default_str();
if( !defval.empty() ) stream << " (default: " << defval << ")";
stream << std::endl;
}
}
void CLArgImpl::print_brief_help( std::ostream& stream )
{
stream << progName;
for( unsigned i = 0; i < mFlags.size(); ++i )
{
std::string str = mFlags[i]->callback()->brief();
if( !str.empty() )
{
stream << "[-" << mFlags[i]->flag() << ' ' << str << "]";
}
else
{
str = mFlags[i]->brief();
if( !str.empty() ) stream << " [" << str << "]";
}
}
print_arg_names( stream );
stream << std::endl;
stream << progName << " -" << HELP_FLAG << std::endl;
stream << progName << " -" << MAN_FLAG << std::endl;
}
void CLArgImpl::print_man( std::ostream& stream )
{
ManPage::begin_manpage( stream, progName, 1 );
ManPage::begin_section( stream, "NAME" );
ManPage::begin_paragraph( stream );
stream << progName << " - " << shortDesc << std::endl << std::endl;
ManPage::begin_section( stream, "SYNOPSIS" );
ManPage::begin_hanging_paragraph( stream );
ManPage::bold( stream, progName );
for( unsigned i = 0; i < mFlags.size(); ++i )
{
std::string s = mFlags[i]->callback()->manstr();
if( !s.empty() )
{
stream << '[';
ManPage::begin_bold( stream );
stream << '-' << mFlags[i]->flag();
ManPage::end_bold( stream );
stream << s << ']';
}
else
{
s = mFlags[i]->manstr();
if( !s.empty() ) stream << " [" << s << "]";
}
}
print_arg_names( stream );
stream << std::endl;
ManPage::begin_hanging_paragraph( stream );
ManPage::bold( stream, progName + " -h" );
ManPage::begin_hanging_paragraph( stream );
ManPage::bold( stream, progName + " -M" );
ManPage::begin_section( stream, "DESCRIPTION" );
ManPage::write_text( stream, false, longDesc );
ManPage::begin_section( stream, "OPTIONS" );
for( unsigned i = 0; i < mFlags.size(); ++i )
{
std::string s = mFlags[i]->callback()->manstr();
if( !s.empty() )
{
char tmp[] = { '-', mFlags[i]->flag(), ' ', '\0' };
s = std::string( tmp ) + s;
}
else
{
s = mFlags[i]->manstr();
if( s.empty() ) continue;
}
ManPage::begin_hanging_paragraph( stream );
stream << s;
ManPage::begin_indent( stream );
ManPage::begin_paragraph( stream );
stream << mFlags[i]->desc();
s = mFlags[i]->callback()->desc_append();
if( !s.empty() ) stream << " " << s;
std::string defval = mFlags[i]->callback()->default_str();
if( !defval.empty() ) stream << " (default: " << defval << ")";
ManPage::end_indent( stream );
}
}
void CLArgImpl::print_arg_names( std::ostream& stream )
{
unsigned i;
for( i = 0; i < reqArgNames.size(); ++i )
stream << " <" << reqArgNames[i] << ">";
for( i = 0; i < optArgNames.size(); ++i )
stream << " [" << optArgNames[i] << "]";
}
CLArgs::CLArgs( const char* progname, const char* brief, const char* desc )
{
impl = new CLArgImpl( progname, brief, desc );<--- Class 'CLArgs' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).<--- Class 'CLArgs' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).
}
CLArgs::~CLArgs()
{
delete impl;
}
bool CLArgs::is_flag_available( char fl ) const
{
return ( fl != HELP_FLAG ) && ( fl != MAN_FLAG ) && !( impl->find_flag( fl ) );
}
bool CLArgs::str_flag( char fl, const char* name, const char* desc, CLArgs::StringArgI* callback )<--- The function 'str_flag' is never used.
{
if( !is_flag_available( fl ) ) return false;
return impl->add_flag( new CLArgString( fl, name, desc, callback ) );
}
bool CLArgs::int_flag( char fl, const char* name, const char* desc, CLArgs::IntArgI* callback )
{
if( !is_flag_available( fl ) ) return false;
return impl->add_flag( new CLArgInt( fl, name, desc, callback ) );
}
bool CLArgs::long_flag( char fl, const char* name, const char* desc, CLArgs::LongArgI* callback )<--- The function 'long_flag' is never used.
{
if( !is_flag_available( fl ) ) return false;
return impl->add_flag( new CLArgLong( fl, name, desc, callback ) );
}
bool CLArgs::double_flag( char fl, const char* name, const char* desc, CLArgs::DoubleArgI* callback )
{
if( !is_flag_available( fl ) ) return false;
return impl->add_flag( new CLArgDouble( fl, name, desc, callback ) );
}
bool CLArgs::toggle_flag( char on_flag, char off_flag, const char* desc, CLArgs::ToggleArgI* callback )
{
if( !( is_flag_available( on_flag ) && is_flag_available( off_flag ) ) ) return false;
CLArgToggle* t1 = new CLArgToggle( on_flag, desc, true, callback );
impl->add_flag( t1 );
impl->add_flag( new CLArgToggle( off_flag, desc, t1 ) );
return true;
}
bool CLArgs::toggle_flag( char fl, const char* desc, CLArgs::ToggleArgI* callback )
{
if( !is_flag_available( fl ) ) return false;
return impl->add_flag( new CLArgToggle( fl, desc, true, callback ) );
}
bool CLArgs::id_list_flag( char fl, const char* desc, CLArgs::IntListArgI* callback )<--- The function 'id_list_flag' is never used.
{
if( !is_flag_available( fl ) ) return false;
return impl->add_flag( new CLArgIDList( fl, desc, callback ) );
}
bool CLArgs::int_list_flag( char fl, const char* desc, CLArgs::IntListArgI* callback )<--- The function 'int_list_flag' is never used.
{
if( !is_flag_available( fl ) ) return false;
return impl->add_flag( new CLArgIntList( fl, desc, callback ) );
}
bool CLArgs::double_list_flag( char fl, const char* desc, CLArgs::DoubleListArgI* callback )
{
if( !is_flag_available( fl ) ) return false;
return impl->add_flag( new CLArgDoubleList( fl, desc, callback ) );
}
bool CLArgs::limit_list_flag( char fl, int num_values, const char* const* value_names )
{
CLArgFlag* f = impl->find_flag( fl );
return f ? f->add_set( num_values, value_names ) : false;
}
void CLArgs::add_required_arg( const char* name )
{
impl->add_req_arg( name );
}
void CLArgs::add_optional_arg( const char* name )
{
impl->add_opt_arg( name );
}
bool CLArgs::parse_options( int argc, char** argv, std::vector< std::string >& args_out, std::ostream& error_stream )
{
std::vector< CLArgFlag* > pending;
bool no_more_flags = false;<--- Assignment 'no_more_flags=false', assigned value is 0
for( int i = 1; i < argc; ++i )
{
if( !pending.empty() )
{
CLArgFlag* flag = pending.front();
pending.erase( pending.begin() );
if( !flag->parse( argv[i] ) )
{
error_stream << argv[0] << ": invalid value for flag: -" << flag->flag() << " \"" << argv[i] << '"'
<< std::endl;
return false;
}
}
else if( !no_more_flags && argv[i][0] == '-' && argv[i][1] != '\0' )<--- Condition '!no_more_flags' is always true
{
for( int j = 1; argv[i][j]; ++j )
{
if( argv[i][j] == HELP_FLAG )
{
print_help( std::cout );
exit( 0 );
}
else if( argv[i][j] == MAN_FLAG )
{
print_man_page( std::cout );
exit( 0 );
}
CLArgFlag* flag = impl->find_flag( argv[i][j] );
if( !flag )
{
error_stream << argv[0] << ": invalid flag: -" << argv[i][j] << std::endl;
return false;
}
else if( !flag->is_toggle() )
{
pending.push_back( flag );
}
else if( !flag->parse( NULL ) )
{
error_stream << argv[0] << ": conflicting flag: -" << argv[i][j] << std::endl;
return false;
}
}
}
else if( !impl->add_parsed_arg( argv[i] ) )
{
error_stream << argv[0] << ": unexpected argument: \"" << argv[i] << '"' << std::endl;
return false;
}
}
impl->get_args( args_out );
if( !pending.empty() )
{
error_stream << argv[0] << ": expected argument following flag: -" << pending.front()->flag() << std::endl;
return false;
}
if( !impl->have_required_args() )
{
error_stream << argv[0] << ": insufficient arguments" << std::endl;
return false;
}
return true;
}
void CLArgs::print_help( std::ostream& stream ) const
{
impl->print_help( stream );
}
void CLArgs::print_man_page( std::ostream& stream ) const
{
impl->print_man( stream );
}
void CLArgs::print_usage( std::ostream& stream ) const
{
impl->print_brief_help( stream );
}
void CLArgs::KeyWordArg::initialize( const char* keyword_list[], int list_length )
{
mKeyWords.resize( list_length );
std::copy( keyword_list, keyword_list + list_length, mKeyWords.begin() );
}
bool CLArgs::KeyWordArg::value( const std::string& val )
{
std::vector< std::string >::const_iterator i;
for( i = mKeyWords.begin(); i != mKeyWords.end(); ++i )
if( compare_no_case( i->c_str(), val.c_str() ) )
{
return value( *i );
}
return false;
}
std::string CLArgs::KeyWordArg::brief() const
{
std::ostringstream ss;
std::vector< std::string >::const_iterator i = mKeyWords.begin();
if( i == mKeyWords.end() ) return std::string();
ss << '{' << *i;
for( ++i; i != mKeyWords.end(); ++i )
ss << '|' << *i;
ss << '}';
return ss.str();
}
std::string CLArgs::KeyWordArg::manstr() const
{
if( mKeyWords.empty() ) return std::string();
std::ostringstream ss;
ManPage::bold( ss, mKeyWords[0].c_str() );
for( unsigned i = 1; i < mKeyWords.size(); ++i )
{
ss << "|";
ManPage::bold( ss, mKeyWords[i].c_str() );
}
return ss.str();
}
bool CLArgs::KeyWordArg::compare_no_case( const char* s1, const char* s2 )
{
for( ; *s1; ++s1, ++s2 )
if( toupper( *s1 ) != toupper( *s2 ) ) return false;
return !*s2;
}
CLArgs::IntRange::IntRange( const int* min, const int* max )
: mMin( min ? *min : std::numeric_limits< int >::min() ), mMax( max ? *max : std::numeric_limits< int >::max() )
{
}
bool CLArgs::IntRange::is_valid( int val ) const
{
return val >= mMin && val <= mMax;
}
std::string CLArgs::IntRange::desc_append() const
{
std::ostringstream ss;
ss << "[" << mMin << "," << mMax << "]";
return ss.str();
}
bool CLArgs::IntRangeArg::value( const int& val )
{
if( !mRange.is_valid( val ) ) return false;
return IntArg::value( val );
}
bool CLArgs::IntListRangeArg::value( const std::vector< int >& val )
{
for( std::vector< int >::const_iterator i = val.begin(); i != val.end(); ++i )
if( !mRange.is_valid( *i ) ) return false;
return IntListArg::value( val );
}
CLArgs::DoubleRange::DoubleRange( const double* min, const double* max, bool inclusive )
: haveMin( min != NULL ), haveMax( max != NULL ), mInclusive( inclusive )
{
if( haveMin ) mMin = *min;
if( haveMax ) mMax = *max;
}
bool CLArgs::DoubleRange::is_valid( double val ) const
{
if( mInclusive )
return ( !haveMin || val >= mMin ) && ( !haveMax || val <= mMax );
else
return ( !haveMin || val > mMin ) && ( !haveMax || val < mMax );
}
std::string CLArgs::DoubleRange::desc_append() const
{
std::ostringstream ss;
if( mInclusive && haveMin )
ss << '[';
else
ss << '(';
if( haveMin )
ss << mMin;
else
ss << "-inf";
ss << ",";
if( haveMax )
ss << mMax;
else
ss << "inf";
if( mInclusive && haveMax )
ss << ']';
else
ss << ')';
return ss.str();
}
bool CLArgs::DoubleRangeArg::value( const double& val )
{
if( !mRange.is_valid( val ) ) return false;
return DoubleArg::value( val );
}
bool CLArgs::DoubleListRangeArg::value( const std::vector< double >& val )
{
for( std::vector< double >::const_iterator i = val.begin(); i != val.end(); ++i )
if( !mRange.is_valid( *i ) ) return false;
return DoubleListArg::value( val );
}
|