summaryrefslogtreecommitdiffstats
path: root/src/tclap/Arg.h
blob: a65822bdce345e7ac204818f7a9f69262f349835 (plain)
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-

/******************************************************************************
 *
 *  file:  Arg.h
 *
 *  Copyright (c) 2003, Michael E. Smoot .
 *  Copyright (c) 2004, Michael E. Smoot, Daniel Aarno .
 *  All rights reverved.
 *
 *  See the file COPYING in the top directory of this distribution for
 *  more information.
 *
 *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *  DEALINGS IN THE SOFTWARE.
 *
 *****************************************************************************/


#ifndef TCLAP_ARGUMENT_H
#define TCLAP_ARGUMENT_H

#ifdef HAVE_CONFIG_H
 #include <config.h>
#else
 #define HAVE_SSTREAM
#endif

#include <string>
#include <vector>
#include <list>
#include <iostream>
#include <iomanip>
#include <cstdio>

#if defined (HAVE_SSTREAM)
 #include <sstream>
typedef std::istringstream istringstream;
#elif defined (HAVE_STRSTREAM)
 #include <strstream>
typedef std::istrstream istringstream;
#else
 #error "Need a stringstream (sstream or strstream) to compile!"
#endif

#include <tclap/ArgException.h>
#include <tclap/Visitor.h>
#include <tclap/CmdLineInterface.h>
#include <tclap/ArgTraits.h>
#include <tclap/StandardTraits.h>

namespace TCLAP
{
	/**
	 * A virtual base class that defines the essential data for all arguments.
	 * This class, or one of its existing children, must be subclassed to do
	 * anything.
	 */
	class Arg
	{
		private:

			/**
			 * Indicates whether the rest of the arguments should be ignored.
			 */
			static bool& ignoreRestRef()
			{
				static bool ign = false;
				return ign;
			}

			/**
			 * The delimiter that separates an argument flag/name from the
			 * value.
			 */
			static char& delimiterRef()
			{
				static char delim = ' ';
				return delim;
			}

		protected:
			/**
			 * The single char flag used to identify the argument.
			 * This value (preceded by a dash {-}), can be used to identify
			 * an argument on the command line.  The _flag can be blank,
			 * in fact this is how unlabeled args work.  Unlabeled args must
			 * override appropriate functions to get correct handling. Note
			 * that the _flag does NOT include the dash as part of the flag.
			 */
			std::string _flag;

			/**
			 * A single work namd indentifying the argument.
			 * This value (preceded by two dashed {--}) can also be used
			 * to identify an argument on the command line.  Note that the
			 * _name does NOT include the two dashes as part of the _name. The
			 * _name cannot be blank.
			 */
			std::string _name;

			/**
			 * Description of the argument.
			 */
			std::string _description;

			/**
			 * Indicating whether the argument is required.
			 */
			bool _required;

			/**
			 * Label to be used in usage description.  Normally set to
			 * "required", but can be changed when necessary.
			 */
			std::string _requireLabel;

			/**
			 * Indicates whether a value is required for the argument.
			 * Note that the value may be required but the argument/value
			 * combination may not be, as specified by _required.
			 */
			bool _valueRequired;

			/**
			 * Indicates whether the argument has been set.
			 * Indicates that a value on the command line has matched the
			 * name/flag of this argument and the values have been set accordingly.
			 */
			bool _alreadySet;

			/**
			 * A pointer to a vistitor object.
			 * The visitor allows special handling to occur as soon as the
			 * argument is matched.  This defaults to NULL and should not
			 * be used unless absolutely necessary.
			 */
			Visitor *_visitor;

			/**
			 * Whether this argument can be ignored, if desired.
			 */
			bool _ignoreable;

			/**
			 * Indicates that the arg was set as part of an XOR and not on the
			 * command line.
			 */
			bool _xorSet;

			bool _acceptsMultipleValues;

			/**
			 * Performs the special handling described by the Vistitor.
			 */
			void _checkWithVisitor() const;

			/**
			 * Primary constructor. YOU (yes you) should NEVER construct an Arg
			 * directly, this is a base class that is extended by various children
			 * that are meant to be used.  Use SwitchArg, ValueArg, MultiArg,
			 * UnlabeledValueArg, or UnlabeledMultiArg instead.
			 *
			 * \param flag - The flag identifying the argument.
			 * \param name - The name identifying the argument.
			 * \param desc - The description of the argument, used in the usage.
			 * \param req - Whether the argument is required.
			 * \param valreq - Whether the a value is required for the argument.
			 * \param v - The visitor checked by the argument. Defaults to NULL.
			 */
			Arg(const std::string &flag, const std::string &name, const std::string &desc, bool req,
			        bool valreq, Visitor *v = NULL);

