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
/* *****************************************************************
    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 CLArgFlag.cpp
 *  \brief
 *  \author Jason Kraftcheck
 */

#include "Mesquite.hpp"
#include "CLArgFlag.hpp"
#include "ManPage.hpp"
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <sstream>

template < typename T >
static std::string stringify( T value )
{
    std::ostringstream ss;
    ss << value;
    return ss.str();
}

/************************* CLArgFlag ********************************/

CLArgFlag::~CLArgFlag() {}

std::string CLArgFlag::make_man_string( int count, const char* names[] ) const
{
    std::ostringstream ss;
    ManPage::begin_bold( ss ) << "-" << flag();
    ManPage::end_bold( ss );
    if( count )
    {
        ss << "<" << names[0];
        for( int i = 1; i < count; ++i )
        {
            ss << "|";
        }
        ss << ">";
    }
    return ss.str();
}

std::string CLArgFlag::make_literal_man_string( int count, const char* args[] ) const
{
    std::ostringstream ss;
    ManPage::begin_bold( ss ) << "-" << flag();
    ManPage::end_bold( ss );
    if( count )
    {
        ss << args[0];
        for( int i = 1; i < count; ++i )
            ManPage::bold( ss, args[i] );
    }
    return ss.str();
}

bool CLArgFlag::is_toggle() const
{
    return false;
}

bool CLArgFlag::add_set( int, const char* const* )
{
    return false;
}

/************************* CLArgToggle ********************************/

bool CLArgToggle::parse( const char* ) const
{
    if( !mCallback->value( mValue ) ) return false;
    mCallback->set_seen();
    return true;
}

std::string CLArgToggle::brief() const
{
    if( !mOpposite )
    {
        const char result[] = { '-', flag(), '\0' };
        return result;
    }
    else if( mOpposite->flag() < flag() )
    {
        return std::string();
    }
    else
    {
        const char result[] = { '-', flag(), '|', '-', mOpposite->flag(), '\0' };
        return result;
    }
}

std::string CLArgToggle::manstr() const
{
    if( !mOpposite )
    {
        return make_man_string();
    }
    else if( mOpposite->flag() > flag() )
    {
        std::ostringstream result;
        ManPage::begin_bold( result );
        result << "-" << flag();
        ManPage::end_bold( result );
        result << "|";
        ManPage::begin_bold( result );
        result << "-" << mOpposite->flag();
        ManPage::end_bold( result );
        return result.str();
    }
    else
    {
        return std::string();
    }
}

/************************* CLArgString ***********************************/

std::string CLArgString::brief() const
{
    std::ostringstream ss;
    ss << '-' << flag() << "  <" << mName << '>';
    return ss.str();
}

std::string CLArgString::manstr() const
{
    return make_man_string( mName.c_str() );
}

bool CLArgString::parse( const char* option ) const
{
    if( !mCallback->value( option ) ) return false;
    mCallback->set_seen();
    return true;
}

/************************* CLArgLong ***********************************/

std::string CLArgLong::brief() const
{
    const char str[] = { '-', flag(), ' ', '<', '\0' };
    std::string result( str );
    result += mName;
    result += ">";
    return result;
}
std::string CLArgLong::manstr() const
{
    return make_man_string( &mName[0] );
}

bool CLArgLong::parse( const char* option ) const
{
    long l;
    char* end_ptr;
    l = strtol( option, &end_ptr, 0 );
    if( *end_ptr || !mCallback->value( l ) ) return false;
    mCallback->set_seen();
    return true;
}

/************************* CLArgInt ***********************************/

std::string CLArgInt::brief() const
{
    const char str[] = { '-', flag(), ' ', '<', '\0' };
    std::string result( str );
    result += mName;
    result += ">";
    return result;
}
std::string CLArgInt::manstr() const
{
    return make_man_string( &mName[0] );
}

bool CLArgInt::parse( const char* option ) const
{
    long l;
    char* end_ptr;
    l     = strtol( option, &end_ptr, 0 );
    int i = (int)l;
    if( *end_ptr || (long)i != l || !mCallback->value( i ) ) return false;
    mCallback->set_seen();
    return true;
}

/************************* CLArgDouble ********************************/

std::string CLArgDouble::brief() const
{
    const char str[] = { '-', flag(), ' ', '<', '\0' };
    std::string result( str );
    result += mName;
    result += ">";
    return result;
}
std::string CLArgDouble::manstr() const
{
    return make_man_string( &mName[0] );
}

bool CLArgDouble::parse( const char* option ) const
{
    char* endptr;
    double val = strtod( option, &endptr );
    if( *endptr || !mCallback->value( val ) ) return false;
    mCallback->set_seen();
    return true;
}

/************************* CLArgIDList ********************************/

std::string CLArgIDList::brief() const
{
    const char str[] = { '-', flag(), '\0' };
    std::string result( str );
    result += " <id_list>";
    return result;
}
std::string CLArgIDList::manstr() const
{
    return make_man_string( "id_list" );
}

