~ubuntu-branches/ubuntu/trusty/hugin/trusty-proposed

« back to all changes in this revision

Viewing changes to src/foreign/tclap/CmdLine.h

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2011-01-06 14:28:24 UTC
  • mfrom: (1.1.9 upstream) (0.1.21 experimental)
  • Revision ID: james.westby@ubuntu.com-20110106142824-zn9lxylg5z44dynn
* Drop Cyril Brulebois from Uploaders. Thank you very much for your work.
* Bump package version. (rc3 was re-released as 2010.4.0).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************************************
 
3
 *
 
4
 *  file:  CmdLine.h
 
5
 *
 
6
 *  Copyright (c) 2003, Michael E. Smoot .
 
7
 *  Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
 
8
 *  All rights reverved.
 
9
 *
 
10
 *  See the file COPYING in the top directory of this distribution for
 
11
 *  more information.
 
12
 *
 
13
 *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
14
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
15
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
16
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
17
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
18
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
19
 *  DEALINGS IN THE SOFTWARE.
 
20
 *
 
21
 *****************************************************************************/
 
22
 
 
23
#ifndef TCLAP_CMDLINE_H
 
24
#define TCLAP_CMDLINE_H
 
25
 
 
26
#include <tclap/SwitchArg.h>
 
27
#include <tclap/MultiSwitchArg.h>
 
28
#include <tclap/UnlabeledValueArg.h>
 
29
#include <tclap/UnlabeledMultiArg.h>
 
30
 
 
31
#include <tclap/XorHandler.h>
 
32
#include <tclap/HelpVisitor.h>
 
33
#include <tclap/VersionVisitor.h>
 
34
#include <tclap/IgnoreRestVisitor.h>
 
35
 
 
36
#include <tclap/CmdLineOutput.h>
 
37
#include <tclap/StdOutput.h>
 
38
 
 
39
#include <tclap/Constraint.h>
 
40
#include <tclap/ValuesConstraint.h>
 
41
 
 
42
#include <string>
 
43
#include <vector>
 
44
#include <list>
 
45
#include <iostream>
 
46
#include <iomanip>
 
47
#include <algorithm>
 