		public:
			/**
			 * Destructor.
			 */
			virtual ~Arg();

			/**
			 * Adds this to the specified list of Args.
			 * \param argList - The list to add this to.
			 */
			virtual void addToList(std::list<Arg*> &argList) const;

			/**
			 * Begin ignoring arguments since the "--" argument was specified.
			 */
			static void beginIgnoring()
			{
				ignoreRestRef() = true;
			}

			/**
			 * Whether to ignore the rest.
			 */
			static bool ignoreRest()
			{
				return ignoreRestRef();
			}

			/**
			 * The delimiter that separates an argument flag/name from the
			 * value.
			 */
			static char delimiter()
			{
				return delimiterRef();
			}

			/**
			 * The char used as a place holder when SwitchArgs are combined.
			 * Currently set to the bell char (ASCII 7).
			 */
			static char blankChar()
			{
				return (char)7;
			}

			/**
			 * The char that indicates the beginning of a flag.  Currently '-'.
			 */
			static char flagStartChar()
			{
				return '-';
			}

			/**
			 * The sting that indicates the beginning of a flag.  Currently "-".
			 * Should be identical to flagStartChar.
			 */
			static const std::string flagStartString()
			{
				return "-";
			}

			/**
			 * The sting that indicates the beginning of a name.  Currently "--".
			 * Should be flagStartChar twice.
			 */
			static const std::string nameStartString()
			{
				return "--";
			}

			/**
			 * The name used to identify the ignore rest argument.
			 */
			static const std::string ignoreNameString()
			{
				return "ignore_rest";
			}

			/**
			 * Sets the delimiter for all arguments.
			 * \param c - The character that delimits flags/names from values.
			 */
			static void setDelimiter(char c)
			{
				delimiterRef() = c;
			}

			/**
			 * Pure virtual method meant to handle the parsing and value assignment
			 * of the string on the command line.
			 * \param i - Pointer the the current argument in the list.
			 * \param args - Mutable list of strings. What is
			 * passed in from main.
			 */
			virtual bool processArg(int *i, std::vector<std::string> &args) = 0;

			/**
			 * Operator ==.
			 * Equality operator. Must be virtual to handle unlabeled args.
			 * \param a - The Arg to be compared to this.
			 */
			virtual bool operator==(const Arg &a) const;

			/**
			 * Returns the argument flag.
			 */
			const std::string& getFlag() const;

			/**
			 * Returns the argument name.
			 */
			const std::string& getName() const;

			/**
			 * Returns the argument description.
			 */
			std::string getDescription() const;

			/**
			 * Indicates whether the argument is required.
			 */
			virtual bool isRequired() const;

			/**
			 * Sets _required to true. This is used by the XorHandler.
			 * You really have no reason to ever use it.
			 */
			void forceRequired();

			/**
			 * Sets the _alreadySet value to true.  This is used by the XorHandler.
			 * You really have no reason to ever use it.
			 */
			void xorSet();

			/**
			 * Indicates whether a value must be specified for argument.
			 */
			bool isValueRequired() const;

			/**
			 * Indicates whether the argument has already been set.  Only true
			 * if the arg has been matched on the command line.
			 */
			bool isSet() const;

			/**
			 * Indicates whether the argument can be ignored, if desired.
			 */
			bool isIgnoreable() const;

			/**
			 * A method that tests whether a string matches this argument.
			 * This is generally called by the processArg() method.  This
			 * method could be re-implemented by a child to change how
			 * arguments are specified on the command line.
			 * \param s - The string to be compared to the flag/name to determine
			 * whether the arg matches.
			 */
			virtual bool argMatches(const std::string &s) const;

			/**
			 * Returns a simple string representation of the argument.
			 * Primarily for debugging.
			 */
			virtual std::string toString() const;

			/**
			 * Returns a short ID for the usage.
			 * \param valueId - The value used in the id.
			 */
			virtual std::string shortID(const std::string &valueId = "val") const;

			/**
			 * Returns a long ID for the usage.
			 * \param valueId - The value used in the id.
			 */
			virtual std::string longID(const std::string &valueId = "val") const;