bool CLArgIDList::parse( const char* str ) const
{
    std::vector< int > list;
    long v;
    int i, j;
    char* endptr;
    for( ;; )
    {
        while( isspace( *str ) )
            ++str;
        if( !*str ) break;

        v = strtol( str, &endptr, 10 );
        if( endptr == str || v <= 0 ) return false;

        i = (int)v;
        if( (long)i != v ) return false;

        list.push_back( i );
        str = endptr;

        while( isspace( *str ) )
            ++str;

        if( *str == '-' )
        {
            do
            {
                ++str;
            } while( isspace( *str ) );
            v = strtol( str, &endptr, 10 );
            if( endptr == str || v < i ) return false;
            j = (int)v;
            if( (long)j != v ) return false;
            for( ++i; i < j; ++j )
                list.push_back( i );

            str = endptr;
            while( isspace( *str ) )
                ++str;
        }

        if( !*str ) break;
        if( *str != ',' ) return false;
        ++str;
    }

    if( list.empty() ) return false;

    if( !mCallback->value( list ) ) return false;
    mCallback->set_seen();
    return true;
}

/************************* CLArgListData ********************************/

bool CLArgListData::add_set( int size, const char* const* names )
{
    if( size < 0 || ( !mSets.empty() && acceptable_length( size ) ) ) return false;

    mSets.resize( mSets.size() + 1 );
    for( int i = 0; i < size; ++i )
        mSets.back().push_back( names[i] );
    return true;
}

bool CLArgListData::acceptable_length( unsigned len ) const
{
    for( unsigned i = 0; i < mSets.size(); ++i )
        if( len == mSets[i].size() ) return true;
    return mSets.empty();
}

std::string CLArgListData::set_string( int i ) const
{
    if( mSets[i].empty() ) return std::string();

    std::ostringstream result;
    result << mSets[i][0];
    for( unsigned j = 1; j < mSets[i].size(); ++j )
        result << "," << mSets[i][j];
    return result.str();
}

std::string CLArgListData::brief() const
{
    if( mSets.empty() ) return std::string();

    std::ostringstream result;
    for( unsigned i = 0; i < mSets.size(); ++i )
    {
        if( i ) result << '|';
        result << '{' << set_string( i ) << "}";
    }
    return result.str();
}

std::string CLArgListData::manstr( char type_char, const CLArgFlag& f ) const
{
    if( mSets.empty() )
    {
        const char argstr[] = { type_char, '1', ',', type_char, '2', ',', '.', '.', '.', '\0' };
        return f.make_man_string( argstr );
    }
    else
    {
        const char flagstr[] = { '-', f.flag(), '\0' };
        std::ostringstream ss;
        for( unsigned i = 0; i < mSets.size(); ++i )
        {
            if( i ) ss << "|";
            ManPage::begin_bold( ss );
            ss << flagstr;
            ManPage::end_bold( ss );
            ss << "<" << set_string( i ) << ">";
        }
        return ss.str();
    }
}

/************************* CLArgIntList ********************************/

std::string CLArgIntList::brief() const
{
    const char str[] = { '-', flag(), ' ', '\0' };
    std::string result( str );

    if( listData.accept_any_length() )
        result += "<n1,n2,...>";
    else
        result += listData.brief();

    return result;
}

std::string CLArgIntList::manstr() const
{
    return listData.manstr( 'n', *this );
}

bool CLArgIntList::parse( const char* str ) const
{
    long i;
    char* endptr;
    std::vector< int > list;
    for( ;; )
    {
        while( isspace( *str ) )
            ++str;

        if( !*str ) break;

        i = strtol( str, &endptr, 0 );
        list.push_back( i );

        while( isspace( *str ) )
            ++str;

        if( !*str ) break;

        if( *str != ',' ) return false;

        ++str;
    }

    if( !listData.acceptable_length( list.size() ) || !mCallback->value( list ) ) return false;

    mCallback->set_seen();
    return true;
}

/************************* CLArgDoubleList ********************************/

std::string CLArgDoubleList::brief() const
{
    const char str[] = { '-', flag(), ' ', '\0' };
    std::string result( str );

    if( listData.accept_any_length() )
        result += "<r1,r2,...>";
    else
        result += listData.brief();

    return result;
}

std::string CLArgDoubleList::manstr() const
{
    return listData.manstr( 'r', *this );
}

bool CLArgDoubleList::parse( const char* str ) const
{
    std::vector< double > list;
    char* endptr;
    for( ;; )
    {
        while( isspace( *str ) )
            ++str;
        if( !*str ) break;

        double d = strtod( str, &endptr );
        list.push_back( d );
        str = endptr;

        while( isspace( *str ) )
            ++str;
        if( !*str ) break;

        if( *str != ',' ) return false;
        ++str;
    }

    if( !listData.acceptable_length( list.size() ) || !mCallback->value( list ) ) return false;

    mCallback->set_seen();
    return true;
}