48
 
 
49
namespace TCLAP {
 
50
 
 
51
/**
 
52
 * The base class that manages the command line definition and passes
 
53
 * along the parsing to the appropriate Arg classes.
 
54
 */
 
55
class CmdLine : public CmdLineInterface
 
56
{
 
57
        protected:
 
58
 
 
59
                /**
 
60
                 * The list of arguments that will be tested against the
 
61
                 * command line.
 
62
                 */
 
63
                std::list<Arg*> _argList;
 
64
 
 
65
                /**
 
66
                 * The name of the program.  Set to argv[0].
 
67
                 */
 
68
                std::string _progName;
 
69
 
 
70
                /**
 
71
                 * A message used to describe the program.  Used in the usage output.
 
72
                 */
 
73
                std::string _message;
 
74
 
 
75
                /**
 
76
                 * The version to be displayed with the --version switch.
 
77
                 */
 
78
                std::string _version;
 
79
 
 
80
                /**
 
81
                 * The number of arguments that are required to be present on
 
82
                 * the command line. This is set dynamically, based on the
 
83
                 * Args added to the CmdLine object.
 
84
                 */
 
85
                int _numRequired;
 
86
 
 
87
                /**
 
88
                 * The character that is used to separate the argument flag/name
 
89
                 * from the value.  Defaults to ' ' (space).
 
90
                 */
 
91
                char _delimiter;
 
92
 
 
93
                /**
 
94
                 * The handler that manages xoring lists of args.
 
95
                 */
 
96
                XorHandler _xorHandler;
 
97
 
 
98
                /**
 
99
                 * A list of Args to be explicitly deleted when the destructor
 
100
                 * is called.  At the moment, this only includes the three default
 
101
                 * Args.
 
102
                 */
 
103
                std::list<Arg*> _argDeleteOnExitList;
 
104
 
 
105
                /**
 
106
                 * A list of Visitors to be explicitly deleted when the destructor
 
107
                 * is called.  At the moment, these are the Vistors created for the
 
108
                 * default Args.
 
109
                 */
 
110
                std::list<Visitor*> _visitorDeleteOnExitList;
 
111
 
 
112
                /**
 
113
                 * Object that handles all output for the CmdLine.
 
114
                 */
 
115
                CmdLineOutput* _output;
 
116
 
 
117
                /**
 
118
                 * Checks whether a name/flag string matches entirely matches
 
119
                 * the Arg::blankChar.  Used when multiple switches are combined
 
120
                 * into a single argument.
 
121
                 * \param s - The message to be used in the usage.
 
122
                 */
 
123
                bool _emptyCombined(const std::string& s);
 
124
 
 
125
                /**
 
126
                 * Perform a delete ptr; operation on ptr when this object is deleted.
 
127
                 */
 
128
                void deleteOnExit(Arg* ptr);
 
129
 
 
130
                /**
 
131
                 * Perform a delete ptr; operation on ptr when this object is deleted.
 
132
                 */
 
133
                void deleteOnExit(Visitor* ptr);
 
134
 
 
135
        private:
 
136
 
 
137
                /**
 
138
                 * Encapsulates the code common to the constructors (which is all
 
139
                 * of it).
 
140
                 */
 
141
                void _constructor();
 
142
 
 
143
                /**
 
144
                 * Is set to true when a user sets the output object. We use this so
 
145
                 * that we don't delete objects that are created outside of this lib.
 
146
                 */
 
147
                bool _userSetOutput;
 
148
 
 
149
                /**
 
150
                 * Whether or not to automatically create help and version switches.
 
151
                 */
 
152
                bool _helpAndVersion;
 
153
 
 
154
        public:
 
155
 
 
156
                /**
 
157
                 * Command line constructor. Defines how the arguments will be
 
158
                 * parsed.
 
159
                 * \param message - The message to be used in the usage
 
160
                 * output.
 
161
                 * \param delimiter - The character that is used to separate
 
162
                 * the argument flag/name from the value.  Defaults to ' ' (space).
 
163
                 * \param version - The version number to be used in the
 
164
                 * --version switch.
 
165
                 * \param helpAndVersion - Whether or not to create the Help and
 
166
                 * Version switches. Defaults to true.
 
167
                 */
 
168
                CmdLine(const std::string& message,
 
169
                                const char delimiter = ' ',
 
170
                                const std::string& version = "none",
 
171
                                bool helpAndVersion = true);
 
172
 
 
173
                /**
 
174
                 * Deletes any resources allocated by a CmdLine object.
 
175
                 */
 
176
                virtual ~CmdLine();
 
177
 
 
178
                /**
 
179
                 * Adds an argument to the list of arguments to be parsed.
 
180
                 * \param a - Argument to be added.
 
181
                 */
 
182
                void add( Arg& a );
 
183
 
 
184
                /**
 
185
                 * An alternative add.  Functionally identical.
 
186
                 * \param a - Argument to be added.
 
187
                 */
 
188
                void add( Arg* a );
 
189
 
 
190
                /**
 
191
                 * Add two Args that will be xor'd.  If this method is used, add does
 
192
                 * not need to be called.
 
193
                 * \param a - Argument to be added and xor'd.
 
194
                 * \param b - Argument to be added and xor'd.
 
195
                 */
 
196
                void xorAdd( Arg& a, Arg& b );
 
197
 
 
198
                /**
 
199
                 * Add a list of Args that will be xor'd.  If this method is used,
 
200
                 * add does not need to be called.
 
201
                 * \param xors - List of Args to be added and xor'd.
 
202
                 */
 
203
                void xorAdd( std::vector<Arg*>& xors );
 
204
 
 
205
                /**
 
206
                 * Parses the command line.
 
207
                 * \param argc - Number of arguments.
 
208
                 * \param argv - Array of arguments.
 
209
                 */
 
210
                void parse(int argc, char** argv);
 
211
 
 
212
                /**
 
213
                 *
 
214
                 */
 
215
                CmdLineOutput* getOutput();
 
216
 
 
217
                /**
 
218
                 *
 
219
                 */
 
220
                void setOutput(CmdLineOutput* co);
 
221
 
 
222
                /**
 
223
                 *
 
224
                 */
 
225
                std::string& getVersion();
 
226
 
 
227
                /**
 
228
                 *
 
229
                 */
 
230
                std::string& getProgramName();
 
231
 
 
232
                /**
 
233
                 *
 
234
                 */
 
235
                std::list<Arg*>& getArgList();
 
236
 
 
237
                /**
 
238
                 *
 
239
                 */
 
240
                XorHandler& getXorHandler();
 
241
 
 
242
                /**
 
243
                 *
 
244
                 */
 
245
                char getDelimiter();
 
246
 
 
247
                /**
 
248
                 *
 
249
                 */
 
250
                std::string& getMessage();
 
251
 
 
252
                /**
 
253
                 *
 
254
                 */
 
255
                bool hasHelpAndVersion();
 
256
};
 
257
 
 
258
 
 
259
///////////////////////////////////////////////////////////////////////////////
 
260
//Begin CmdLine.cpp
 
261
///////////////////////////////////////////////////////////////////////////////
 
262
 
 
263
inline CmdLine::CmdLine(const std::string& m,
 
264
                                        char delim,
 
265
                                                const std::string& v,
 
266
                                                bool help )
 
267
: _progName("not_set_yet"),
 
268
  _message(m),
 
269
  _version(v),
 
270
  _numRequired(0),
 
271
  _delimiter(delim),
 
272
  _userSetOutput(false),
 
273
  _helpAndVersion(help)
 
274
{
 
275
        _constructor();
 
276
}
 
277
 
 
278
inline CmdLine::~CmdLine()
 
279
{
 
280
        ArgListIterator argIter;
 
281
        VisitorListIterator visIter;
 
282
 
 
283
        for( argIter = _argDeleteOnExitList.begin();
 
284
                 argIter != _argDeleteOnExitList.end();
 
285
                 ++argIter)
 
286
                delete *argIter;
 
287
 
 
288
        for( visIter = _visitorDeleteOnExitList.begin();
 
289
                 visIter != _visitorDeleteOnExitList.end();
 
290
                 ++visIter)
 
291
                delete *visIter;
 
292
 
 
293
        if ( !_userSetOutput )
 
294
                delete _output;
 
295
}
 
296
 
 
297
inline void CmdLine::_constructor()
 
298
{
 
299
        _output = new StdOutput;
 
300
 
 
301
        Arg::setDelimiter( _delimiter );
 
302
 
 
303
        Visitor* v;
 
304
 
 
305
        if ( _helpAndVersion )
 
306
        {
 
307
                v = new HelpVisitor( this, &_output );
 
308
                SwitchArg* help = new SwitchArg("h","help",
 
309
                                                "Displays usage information and exits.",
 
310
                                                false, v);
 
311
                add( help );
 
312
                deleteOnExit(help);
 
313
                deleteOnExit(v);
 
314
 
 
315
                v = new VersionVisitor( this, &_output );
 
316
                SwitchArg* vers = new SwitchArg("","version",
 
317
                                        "Displays version information and exits.",
 
318
                                        false, v);
 
319
                add( vers );
 
320
                deleteOnExit(vers);
 
321
                deleteOnExit(v);
 
322
        }
 
323
 
 
324
        v = new IgnoreRestVisitor();
 
325
        SwitchArg* ignore  = new SwitchArg(Arg::flagStartString(),
 
326
                                           Arg::ignoreNameString(),
 
327
                           "Ignores the rest of the labeled arguments following this flag.",
 
328
                                           false, v);
 
329
        add( ignore );
 
330
        deleteOnExit(ignore);
 
331
        deleteOnExit(v);
 
332
}
 
333
 
 
334
inline void CmdLine::xorAdd( std::vector<Arg*>& ors )
 
335
{
 
336
        _xorHandler.add( ors );
 
337
 
 
338
        for (ArgVectorIterator it = ors.begin(); it != ors.end(); it++)
 
339
        {
 
340
                (*it)->forceRequired();
 
341
                (*it)->setRequireLabel( "OR required" );
 
342
 
 
343
                add( *it );
 
344
        }
 
345
}
 
346
 
 
347
inline void CmdLine::xorAdd( Arg& a, Arg& b )
 
348
{
 
349
    std::vector<Arg*> ors;
 
350
    ors.push_back( &a );
 
351
    ors.push_back( &b );
 
352
        xorAdd( ors );
 
353
}
 
354
 
 
355
inline void CmdLine::add( Arg& a )
 
356
{
 
357
        add( &a );
 
358
}
 
359
 
 
360
inline void CmdLine::add( Arg* a )
 
361
{
 
362
        for( ArgListIterator it = _argList.begin(); it != _argList.end(); it++ )
 
363
                if ( *a == *(*it) )
 
364
                        throw( SpecificationException(
 
365
                                "Argument with same flag/name already exists!",
 
366
                                        a->longID() ) );
 
367
 
 
368
        a->addToList( _argList );
 
369
 
 
370
        if ( a->isRequired() )
 
371
                _numRequired++;
 
372
}
 
373
 
 
374
inline void CmdLine::parse(int argc, char** argv)
 
375
{
 
376
        try {
 
377
 
 
378
        _progName = argv[0];
 
379
 
 
380
        // this step is necessary so that we have easy access to mutable strings.
 
381
        std::vector<std::string> args;
 
382
        for (int i = 1; i < argc; i++)
 
383
                args.push_back(argv[i]);
 
384
 
 
385
        int requiredCount = 0;
 
386
 
 
387
        for (int i = 0; static_cast<unsigned int>(i) < args.size(); i++)
 
388
        {
 
389
                bool matched = false;
 
390
                for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++)
 
391
        {
 
392
                        if ( (*it)->processArg( &i, args ) )
 
393
                        {
 
394
                                requiredCount += _xorHandler.check( *it );
 
395
                                matched = true;
 
396
                                break;
 
397
                        }
 
398
        }
 
399
 
 
400
                // checks to see if the argument is an empty combined switch ...
 
401
                // and if so, then we've actually matched it
 
402
                if ( !matched && _emptyCombined( args[i] ) )
 
403
                        matched = true;
 
404
 
 
405
                if ( !matched && !Arg::ignoreRest() )
 
406
                        throw(CmdLineParseException("Couldn't find match for argument",
 
407
                                                     args[i]));
 
408
    }
 
409
 
 
410
        if ( requiredCount < _numRequired )
 
411
                throw(CmdLineParseException("One or more required arguments missing!"));
 
412
 
 
413
        if ( requiredCount > _numRequired )
 
414
                throw(CmdLineParseException("Too many arguments!"));
 
415
 
 
416
        } catch ( ArgException e ) { _output->failure(*this,e); exit(1); }
 
417
}
 