			/**
			 * Trims a value off of the flag.
			 * \param flag - The string from which the flag and value will be
			 * trimmed. Contains the flag once the value has been trimmed.
			 * \param value - Where the value trimmed from the string will
			 * be stored.
			 */
			virtual void trimFlag(std::string &flag, std::string &value) const;

			/**
			 * Checks whether a given string has blank chars, indicating that
			 * it is a combined SwitchArg.  If so, return true, otherwise return
			 * false.
			 * \param s - string to be checked.
			 */
			bool _hasBlanks(const std::string &s) const;

			/**
			 * Sets the requireLabel. Used by XorHandler.  You shouldn't ever
			 * use this.
			 * \param s - Set the requireLabel to this value.
			 */
			void setRequireLabel(const std::string &s);

			/**
			 * Used for MultiArgs and XorHandler to determine whether args
			 * can still be set.
			 */
			virtual bool allowMore();

			/**
			 * Use by output classes to determine whether an Arg accepts
			 * multiple values.
			 */
			virtual bool acceptsMultipleValues();

			/**
			 * Clears the Arg object and allows it to be reused by new
			 * command lines.
			 */
			virtual void reset();
	};

	/**
	 * Typedef of an Arg list iterator.
	 */
	typedef std::list<Arg*>::iterator ArgListIterator;

	/**
	 * Typedef of an Arg vector iterator.
	 */
	typedef std::vector<Arg*>::iterator ArgVectorIterator;

	/**
	 * Typedef of a Visitor list iterator.
	 */
	typedef std::list<Visitor*>::iterator VisitorListIterator;

	/*
	 * Extract a value of type T from it's string representation contained
	 * in strVal. The ValueLike parameter used to select the correct
	 * specialization of ExtractValue depending on the value traits of T.
	 * ValueLike traits use operator>> to assign the value from strVal.
	 */
	template<typename T>
	void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
	{
		static_cast<void>(vl); // Avoid warning about unused vl
		std::istringstream is(strVal);

		int valuesRead = 0;
		while (is.good())
		{
			if (is.peek() != EOF)
#ifdef TCLAP_SETBASE_ZERO
			{
				is >> std::setbase(0) >> destVal;
			}
#else
			{
				is >> destVal;
			}
#endif
			else
			{
				break;
			}

			valuesRead++;
		}

		if (is.fail())
		{
			throw (ArgParseException("Couldn't read argument value "
			                         "from string '" + strVal + "'"));
		}

		if (valuesRead > 1)
		{
			throw (ArgParseException("More than one valid value parsed from "
			                         "string '" + strVal + "'"));
		}
	}

	/*
	 * Extract a value of type T from it's string representation contained
	 * in strVal. The ValueLike parameter used to select the correct
	 * specialization of ExtractValue depending on the value traits of T.
	 * StringLike uses assignment (operator=) to assign from strVal.
	 */
	template<typename T>
	void ExtractValue(T &destVal, const std::string &strVal, StringLike sl)
	{
		static_cast<void>(sl); // Avoid warning about unused sl
		SetString(destVal, strVal);
	}

	//////////////////////////////////////////////////////////////////////
	//BEGIN Arg.cpp
	//////////////////////////////////////////////////////////////////////

	inline Arg::Arg(const std::string &flag, const std::string &name, const std::string &desc,
	        bool req, bool valreq, Visitor *v) :
			_flag(flag), _name(name), _description(desc), _required(req), _requireLabel("required"),
			_valueRequired(valreq), _alreadySet(false), _visitor(v), _ignoreable(true),
			_xorSet(false), _acceptsMultipleValues(false)
	{
		if (_flag.length() > 1)
		{
			throw (SpecificationException(
							"Argument flag can only be one character long", toString()));
		}

		if (_name != ignoreNameString() &&
		        (_flag == Arg::flagStartString() ||
		        _flag == Arg::nameStartString() ||
		        _flag == " "))
		{
			throw (SpecificationException("Argument flag cannot be either '" + Arg::flagStartString() +
			      "' or '" + Arg::nameStartString() + "' or a space.", toString()));
		}

		if ((_name.substr(0, Arg::flagStartString().length()) == Arg::flagStartString()) ||
		        (_name.substr(0, Arg::nameStartString().length()) == Arg::nameStartString()) ||
		        (_name.find(" ", 0) != std::string::npos))
		{
			throw (SpecificationException("Argument name begin with either '" + Arg::flagStartString() +
			      "' or '" + Arg::nameStartString() + "' or space.", toString()));
		}
	}

	inline Arg::~Arg()
	{
	}

	inline std::string Arg::shortID(const std::string &valueId) const
	{
		std::string id = "";

		if (_flag != "")
		{
			id = Arg::flagStartString() + _flag;
		}
		else
		{
			id = Arg::nameStartString() + _name;
		}

		if (_valueRequired)
		{
			id += std::string(1, Arg::delimiter()) + "<" + valueId + ">";
		}

		if (!_required)
		{
			id = "[" + id + "]";
		}

		return id;
	}

	inline std::string Arg::longID(const std::string &valueId) const
	{
		std::string id = "";

		if (_flag != "")
		{
			id += Arg::flagStartString() + _flag;

			if (_valueRequired)
			{
				id += std::string(1, Arg::delimiter()) + "<" + valueId + ">";
			}

			id += ",  ";
		}

		id += Arg::nameStartString() + _name;

		if (_valueRequired)
		{
			id += std::string(1, Arg::delimiter()) + "<" + valueId + ">";
		}

		return id;
	}

	inline bool Arg::operator==(const Arg &a) const
	{
		if ((_flag != "" && _flag == a._flag) || _name == a._name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	inline std::string Arg::getDescription() const
	{
		std::string desc = "";
		if (_required)
		{
			desc = "(" + _requireLabel + ")  ";
		}

		//	if ( _valueRequired )
		//		desc += "(value required)  ";

		desc += _description;
		return desc;
	}

	inline const std::string& Arg::getFlag() const
	{
		return _flag;
	}

	inline const std::string& Arg::getName() const
	{
		return _name;
	}

	inline bool Arg::isRequired() const
	{
		return _required;
	}

	inline bool Arg::isValueRequired() const
	{
		return _valueRequired;
	}

	inline bool Arg::isSet() const
	{
		if (_alreadySet && !_xorSet)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	inline bool Arg::isIgnoreable() const
	{
		return _ignoreable;
	}

	inline void Arg::setRequireLabel(const std::string &s)
	{
		_requireLabel = s;
	}

	inline bool Arg::argMatches(const std::string &argFlag) const
	{
		if ((argFlag == Arg::flagStartString() + _flag && _flag != "") ||
		        argFlag == Arg::nameStartString() + _name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	inline std::string Arg::toString() const
	{
		std::string s = "";

		if (_flag != "")
		{
			s += Arg::flagStartString() + _flag + " ";
		}

		s += "(" + Arg::nameStartString() + _name + ")";

		return s;
	}

	inline void Arg::_checkWithVisitor() const
	{
		if (_visitor != NULL)
		{
			_visitor->visit();
		}
	}

	/**
	 * Implementation of trimFlag.
	 */
	inline void Arg::trimFlag(std::string &flag, std::string &value) const
	{
		int stop = 0;
		for (int i = 0; static_cast<unsigned int>(i) < flag.length(); i++)
		{
			if (flag[i] == Arg::delimiter())
			{
				stop = i;
				break;
			}
		}

		if (stop > 1)
		{
			value = flag.substr(stop + 1);
			flag  = flag.substr(0, stop);
		}
	}

	/**
	 * Implementation of _hasBlanks.
	 */
	inline bool Arg::_hasBlanks(const std::string &s) const
	{
		for (int i = 1; static_cast<unsigned int>(i) < s.length(); i++)
		{
			if (s[i] == Arg::blankChar())
			{
				return true;
			}
		}

		return false;
	}

	inline void Arg::forceRequired()
	{
		_required = true;
	}

	inline void Arg::xorSet()
	{
		_alreadySet = true;
		_xorSet     = true;
	}

	/**
	 * Overridden by Args that need to added to the end of the list.
	 */
	inline void Arg::addToList(std::list<Arg*> &argList) const
	{
		argList.push_front(const_cast<Arg*>(this));
	}

	inline bool Arg::allowMore()
	{
		return false;
	}

	inline bool Arg::acceptsMultipleValues()
	{
		return _acceptsMultipleValues;
	}

	inline void Arg::reset()
	{
		_xorSet     = false;
		_alreadySet = false;
	}

	//////////////////////////////////////////////////////////////////////
	//END Arg.cpp
	//////////////////////////////////////////////////////////////////////
} //namespace TCLAP

#endif