418
 
 
419
inline bool CmdLine::_emptyCombined(const std::string& s)
 
420
{
 
421
        if ( s[0] != Arg::flagStartChar() )
 
422
                return false;
 
423
 
 
424
        for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
 
425
                if ( s[i] != Arg::blankChar() )
 
426
                        return false;
 
427
 
 
428
        return true;
 
429
}
 
430
 
 
431
inline void CmdLine::deleteOnExit(Arg* ptr)
 
432
{
 
433
        _argDeleteOnExitList.push_back(ptr);
 
434
}
 
435
 
 
436
inline void CmdLine::deleteOnExit(Visitor* ptr)
 
437
{
 
438
        _visitorDeleteOnExitList.push_back(ptr);
 
439
}
 
440
 
 
441
inline CmdLineOutput* CmdLine::getOutput()
 
442
{
 
443
        return _output;
 
444
}
 
445
 
 
446
inline void CmdLine::setOutput(CmdLineOutput* co)
 
447
{
 
448
        _userSetOutput = true;
 
449
        _output = co;
 
450
}
 
451
 
 
452
inline std::string& CmdLine::getVersion()
 
453
{
 
454
        return _version;
 
455
}
 
456
 
 
457
inline std::string& CmdLine::getProgramName()
 
458
{
 
459
        return _progName;
 
460
}
 
461
 
 
462
inline std::list<Arg*>& CmdLine::getArgList()
 
463
{
 
464
        return _argList;
 
465
}
 
466
 
 
467
inline XorHandler& CmdLine::getXorHandler()
 
468
{
 
469
        return _xorHandler;
 
470
}
 
471
 
 
472
inline char CmdLine::getDelimiter()
 
473
{
 
474
        return _delimiter;
 
475
}
 
476
 
 
477
inline std::string& CmdLine::getMessage()
 
478
{
 
479
        return _message;
 
480
}
 
481
 
 
482
inline bool CmdLine::hasHelpAndVersion()
 
483
{
 
484
        return _helpAndVersion;
 
485
}
 
486
 
 
487
///////////////////////////////////////////////////////////////////////////////
 
488
//End CmdLine.cpp
 
489
///////////////////////////////////////////////////////////////////////////////
 
490
 
 
491
 
 
492
 
 
493
} //namespace TCLAP
 
494
#endif